· 8 years ago · Jan 18, 2018, 09:40 AM
1internal static JObject GetToBackOffice(string action)
2{
3 var url = backOfficeUrl;
4 url = url + action;
5
6 HttpClient httpClient = new HttpClient();
7
8 var response =
9 httpClient.GetAsync(url
10 ).Result;
11 if (response.StatusCode == System.Net.HttpStatusCode.OK)
12 {
13 var idProcess = Newtonsoft.Json.Linq.JObject.Parse(response.Content.ReadAsStringAsync().Result);
14
15 return idProcess;
16 }
17 else
18 return null;
19
20internal class Utils
21{
22 internal static JObject GetToBackOffice(string action)
23 {
24 var url = backOfficeUrl;
25 url = url + action;
26
27 HttpClient httpClient = new HttpClient();
28
29 JObject idProcess = new JObject();
30
31 httpClient.GetString(new Uri(url),
32 (response) =>
33 {
34 // Raised when the download completes
35 if (response.StatusCode == System.Net.HttpStatusCode.OK)
36 {
37 idProcess = Newtonsoft.Json.Linq.JObject.Parse(response.Data);
38 }
39 else
40 {
41 idProcess = null;
42 }
43 });
44 // Here I would like to wait for the response, so that idProcess is filled with the received data before returning
45 return idProcess;
46 }
47
48public class Action
49{
50 public bool SendData(string id, string secretKey, FakeData data)
51 {
52 var idProcess = Utils.GetToBackOffice(String.Format("Events/{0}/infos",id));
53 //...I do then something with idProcess
54 //Currently, when I use idProcess here, it is still empty since the GetString response hasn't been received yet when this line is executed
55
56 return true;
57 }
58}
59
60public class EventHubSimulator : MonoBehaviour
61{
62 void Start()
63 {
64 //Fill the parameters (I skip the details)
65 string oId = ...;
66 string secretKey = ...;
67 var vh = ...;
68
69 Action action = new Action();
70 action.SendData(oId, secretKey, vh);
71 }
72}
73
74//Class scope variable is neede to hold Resopnce other wise it will
75 //be destroied as soon as function is ended
76 ResponceType response;
77 IEnumerator WaitForResponce(ResponceType response)
78 {
79 this.response = response;
80 while(this.response.Data == null)
81 yield return new WaitForSeconds (0.02f);
82 //do what you want here
83
84 }
85
86httpClient.GetString(new Uri(url),
87 (response) =>
88 {
89 // Raised when the download completes
90 if (response.StatusCode == System.Net.HttpStatusCode.OK)
91 {
92 //idProcess = Newtonsoft.Json.Linq.JObject.Parse(response.Data);
93 StartCoroutine(WaitForResponce(responce));
94 }
95 else
96 {
97 idProcess = null;
98 }
99 });
100
101internal static void GetToBackOffice(string action, Action<JObject>onCompletion)
102{
103 var url = backOfficeUrl;
104 url = url + action;
105
106 HttpClient httpClient = new HttpClient();
107
108 JObject idProcess = new JObject();
109
110 httpClient.GetString(new Uri(url),
111 (response) =>
112 {
113 // Raised when the download completes
114 if (response.StatusCode == System.Net.HttpStatusCode.OK)
115 {
116 idProcess = Newtonsoft.Json.Linq.JObject.Parse(response.Data);
117 }
118 else
119 {
120 idProcess = null;
121 }
122 if (onCompletion){ onCompletion(idProcess);
123 });
124}
125
126async Task CallerMethod()
127{
128 JObject result = await GetToBackOffice(...);
129 // Do something with result
130}
131
132internal static Task<JObject> GetToBackOffice(string action)
133{
134 var tsc = new TaskCompletionSource<JObject>();
135 var url = backOfficeUrl;
136 url = url + action;
137
138 HttpClient httpClient = new HttpClient();
139
140 JObject idProcess = new JObject();
141
142 httpClient.GetString(new Uri(url),
143 (response) =>
144 {
145 // Raised when the download completes
146 if (response.StatusCode == System.Net.HttpStatusCode.OK)
147 {
148 tsc.SetResult(Newtonsoft.Json.Linq.JObject.Parse(response.Data));
149 }
150 else
151 {
152 tsc.SetResult(null);
153 }
154 });
155
156 return tsc.Task;
157}