|
1 | 1 | using System; |
2 | | -using System.Net.Http; |
3 | | -using System.Text; |
4 | 2 | using System.Threading.Tasks; |
5 | 3 | using UnityEngine; |
6 | 4 |
|
7 | 5 | namespace TaloGameServices |
8 | 6 | { |
9 | 7 | public class PlayersAPI : BaseAPI |
10 | 8 | { |
11 | | - public PlayersAPI(TaloSettings settings, HttpClient client) : base(settings, client, "players") { } |
| 9 | + public PlayersAPI(TaloManager manager) : base(manager, "players") { } |
12 | 10 |
|
13 | 11 | public async void Create(PlayerAlias alias) |
14 | 12 | { |
15 | | - var req = new HttpRequestMessage(); |
16 | | - req.Method = HttpMethod.Post; |
17 | | - req.RequestUri = new Uri(baseUrl); |
18 | | - req.Content = new StringContent(JsonUtility.ToJson(alias), Encoding.UTF8, "application/json"); |
| 13 | + var uri = new Uri(baseUrl); |
| 14 | + var content = JsonUtility.ToJson(alias); |
19 | 15 |
|
20 | | - string json = await Call(req); |
| 16 | + var json = await Call(uri, "POST", content); |
21 | 17 | var res = JsonUtility.FromJson<PlayersIdentifyResponse>(json); |
22 | 18 |
|
23 | 19 | Talo.CurrentAlias = res.alias; |
24 | 20 | } |
25 | 21 |
|
26 | 22 | public async Task Identify(string service, string identifier) |
27 | 23 | { |
28 | | - var req = new HttpRequestMessage(); |
29 | | - req.Method = HttpMethod.Get; |
30 | | - req.RequestUri = new Uri(baseUrl + $"/identify?service={service}&identifier={identifier}"); |
| 24 | + var uri = new Uri(baseUrl + $"/identify?service={service}&identifier={identifier}"); |
31 | 25 |
|
32 | | - string json = await Call(req); |
| 26 | + var json = await Call(uri, "GET"); |
33 | 27 | var res = JsonUtility.FromJson<PlayersIdentifyResponse>(json); |
| 28 | + |
34 | 29 | Talo.CurrentAlias = res.alias; |
35 | 30 |
|
36 | 31 | await Talo.Saves.GetSaves(); |
37 | 32 | } |
38 | 33 |
|
39 | 34 | public async void Update() |
40 | 35 | { |
41 | | - var req = new HttpRequestMessage(); |
42 | | - req.Method = new HttpMethod("PATCH"); |
43 | | - req.RequestUri = new Uri(baseUrl + $"/{Talo.CurrentPlayer.id}"); |
44 | | - |
45 | | - string content = JsonUtility.ToJson(Talo.CurrentPlayer); |
46 | | - req.Content = new StringContent(content, Encoding.UTF8, "application/json"); |
| 36 | + var uri = new Uri(baseUrl + $"/{Talo.CurrentPlayer.id}"); |
| 37 | + var content = JsonUtility.ToJson(Talo.CurrentPlayer); |
47 | 38 |
|
48 | | - string json = await Call(req); |
| 39 | + var json = await Call(uri, "PATCH", content); |
49 | 40 | var res = JsonUtility.FromJson<PlayersUpdateResponse>(json); |
| 41 | + |
50 | 42 | Talo.CurrentPlayer = res.player; |
51 | 43 | } |
52 | 44 |
|
53 | 45 | public async Task<Player> Merge(string alias1, string alias2) |
54 | 46 | { |
55 | | - var req = new HttpRequestMessage(); |
56 | | - req.Method = new HttpMethod("POST"); |
57 | | - req.RequestUri = new Uri(baseUrl + "/merge"); |
58 | | - |
| 47 | + var uri = new Uri(baseUrl + "/merge"); |
59 | 48 | string content = JsonUtility.ToJson(new PlayersMergeRequest(alias1, alias2)); |
60 | | - req.Content = new StringContent(content, Encoding.UTF8, "application/json"); |
61 | 49 |
|
62 | | - string json = await Call(req); |
| 50 | + string json = await Call(uri, "POST", content); |
63 | 51 | var res = JsonUtility.FromJson<PlayersUpdateResponse>(json); |
| 52 | + |
64 | 53 | return res.player; |
65 | 54 | } |
66 | 55 | } |
|
0 commit comments