@@ -23,7 +23,7 @@ public enum LootLockerHTTPMethod
23
23
CREATE = 5 ,
24
24
OPTIONS = 6 ,
25
25
PATCH = 7 ,
26
- UPLOAD = 8
26
+ UPLOAD = 8
27
27
}
28
28
29
29
/// <summary>
@@ -32,34 +32,66 @@ public enum LootLockerHTTPMethod
32
32
[ System . Serializable ]
33
33
public class LootLockerResponse
34
34
{
35
-
36
35
/// <summary>
37
36
/// TRUE if http error OR server returns an error status
38
37
/// </summary>
39
38
public bool hasError ;
39
+
40
40
/// <summary>
41
41
/// HTTP Status Code
42
42
/// </summary>
43
43
public int statusCode ;
44
+
44
45
/// <summary>
45
46
/// Raw text response from the server
46
47
/// <para>If hasError = true, this will contain the error message.</para>
47
48
/// </summary>
48
49
public string text ;
50
+
49
51
public bool success ;
50
52
51
53
52
54
public string Error ;
55
+
53
56
/// <summary>
54
57
/// A texture downloaded in the webrequest, if applicable, otherwise this will be null.
55
58
/// </summary>
56
59
public Texture2D texture ;
60
+
57
61
/// <summary>
58
62
/// inheritdoc added this because unity main thread excuting style cut the calling stack and make the event orphant seealso calling multiple events
59
63
/// of the same type makes use unable to identify each one
60
64
/// </summary>
61
65
public string EventId ;
62
66
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
+ }
63
95
}
64
96
65
97
/// <summary>
@@ -70,7 +102,7 @@ public class LootLockerResponseFactory
70
102
/// <summary>
71
103
/// Construct an error response to send to the client.
72
104
/// </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 ( )
74
106
{
75
107
return new T ( )
76
108
{
@@ -105,20 +137,24 @@ public struct LootLockerServerRequest
105
137
public string uploadType ;
106
138
public LootLocker . LootLockerEnums . LootLockerCallerRole adminCall ;
107
139
public WWWForm form ;
140
+
108
141
/// <summary>
109
142
/// Leave this null if you don't need custom headers
110
143
/// </summary>
111
144
public Dictionary < string , string > extraHeaders ;
145
+
112
146
/// <summary>
113
147
/// Query parameters to append to the end of the request URI
114
148
/// <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>
115
149
/// </summary>
116
150
public Dictionary < string , string > queryParams ;
151
+
117
152
public int retryCount ;
118
153
119
154
#region Make ServerRequest and call send (3 functions)
120
155
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 )
122
158
{
123
159
#if UNITY_EDITOR
124
160
LootLockerSDKManager . DebugMessage ( "Caller Type: " + callerRole . ToString ( ) ) ;
@@ -129,27 +165,25 @@ public static void CallAPI(string endPoint, LootLockerHTTPMethod httpMethod, str
129
165
if ( useAuthToken )
130
166
{
131
167
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 ) ;
133
170
}
134
171
135
172
if ( LootLockerConfig . current != null )
136
173
headers . Add ( LootLockerConfig . current . dateVersion . key , LootLockerConfig . current . dateVersion . value ) ;
137
174
138
175
LootLockerBaseServerAPI . I . SwitchURL ( callerRole ) ;
139
176
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 ) ; } ) ;
144
178
}
145
179
146
180
public static void CallDomainAuthAPI ( string endPoint , LootLockerHTTPMethod httpMethod , string body = null , Action < LootLockerResponse > onComplete = null )
147
181
{
148
182
if ( LootLockerConfig . current . domainKey . ToString ( ) . Length == 0 )
149
183
{
150
- #if UNITY_EDITOR
184
+ #if UNITY_EDITOR
151
185
LootLockerSDKManager . DebugMessage ( "LootLocker domain key must be set in settings" , true ) ;
152
- #endif
186
+ #endif
153
187
onComplete ? . Invoke ( LootLockerResponseFactory . Error < LootLockerResponse > ( "LootLocker domain key must be set in settings" ) ) ;
154
188
155
189
return ;
@@ -165,13 +199,11 @@ public static void CallDomainAuthAPI(string endPoint, LootLockerHTTPMethod httpM
165
199
166
200
LootLockerBaseServerAPI . I . SwitchURL ( LootLockerCallerRole . Base ) ;
167
201
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 ) ; } ) ;
172
203
}
173
204
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 )
175
207
{
176
208
Dictionary < string , string > headers = new Dictionary < string , string > ( ) ;
177
209
@@ -180,20 +212,19 @@ public static void UploadFile(string endPoint, LootLockerHTTPMethod httpMethod,
180
212
headers = new Dictionary < string , string > ( ) ;
181
213
182
214
headers . Add ( callerRole == LootLockerCallerRole . Admin ? "x-auth-token" : "x-session-token" , LootLockerConfig . current . token ) ;
183
-
184
215
}
185
216
186
217
LootLockerBaseServerAPI . I . SwitchURL ( callerRole ) ;
187
218
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 ) ; } ) ;
192
220
}
221
+
193
222
#endregion
194
223
195
224
#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 )
197
228
{
198
229
this . retryCount = 0 ;
199
230
this . endpoint = endpoint ;
@@ -223,7 +254,8 @@ public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod
223
254
}
224
255
}
225
256
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 )
227
259
{
228
260
this . retryCount = 0 ;
229
261
this . endpoint = endpoint ;
@@ -243,7 +275,9 @@ public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod
243
275
LootLockerSDKManager . DebugMessage ( "WARNING: Payloads should not be sent in GET, HEAD, OPTIONS, requests. Attempted to send a payload to: " + this . httpMethod . ToString ( ) + " " + this . endpoint ) ;
244
276
}
245
277
}
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 )
247
281
{
248
282
this . retryCount = 0 ;
249
283
this . endpoint = endpoint ;
@@ -263,17 +297,15 @@ public LootLockerServerRequest(string endpoint, LootLockerHTTPMethod httpMethod
263
297
LootLockerSDKManager . DebugMessage ( "WARNING: Payloads should not be sent in GET, HEAD, OPTIONS, requests. Attempted to send a payload to: " + this . httpMethod . ToString ( ) + " " + this . endpoint ) ;
264
298
}
265
299
}
300
+
266
301
#endregion
267
302
268
303
/// <summary>
269
304
/// just debug and call ServerAPI.SendRequest which takes the current ServerRequest and pass this response
270
305
/// </summary>
271
306
public void Send ( System . Action < LootLockerResponse > OnServerResponse )
272
307
{
273
- LootLockerBaseServerAPI . I . SendRequest ( this , ( response ) =>
274
- {
275
- OnServerResponse ? . Invoke ( response ) ;
276
- } ) ;
308
+ LootLockerBaseServerAPI . I . SendRequest ( this , ( response ) => { OnServerResponse ? . Invoke ( response ) ; } ) ;
277
309
}
278
310
}
279
- }
311
+ }
0 commit comments