Skip to content

Commit d58697a

Browse files
Merge pull request #29 from LootLocker/fix/refactors
Fix/refactors Fix a mapping bug when LootLockerLevel_Thresholds next property is returned as null from the server
2 parents 63d8b25 + 0418073 commit d58697a

20 files changed

+345
-1390
lines changed

Runtime/Client/LootLockerServerRequest.cs

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum LootLockerHTTPMethod
2323
CREATE = 5,
2424
OPTIONS = 6,
2525
PATCH = 7,
26-
UPLOAD = 8
26+
UPLOAD = 8
2727
}
2828

2929
/// <summary>
@@ -32,34 +32,66 @@ public enum LootLockerHTTPMethod
3232
[System.Serializable]
3333
public class LootLockerResponse
3434
{
35-
3635
/// <summary>
3736
/// TRUE if http error OR server returns an error status
3837
/// </summary>
3938
public bool hasError;
39+
4040
/// <summary>
4141
/// HTTP Status Code
4242
/// </summary>
4343
public int statusCode;
44+
4445
/// <summary>
4546
/// Raw text response from the server
4647
/// <para>If hasError = true, this will contain the error message.</para>
4748
/// </summary>
4849
public string text;
50+
4951
public bool success;
5052

5153

5254
public string Error;
55+
5356
/// <summary>
5457
/// A texture downloaded in the webrequest, if applicable, otherwise this will be null.
5558
/// </summary>
5659
public Texture2D texture;
60+
5761
/// <summary>
5862
/// inheritdoc added this because unity main thread excuting style cut the calling stack and make the event orphant seealso calling multiple events
5963
/// of the same type makes use unable to identify each one
6064
/// </summary>
6165
public string EventId;
6266

67+
public static void Serialize<T>(Action<T> onComplete, LootLockerResponse serverResponse)
68+
where T : LootLockerResponse
69+
{
70+
if (!string.IsNullOrEmpty(serverResponse.Error)) return;
71+
72+
var response = JsonConvert.DeserializeObject<T>(serverResponse.text);
73+
74+
response.text = serverResponse.text;
75+
response.success = serverResponse.success;
76+
response.Error = serverResponse.Error;
77+
response.statusCode = serverResponse.statusCode;
78+
onComplete?.Invoke(response);
79+
}
80+
81+
public static T Serialize<T>(LootLockerResponse serverResponse)
82+
where T : LootLockerResponse
83+
{
84+
if (!string.IsNullOrEmpty(serverResponse.Error)) return null;
85+
86+
var response = JsonConvert.DeserializeObject<T>(serverResponse.text);
87+
88+
response.text = serverResponse.text;
89+
response.success = serverResponse.success;
90+
response.Error = serverResponse.Error;
91+
response.statusCode = serverResponse.statusCode;
92+
93+
return response;
94+
}
6395
}
6496

