/
PomisedRequest.cs
87 lines (62 loc) · 1.61 KB
/
PomisedRequest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using RSG;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
namespace PromisedRequest{
public class HTTPRequest {
private string url;
private Dictionary<string, string> headers;
public HTTPRequest(string url, Dictionary<string, string> headers){
this.url = url;
this.headers = headers;
}
public IPromise<string> GetJsonString(){
var promise = new Promise<string> ();
using (var client = new WebClient())
{
if (this.headers != null) {
foreach(KeyValuePair<string, string> h in this.headers){
client.Headers.Add(h.Key, h.Value);
}
}
client.DownloadStringCompleted +=
(s, ev) =>
{
if (ev.Error != null){
promise.Reject(ev.Error);
} else {
promise.Resolve(ev.Result);
}
};
client.DownloadStringAsync(new Uri(this.url), null);
}
return promise;
}
public IPromise<string> GET(Dictionary<string, string> parameters){
if (parameters == null) {
throw new ArgumentNullException ();
}
var promise = new Promise<string> ();
using(var client = new WebClient()){
if (this.headers != null) {
foreach(KeyValuePair<string, string> h in this.headers){
client.Headers.Add(h.Key, h.Value);
}
}
client.DownloadStringCompleted +=
(s, ev) =>
{
if (ev.Error != null){
promise.Reject(ev.Error);
} else {
promise.Resolve(ev.Result);
}
};
client.DownloadStringAsync(new Uri(this.url + "?" + PromisedRequest.Helpers.GenerateQueryString(parameters)), null);
}
return promise;
}
}
}