Skip to content

Commit 416b5c4

Browse files
committed
Use UnityHttpClient in ApiController
1 parent 50df99d commit 416b5c4

4 files changed

Lines changed: 58 additions & 66 deletions

File tree

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
1+
using System;
12
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Threading.Tasks;
35
using UnityEngine.Networking;
4-
using WalletConnectUnity.Core;
6+
using WalletConnect.Web3Modal.Http;
57
using WalletConnectUnity.Core.Networking;
68

79
namespace WalletConnect.Web3Modal
810
{
911
public class ApiController
1012
{
11-
public readonly UnityWebRequestWalletsFactory walletsRequestsFactory;
12-
13-
public ApiController()
14-
{
15-
walletsRequestsFactory = new UnityWebRequestWalletsFactory(
16-
includedWalletIds: Web3Modal.Config.includedWalletIds,
17-
excludedWalletIds: Web3Modal.Config.excludedWalletIds
18-
);
19-
}
20-
21-
public async ValueTask<GetWalletsResponse> GetWallets(int page, int count, string search = null)
13+
private readonly string _includedWalletIdsString = Web3Modal.Config.includedWalletIds is { Length: > 0 }
14+
? string.Join(",", Web3Modal.Config.includedWalletIds)
15+
: null;
16+
private readonly string _excludedWalletIdsString = Web3Modal.Config.excludedWalletIds is { Length: > 0 }
17+
? string.Join(",", Web3Modal.Config.excludedWalletIds)
18+
: null;
19+
20+
private readonly UnityHttpClient _httpClient = new(new Uri(BasePath), TimeSpan.FromSeconds(TimoutSeconds),
21+
new Web3ModalApiHeaderDecorator()
22+
);
23+
24+
private const string BasePath = "https://api.web3modal.com/";
25+
private const int TimoutSeconds = 5;
26+
27+
private const string Platform =
28+
#if UNITY_ANDROID
29+
"android";
30+
#elif UNITY_IOS
31+
"ios";
32+
#else
33+
null;
34+
#endif
35+
36+
public async Task<GetWalletsResponse> GetWallets(int page, int count, string search = null)
2237
{
2338
if (page < 1)
24-
throw new System.ArgumentOutOfRangeException(nameof(page), "Page must be greater than 0");
39+
throw new ArgumentOutOfRangeException(nameof(page), "Page must be greater than 0");
2540

2641
if (count < 1)
27-
throw new System.ArgumentOutOfRangeException(nameof(count), "Count must be greater than 0");
28-
29-
return await GetWalletsCore(page, count);
30-
}
42+
throw new ArgumentOutOfRangeException(nameof(count), "Count must be greater than 0");
3143

32-
protected virtual async Task<GetWalletsResponse> GetWalletsCore(int page, int count, string search = null)
33-
{
34-
using var uwr = walletsRequestsFactory.GetWallets(page, count, search);
35-
36-
// TODO: use Awaitable in Unity 2023.1+
37-
var tcs = new TaskCompletionSource<GetWalletsResponse>();
38-
UnityEventsDispatcher.Instance.StartCoroutine(SendWebRequest(uwr, tcs));
39-
return await tcs.Task;
40-
}
4144

42-
protected static IEnumerator SendWebRequest<T>(
43-
UnityWebRequest uwr,
44-
TaskCompletionSource<T> tcs)
45-
{
46-
yield return uwr.SendWebRequest();
47-
48-
if (uwr.result != UnityWebRequest.Result.Success)
45+
return await _httpClient.GetAsync<GetWalletsResponse>("getWallets", new Dictionary<string, string>()
4946
{
50-
// TODO: use custom ex type
51-
tcs.SetException(new System.Exception($"Failed to send web request: {uwr.error}"));
52-
yield break;
53-
}
54-
55-
var response = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(uwr.downloadHandler.text);
56-
tcs.SetResult(response);
47+
{"page", page.ToString()},
48+
{"entries", count.ToString()},
49+
{"search", search},
50+
{"platform", Platform},
51+
{"include", _includedWalletIdsString},
52+
{"exclude", _excludedWalletIdsString}
53+
});
5754
}
5855
}
5956
}

Packages/com.walletconnect.web3modal/Runtime/Http/UnityHttpClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,12 @@ protected override Task<HttpResponseContext> SendAsyncCore(HttpRequestContext re
173173
if (uwr.result != UnityWebRequest.Result.Success)
174174
{
175175
tcs.SetException(new Exception($"Failed to send web request: {uwr.error}")); // TODO: use custom ex type
176+
uwr.Dispose();
176177
return;
177178
}
178179

179180
tcs.SetResult(new HttpResponseContext(uwr.downloadHandler.data, uwr.responseCode, uwr.GetResponseHeaders()));
181+
uwr.Dispose();
180182
};
181183

182184
return tcs.Task;

