-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ConfigLoader - add additional params #13
base: dev
Are you sure you want to change the base?
Changes from 1 commit
31ca489
e06863f
bdfaaba
ba1fb90
b9a7c5f
0d17153
a513e9b
24815e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,29 +14,26 @@ public class SnipeConfigLoader | |
private readonly string _url; | ||
private readonly IApplicationInfo _appInfo; | ||
|
||
private string _requestParamsJson; | ||
public SnipeConfigLoader(string projectID, IApplicationInfo appInfo) | ||
{ | ||
_projectID = projectID; | ||
_appInfo = appInfo; | ||
_url = "https://config.snipe.dev/api/v1/configStrings"; | ||
} | ||
|
||
public async UniTask<Dictionary<string, object>> Load() | ||
public void SetRequestParams(string requestParams) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а тут целый ряд проблем
Load(Dictionary<string, object> additionalParams)
{
string requestParamsJson = BuildRequestParamsJson(additionalParams);
//... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Согласен, перенесу |
||
{ | ||
string requestParamsJson = "{" + | ||
$"\"project\":\"{_projectID}\"," + | ||
$"\"deviceID\":\"{_appInfo.DeviceIdentifier}\"," + | ||
$"\"identifier\":\"{_appInfo.ApplicationIdentifier}\"," + | ||
$"\"version\":\"{_appInfo.ApplicationVersion}\"," + | ||
$"\"platform\":\"{_appInfo.ApplicationPlatform}\"," + | ||
$"\"packageVersion\":\"{PackageInfo.VERSION_CODE}\"" + | ||
"}"; | ||
_requestParamsJson = requestParams; | ||
} | ||
|
||
public async UniTask<Dictionary<string, object>> Load() | ||
{ | ||
Dictionary<string, object> config = null; | ||
|
||
try | ||
{ | ||
using var request = UnityWebRequest.Post(_url, requestParamsJson, "application/json"); | ||
using var request = UnityWebRequest.Post(_url, _requestParamsJson, "application/json"); | ||
request.downloadHandler = new DownloadHandlerBuffer(); | ||
var response = await request.SendWebRequest().ToUniTask(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using Cysharp.Threading.Tasks; | ||
using fastJSON; | ||
using UnityEngine; | ||
|
||
namespace MiniIT.Snipe | ||
{ | ||
public interface ISnipeConfigLoadingService | ||
{ | ||
Dictionary<string, object> Config { get; } | ||
UniTask<Dictionary<string, object>> Load(CancellationToken cancellationToken = default); | ||
void Reset(); | ||
} | ||
|
||
public class SnipeConfigLoadingService : ISnipeConfigLoadingService | ||
|
@@ -53,10 +57,57 @@ await UniTask.WaitUntil(() => SnipeServices.IsInitialized, PlayerLoopTiming.Upda | |
_loader ??= new SnipeConfigLoader(_projectID, SnipeServices.ApplicationInfo); | ||
} | ||
|
||
var loadedAdditionalParams = await LoadAdditionalParamsAsync(); | ||
|
||
string requestParams = BuildRequestParamsJson(SnipeServices.ApplicationInfo, loadedAdditionalParams); | ||
_loader.SetRequestParams(requestParams); | ||
|
||
_config = await _loader.Load(); | ||
_loading = false; | ||
|
||
return _config; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_config = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. сначала нужно остановить загрузку (которая теоретически может выполняться в данный момент) через CancellationToken. иначе она завершится в параллельном потоке и присвоит своё значение конфигу вне зависимости от того, что тут ему присваивается null |
||
_loading = false; | ||
} | ||
|
||
private string BuildRequestParamsJson(IApplicationInfo appInfo, Dictionary<string, object> additionalParams = null) | ||
{ | ||
var requestParams = new Dictionary<string, object> | ||
{ | ||
{ "project", _projectID }, | ||
{ "deviceID", appInfo.DeviceIdentifier }, | ||
{ "identifier", appInfo.ApplicationIdentifier }, | ||
{ "version", appInfo.ApplicationVersion }, | ||
{ "platform", appInfo.ApplicationPlatform }, | ||
{ "packageVersion", PackageInfo.VERSION_CODE } | ||
}; | ||
|
||
if (additionalParams != null) | ||
{ | ||
foreach (var param in additionalParams) | ||
{ | ||
requestParams[param.Key] = param.Value; | ||
} | ||
} | ||
|
||
return JSON.ToJSON(requestParams); | ||
} | ||
|
||
private async UniTask<Dictionary<string, object>> LoadAdditionalParamsAsync() | ||
{ | ||
string filePath = Path.Combine(Application.persistentDataPath, string.Format("additionalParams{0}.json", Application.version)); | ||
|
||
if (!File.Exists(filePath)) | ||
{ | ||
return new Dictionary<string, object>(); | ||
} | ||
|
||
string json = await File.ReadAllTextAsync(filePath); | ||
return (Dictionary<string, object>)JSON.Parse(json); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
отделяй методы пустой строкой