7 using System.Collections.Generic;
11 using System.Security.Cryptography;
17 using System.Globalization;
63 public void PerformRequest(
string command, Dictionary<string, object> fields = null, RequestResponseDelegate onResponse = null,
string authToken =
"") {
66 WWWForm wwwForm =
new WWWForm();
67 Dictionary<string, string> headers =
new Dictionary<string, string>();
70 headers.Add(
"Accept",
"application/json");
72 if (authToken !=
"") {
74 headers.Add(
"Authorization",
"Token " + authToken);
78 foreach (KeyValuePair<string, object> pair
in fields) {
79 wwwForm.AddField(pair.Key, pair.Value.ToString());
81 request =
new WWW(url, wwwForm.data, headers);
83 request =
new WWW(url, null , headers);
86 System.Diagnostics.StackTrace stackTrace =
new System.Diagnostics.StackTrace();
87 string callee = stackTrace.GetFrame(1).GetMethod().Name;
96 public void PerformFormRequest(
string command, WWWForm wwwForm,
string filename,
string fileData, RequestResponseDelegate onResponse = null,
string authToken =
"") {
97 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
hostUrl + command);
99 request.Headers.Add(
"Authorization",
"Token " + authToken);
100 request.Method =
"POST";
101 request.Accept =
"application/json";
102 request.Headers[
"Accept-Encoding"] =
"identity";
105 string boundary = DateTime.Now.Ticks.ToString(
"x", NumberFormatInfo.InvariantInfo);
106 request.ContentType =
"multipart/form-data; boundary=" + boundary;
108 string postData =
"--" + boundary +
"\r\nContent-Type: text/plain\r\n";
109 postData +=
"Content-Disposition: form-data; name=\"filename\"; filename=\"" + filename +
"\"\r\n\r\n";
110 postData += fileData;
111 postData +=
"\r\n--" + boundary +
"--\r\n";
112 byte[] data = Encoding.UTF8.GetBytes(postData);
114 request.ContentLength = data.Length;
116 using (Stream stream = request.GetRequestStream())
118 stream.Write(data, 0, data.Length);
121 HttpWebResponse response;
123 response = (HttpWebResponse)request.GetResponse();
125 }
catch(WebException e) {
126 StartCoroutine(
HandleResponse((HttpWebResponse)e.Response, onResponse,
""));
130 IEnumerator
HandleResponse(HttpWebResponse response, RequestResponseDelegate onResponse,
string callee) {
131 HttpStatusCode statusCode = response.StatusCode;
132 Debug.Log(
"Http Status Code: " + response.StatusCode);
134 bool responseSuccessful = (statusCode >= HttpStatusCode.OK && statusCode <= HttpStatusCode.PartialContent);
135 Debug.Log(
"response successful: " + responseSuccessful);
139 Stream receiveStream = response.GetResponseStream ();
142 StreamReader readStream =
new StreamReader (receiveStream, Encoding.UTF8);
144 Debug.Log(
"Got stream");
145 Debug.Log(readStream.ReadToEnd());
148 responseObj =
JSON.
Parse(readStream.ReadToEnd());
149 }
catch (Exception ex) {
150 if (onResponse != null) {
151 if (!responseSuccessful) {
152 Debug.Log(
"Could not parse the response, request.text=" + readStream.ReadToEnd());
153 Debug.Log(
"Exception=" + ex.ToString());
156 if (readStream.ReadToEnd() ==
"") {
159 Debug.Log(
"Could not parse the response, request.text=" + readStream.ReadToEnd());
160 Debug.Log(
"Exception=" + ex.ToString());
168 if (!responseSuccessful) {
169 if (onResponse != null) {
170 onResponse(
ResponseType.ErrorFromServer, responseObj, callee);
176 if (onResponse != null) {
181 IEnumerator
HandleRequest(WWW request, RequestResponseDelegate onResponse,
string callee) {
184 if (request.isDone) {
187 yield
return new WaitForEndOfFrame();
191 if (!String.IsNullOrEmpty(request.error)) {
192 if (onResponse != null) {
197 int statusCode = 200;
199 if (request.responseHeaders.ContainsKey(
"REAL_STATUS")) {
200 string status = request.responseHeaders[
"REAL_STATUS"];
201 statusCode =
int.Parse(status.Split(
' ')[0]);
204 bool responseSuccessful = (statusCode >= 200 && statusCode <= 206);
209 }
catch (Exception ex) {
210 if (onResponse != null) {
211 if (!responseSuccessful) {
212 Debug.Log(
"Could not parse the response, request.text=" + request.text);
213 Debug.Log(
"Exception=" + ex.ToString());
216 if (request.text ==
"") {
219 Debug.Log(
"Could not parse the response, request.text=" + request.text);
220 Debug.Log(
"Exception=" + ex.ToString());
228 if (!responseSuccessful) {
229 if (onResponse != null) {
230 onResponse(
ResponseType.ErrorFromServer, responseObj, callee);
236 if (onResponse != null) {
void PerformRequest(string command, Dictionary< string, object > fields=null, RequestResponseDelegate onResponse=null, string authToken="")
Performs a request to the backend.
IEnumerator HandleResponse(HttpWebResponse response, RequestResponseDelegate onResponse, string callee)
static string getVersionString()
static JSONNode Parse(string aJSON)
void PerformFormRequest(string command, WWWForm wwwForm, string filename, string fileData, RequestResponseDelegate onResponse=null, string authToken="")
Performs a request to the backend.
delegate void RequestResponseDelegate(ResponseType responseType, JSONNode jsonResponse, string callee)
The response delegate
IEnumerator HandleRequest(WWW request, RequestResponseDelegate onResponse, string callee)