Skip to content

Commit b70aaf6

Browse files
authored
Merge pull request #252 from easy-games/feature/ratelimit-retry-logic
Expose headers via HttpResponse
2 parents b6edb5d + 4126f00 commit b70aaf6

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

Runtime/Code/Http/HttpResponse.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
using System.Collections.Generic;
2+
13
namespace Code.Http {
24
public struct HttpResponse {
35
public bool success;
4-
public int statusCode;
6+
// workable data
57
public string data;
68
public string error;
9+
// meta data
10+
public int statusCode;
11+
public Dictionary<string, string> headers;
12+
13+
public string GetHeader(string headerName) {
14+
return headers[headerName];
15+
}
716
}
817
}

Runtime/Code/Http/Public/HttpManager.cs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,25 @@ public static async Task<HttpResponse> GetAsync(string url, string headers) {
3838
return new HttpResponse() {
3939
success = false,
4040
error = req.error,
41-
statusCode = (int)req.responseCode
41+
statusCode = (int)req.responseCode,
42+
headers = req.GetResponseHeaders()
43+
};
44+
}
45+
46+
if (req.result == UnityWebRequest.Result.ConnectionError) {
47+
return new HttpResponse() {
48+
success = false,
49+
error = req.error,
50+
statusCode = (int)req.responseCode,
51+
headers = req.GetResponseHeaders()
4252
};
4353
}
4454

4555
return new HttpResponse() {
4656
success = true,
4757
data = req.downloadHandler.text,
48-
statusCode = (int)req.responseCode
58+
statusCode = (int)req.responseCode,
59+
headers = req.GetResponseHeaders()
4960
};
5061

5162

@@ -83,11 +94,13 @@ public static Task<HttpResponse> PostAsync(string url, string data, string heade
8394
if (string.IsNullOrEmpty(data)) {
8495
options = new RequestHelper {
8596
Uri = url,
97+
IgnoreHttpException = true
8698
};
8799
} else {
88100
options = new RequestHelper {
89101
Uri = url,
90-
BodyString = data
102+
BodyString = data,
103+
IgnoreHttpException = true
91104
};
92105
}
93106
if (headers != "") {
@@ -102,9 +115,10 @@ public static Task<HttpResponse> PostAsync(string url, string data, string heade
102115

103116
RestClient.Post(UnityWebRequestProxyHelper.ApplyProxySettings(options)).Then((res) => {
104117
task.SetResult(new HttpResponse() {
105-
success = true,
118+
success = 200 <= res.StatusCode && res.StatusCode < 300,
106119
data = res.Text,
107-
statusCode = (int)res.StatusCode
120+
statusCode = (int)res.StatusCode,
121+
headers = res.Headers
108122
});
109123
}).Catch((err) => {
110124
var error = err as RequestException;
@@ -113,8 +127,9 @@ public static Task<HttpResponse> PostAsync(string url, string data, string heade
113127
}
114128
task.SetResult(new HttpResponse() {
115129
success = false,
116-
statusCode = (int) error.StatusCode,
117-
error = error.Response
130+
statusCode = 0,
131+
error = error.Message,
132+
headers = {}
118133
});
119134
});
120135

@@ -130,6 +145,7 @@ public static Task<HttpResponse> DeleteAsync(string url, string headers) {
130145

131146
var options = new RequestHelper {
132147
Uri = url,
148+
IgnoreHttpException = true
133149
};
134150
if (headers != "") {
135151
var split = headers.Split(",");
@@ -143,9 +159,10 @@ public static Task<HttpResponse> DeleteAsync(string url, string headers) {
143159

144160
RestClient.Delete(UnityWebRequestProxyHelper.ApplyProxySettings(options)).Then((res) => {
145161
task.SetResult(new HttpResponse() {
146-
success = true,
162+
success = 200 <= res.StatusCode && res.StatusCode < 300,
147163
data = res.Text,
148-
statusCode = (int)res.StatusCode
164+
statusCode = (int)res.StatusCode,
165+
headers = res.Headers
149166
});
150167
}).Catch((err) => {
151168
var error = err as RequestException;
@@ -154,8 +171,9 @@ public static Task<HttpResponse> DeleteAsync(string url, string headers) {
154171
}
155172
task.SetResult(new HttpResponse() {
156173
success = false,
157-
statusCode = (int) error.StatusCode,
158-
error = error.Response
174+
statusCode = 0,
175+
error = error.Message,
176+
headers = {}
159177
});
160178
});
161179

@@ -171,7 +189,8 @@ public static Task<HttpResponse> PatchAsync(string url, string data, string head
171189

172190
var options = new RequestHelper {
173191
Uri = url,
174-
BodyString = data
192+
BodyString = data,
193+
IgnoreHttpException = true
175194
};
176195
if (headers != "") {
177196
var split = headers.Split(",");
@@ -185,9 +204,10 @@ public static Task<HttpResponse> PatchAsync(string url, string data, string head
185204

186205
RestClient.Patch(UnityWebRequestProxyHelper.ApplyProxySettings(options)).Then((res) => {
187206
task.SetResult(new HttpResponse() {
188-
success = true,
207+
success = 200 <= res.StatusCode && res.StatusCode < 300,
189208
data = res.Text,
190-
statusCode = (int)res.StatusCode
209+
statusCode = (int)res.StatusCode,
210+
headers = res.Headers
191211
});
192212
}).Catch((err) => {
193213
var error = err as RequestException;
@@ -196,16 +216,17 @@ public static Task<HttpResponse> PatchAsync(string url, string data, string head
196216
}
197217
task.SetResult(new HttpResponse() {
198218
success = false,
199-
statusCode = (int) error.StatusCode,
200-
error = error.Response
219+
statusCode = 0,
220+
error = error.Message,
221+
headers = {}
201222
});
202223
});
203224

204225
return task.Task;
205226
}
206227

207228
private static void LogRequestError(string url, RequestException error) {
208-
Debug.LogError(url + " " + error + " " + error.Response);
229+
Debug.LogError(url + " " + error + " " + error.Message);
209230
}
210231

211232
public static Task<HttpResponse> PutAsync(string url, string data) {
@@ -216,6 +237,7 @@ public static Task<HttpResponse> PutAsync(string url, string data, string header
216237
return PutAsync(new RequestHelper {
217238
Uri = url,
218239
BodyString = data,
240+
IgnoreHttpException = true
219241
}, headers);
220242
}
221243

@@ -234,9 +256,10 @@ public static Task<HttpResponse> PutAsync(RequestHelper options, string headers)
234256

235257
RestClient.Put(UnityWebRequestProxyHelper.ApplyProxySettings(options)).Then((res) => {
236258
task.SetResult(new HttpResponse() {
237-
success = true,
259+
success = 200 <= res.StatusCode && res.StatusCode < 300,
238260
data = res.Text,
239-
statusCode = (int)res.StatusCode
261+
statusCode = (int)res.StatusCode,
262+
headers = res.Headers
240263
});
241264
}).Catch((err) => {
242265
var error = err as RequestException;
@@ -245,8 +268,9 @@ public static Task<HttpResponse> PutAsync(RequestHelper options, string headers)
245268
}
246269
task.SetResult(new HttpResponse() {
247270
success = false,
248-
statusCode = (int) error.StatusCode,
249-
error = error.Response
271+
statusCode = 0,
272+
error = error.Message,
273+
headers = {}
250274
});
251275
});
252276

0 commit comments

Comments
 (0)