Packages/com.walletconnect.web3modal/Runtime/Presenters/WalletSearchPresenter.cs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using Newtonsoft.Json;
45
using UnityEngine;
56
using UnityEngine.Networking;
@@ -21,15 +22,15 @@ public override string Title
2122
}
2223

2324
private bool _isPageLoading;
24-
private bool __reachedMaxWalletsCount;
2525

2626
private int _maxWalletsCount = -1;
2727
private int _nextPageToLoad = 1;
2828
private bool _reachedMaxWalletsCount = false;
2929
private int _countPerPageRealtime = 0;
3030
private int _walletsShown = 0;
3131
private string _searchQuery = null;
32-
private Coroutine _loadNextPageCoroutine;
32+
33+
private Task _loadNextPageTask;
3334

3435
private const int WalletsPerPage = 32;
3536

@@ -57,7 +58,6 @@ private void OnSearchInputValueChanged(string value)
5758
_reachedMaxWalletsCount = false;
5859
_countPerPageRealtime = WalletsPerPage;
5960
_isPageLoading = false;
60-
__reachedMaxWalletsCount = false;
6161

6262
foreach (var item in _items)
6363
item.RemoveFromHierarchy();
@@ -86,7 +86,7 @@ private void ConfigureItemPaddings(IList<VisualElement> items = null)
8686

8787
private void OnScrollValueChanged(float value)
8888
{
89-
if (_isPageLoading || __reachedMaxWalletsCount)
89+
if (_isPageLoading || _reachedMaxWalletsCount)
9090
return;
9191

9292
if (value >= (View.scroller.highValue - View.scroller.lowValue) * 0.7f)
@@ -116,17 +116,18 @@ private VisualElement MakeItem(Wallet wallet)
116116
return item;
117117
}
118118

119-
private void LoadNextPage()
119+
private async void LoadNextPage()
120120
{
121121
if (_isPageLoading || _reachedMaxWalletsCount)
122122
return;
123-
124-
if (_loadNextPageCoroutine != null) UnityEventsDispatcher.Instance.StopCoroutine(_loadNextPageCoroutine);
125-
126-
_loadNextPageCoroutine = UnityEventsDispatcher.Instance.StartCoroutine(LoadNextPageCoroutine());
123+
124+
if (_loadNextPageTask != null && !_loadNextPageTask.IsCompleted)
125+
return;
126+
127+
_loadNextPageTask = LoadNextPageCoroutine();
127128
}
128129

129-
private IEnumerator LoadNextPageCoroutine()
130+
private async Task LoadNextPageCoroutine()
130131
{
131132
_isPageLoading = true;
132133

@@ -139,27 +140,16 @@ private IEnumerator LoadNextPageCoroutine()
139140
if (_countPerPageRealtime == 0)
140141
{
141142
_isPageLoading = false;
142-
yield break;
143+
return;
143144
}
144145

145-
146-
// TODO: the request should be happening in the api controller
147-
using var uwr =
148-
Web3Modal.ApiController.walletsRequestsFactory.GetWallets(
149-
_nextPageToLoad,
150-
_countPerPageRealtime, _searchQuery);
151-
152-
yield return uwr.SendWebRequest();
153-
154-
if (uwr.result != UnityWebRequest.Result.Success) yield break;
155-
156-
var response = JsonConvert.DeserializeObject<GetWalletsResponse>(uwr.downloadHandler.text);
146+
var getWalletsResponse = await Web3Modal.ApiController.GetWallets(_nextPageToLoad, _countPerPageRealtime, _searchQuery);
157147

158148
// _noWalletFound.SetActive(response.Count == 0);
159149

160150
if (_maxWalletsCount == -1)
161151
{
162-
_maxWalletsCount = response.Count;
152+
_maxWalletsCount = getWalletsResponse.Count;
163153

164154
if (_nextPageToLoad * _countPerPageRealtime > _maxWalletsCount)
165155
{
@@ -168,12 +158,12 @@ private IEnumerator LoadNextPageCoroutine()
168158
}
169159
}
170160

171-
var walletsCount = response.Data.Length;
161+
var walletsCount = getWalletsResponse.Data.Length;
172162

173163
var items = new List<VisualElement>(walletsCount);
174164
for (var i = 0; i < walletsCount; i++)
175165
{
176-
var wallet = response.Data[i];
166+
var wallet = getWalletsResponse.Data[i];
177167
var item = MakeItem(wallet);
178168
items.Add(item);
179169
View.scrollView.Add(item);

Packages/com.walletconnect.web3modal/Runtime/Utils/Extensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public static string AppendQueryString(this string path, IDictionary<string, str
4141
var queryString = new StringBuilder();
4242
foreach (var param in queryParameters)
4343
{
44+
if (param.Value == default)
45+
continue;
46+
4447
if (queryString.Length > 0)
4548
queryString.Append("&");
4649

0 commit comments

Comments
 (0)