6597
/// <summary>
@@ -70,7 +102,7 @@ public class LootLockerResponseFactory
70102
/// <summary>
71103
/// Construct an error response to send to the client.
72104
/// </summary>
73-
public static T Error<T> (string errorMessage) where T : LootLockerResponse, new()
105+
public static T Error<T>(string errorMessage) where T : LootLockerResponse, new()
74106
{
75107
return new T()
76108
{
@@ -105,20 +137,24 @@ public struct LootLockerServerRequest
105137
public string uploadType;
106138
public LootLocker.LootLockerEnums.LootLockerCallerRole adminCall;
107139
public WWWForm form;
140+
108141
/// <summary>
109142
/// Leave this null if you don't need custom headers
110143
/// </summary>
111144
public Dictionary<string, string> extraHeaders;
145+
112146
/// <summary>
113147
/// Query parameters to append to the end of the request URI
114148
/// <para>Example: If you include a dictionary with a key of "page" and a value of "42" (as a string) then the url would become "https: //mydomain.com/endpoint?page=42"</para>
115149
/// </summary>
116150
public Dictionary<string, string> queryParams;
151+
117152
public int retryCount;
118153

119154
#region Make ServerRequest and call send (3 functions)
120155

121-
public static void CallAPI(string endPoint, LootLockerHTTPMethod httpMethod, string body = null, Action<LootLockerResponse> onComplete = null, bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
156+
public static void CallAPI(string endPoint, LootLockerHTTPMethod httpMethod, string body = null, Action<LootLockerResponse> onComplete = null, bool useAuthToken = true,
157+
LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
122158
{
123159
#if UNITY_EDITOR
124160
LootLockerSDKManager.DebugMessage("Caller Type: " + callerRole.ToString());
@@ -129,27 +165,25 @@ public static void CallAPI(string endPoint, LootLockerHTTPMethod httpMethod, str
129165
if (useAuthToken)
130166
{
131167
headers = new Dictionary<string, string>();
132-
headers.Add(callerRole == LootLocker.LootLockerEnums.LootLockerCallerRole.Admin ? "x-auth-token" : "x-session-token", callerRole == LootLocker.LootLockerEnums.LootLockerCallerRole.Admin ? LootLockerConfig.current.adminToken : LootLockerConfig.current.token);
168+
headers.Add(callerRole == LootLocker.LootLockerEnums.LootLockerCallerRole.Admin ? "x-auth-token" : "x-session-token",
169+
callerRole == LootLocker.LootLockerEnums.LootLockerCallerRole.Admin ? LootLockerConfig.current.adminToken : LootLockerConfig.current.token);
133170
}
134171

135172
if (LootLockerConfig.current != null)
136173
headers.Add(LootLockerConfig.current.dateVersion.key, LootLockerConfig.current.dateVersion.value);
137174

138175
LootLockerBaseServerAPI.I.SwitchURL(callerRole);
139176

140-
new LootLockerServerRequest(endPoint, httpMethod, body, headers, callerRole: callerRole).Send((response) =>
141-
{
142-
onComplete?.Invoke(response);
143-
});
177+
new LootLockerServerRequest(endPoint, httpMethod, body, headers, callerRole: callerRole).Send((response) => { onComplete?.Invoke(response); });
144178
}
145179

146180
public static void CallDomainAuthAPI(string endPoint, LootLockerHTTPMethod httpMethod, string body = null, Action<LootLockerResponse> onComplete = null)
147181
{
148182
if (LootLockerConfig.current.domainKey.ToString().Length == 0)
149183
{
150-
#if UNITY_EDITOR
184+
#if UNITY_EDITOR
151185
LootLockerSDKManager.DebugMessage("LootLocker domain key must be set in settings", true);
152-
#endif
186+
#endif
153187
onComplete?.Invoke(LootLockerResponseFactory.Error<LootLockerResponse>("LootLocker domain key must be set in settings"));
154188

155189
return;
@@ -165,13 +199,11 @@ public static void CallDomainAuthAPI(string endPoint, LootLockerHTTPMethod httpM
165199

166200
LootLockerBaseServerAPI.I.SwitchURL(LootLockerCallerRole.Base);
167201

168-
new LootLockerServerRequest(endPoint, httpMethod, body, headers, callerRole: LootLockerCallerRole.Base).Send((response) =>
169-
{
170-
onComplete?.Invoke(response);
171-
});
202+
new LootLockerServerRequest(endPoint, httpMethod, body, headers, callerRole: LootLockerCallerRole.Base).Send((response) => { onComplete?.Invoke(response); });
172203
}
173204

174-
public static void UploadFile(string endPoint, LootLockerHTTPMethod httpMethod, byte[] file, string fileName = "file", string fileContentType = "text/plain", Dictionary<string, string> body = null, Action<LootLockerResponse> onComplete = null, bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
205+
public static void UploadFile(string endPoint, LootLockerHTTPMethod httpMethod, byte[] file, string fileName = "file", string fileContentType = "text/plain", Dictionary<string, string> body = null, Action<LootLockerResponse> onComplete = null,
206+
bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
175207
{
176208
Dictionary<string, string> headers = new Dictionary<string, string>();
177209

@@ -180,20 +212,19 @@ public static void UploadFile(string endPoint, LootLockerHTTPMethod httpMethod,
180212
headers = new Dictionary<string, string>();
181213

182214
headers.Add(callerRole == LootLockerCallerRole.Admin ? "x-auth-token" : "x-session-token", LootLockerConfig.current.token);
183-
184215
}
185216

186217
LootLockerBaseServerAPI.I.SwitchURL(callerRole);
187218

188-
new LootLockerServerRequest(endPoint, httpMethod, file, fileName, fileContentType, body, headers, callerRole: callerRole).Send((response) =>
189-
{
190-
onComplete?.Invoke(response);
191-
});
219+
new LootLockerServerRequest(endPoint, httpMethod, file, fileName, fileContentType, body, headers, callerRole: callerRole).Send((response) => { onComplete?.Invoke(response); });
192220
}
221+
193222
#endregion
194223

195224
#region ServerRequest constructor
196-
public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod = LootLockerHTTPMethod.GET, byte[] upload = null, string uploadName = null, string uploadType = null, Dictionary<string, string> body = null, Dictionary<string, string> extraHeaders = null, bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User, bool isFileUpload = true)
225+
226+
public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod = LootLockerHTTPMethod.GET, byte[] upload = null, string uploadName = null, string uploadType = null, Dictionary<string, string> body = null,
227+
Dictionary<string, string> extraHeaders = null, bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User, bool isFileUpload = true)
197228
{
198229
this.retryCount = 0;
199230
this.endpoint = endpoint;
@@ -223,7 +254,8 @@ public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod
223254
}
224255
}
225256

226-
public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod = LootLockerHTTPMethod.GET, Dictionary<string, object> payload = null, Dictionary<string, string> extraHeaders = null, Dictionary<string, string> queryParams = null, bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
257+
public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod = LootLockerHTTPMethod.GET, Dictionary<string, object> payload = null, Dictionary<string, string> extraHeaders = null, Dictionary<string, string> queryParams = null,
258+
bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
227259
{
228260
this.retryCount = 0;
229261
this.endpoint = endpoint;
@@ -243,7 +275,9 @@ public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod
243275
LootLockerSDKManager.DebugMessage("WARNING: Payloads should not be sent in GET, HEAD, OPTIONS, requests. Attempted to send a payload to: " + this.httpMethod.ToString() + " " + this.endpoint);
244276
}
245277
}
246-
public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod = LootLockerHTTPMethod.GET, string payload = null, Dictionary<string, string> extraHeaders = null, Dictionary<string, string> queryParams = null, bool useAuthToken = true, LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
278+
279+
public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod = LootLockerHTTPMethod.GET, string payload = null, Dictionary<string, string> extraHeaders = null, Dictionary<string, string> queryParams = null, bool useAuthToken = true,
280+
LootLocker.LootLockerEnums.LootLockerCallerRole callerRole = LootLocker.LootLockerEnums.LootLockerCallerRole.User)
247281
{
248282
this.retryCount = 0;
249283
this.endpoint = endpoint;
@@ -263,17 +297,15 @@ public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod
263297
LootLockerSDKManager.DebugMessage("WARNING: Payloads should not be sent in GET, HEAD, OPTIONS, requests. Attempted to send a payload to: " + this.httpMethod.ToString() + " " + this.endpoint);
264298
}
265299
}
300+
266301
#endregion
267302

268303
/// <summary>
269304
/// just debug and call ServerAPI.SendRequest which takes the current ServerRequest and pass this response
270305
/// </summary>
271306
public void Send(System.Action<LootLockerResponse> OnServerResponse)
272307
{
273-
LootLockerBaseServerAPI.I.SendRequest(this, (response) =>
274-
{
275-
OnServerResponse?.Invoke(response);
276-
});
308+
LootLockerBaseServerAPI.I.SendRequest(this, (response) => { OnServerResponse?.Invoke(response); });
277309
}
278310
}
279-
}
311+
}

0 commit comments

Comments
 (0)