forked from Feynmen/UnityHttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpCommunication.cs
145 lines (137 loc) · 5.44 KB
/
HttpCommunication.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections;
using UnityEngine;
using UnityHTTP.Request;
using UnityHTTP.Response;
namespace UnityHTTP
{
public sealed class HttpCommunication : MonoBehaviour
{
private static HttpCommunication _instance;
public static HttpCommunication Instance
{
get
{
if (_instance == null)
{
_instance = new GameObject("HttpCommunication").AddComponent<HttpCommunication>();
}
return _instance;
}
}
private string _serverUrl;
private int _requestTimeout;
private HttpCommunication() { }
/// <summary>
/// Init HttpCommunication with server root URL and request timeout
/// </summary>
/// <param name="serverRootUrl">Server root URl. For example: "https://github.com/"</param>
/// <param name="requestTimeout">Request timeout</param>
public void Init(string serverRootUrl, int requestTimeout)
{
_serverUrl = serverRootUrl;
_requestTimeout = requestTimeout;
}
/// <summary>
/// Init HttpCommunication with server root URL
/// </summary>
/// <param name="serverRootUrl">Server root URl. For example: "https://github.com/"</param>
public void Init(string serverRootUrl)
{
_serverUrl = serverRootUrl;
_requestTimeout = 50;
}
/// <summary>
/// Init HttpCommunication. You mast insert full server URl in your requests
/// </summary>
public void Init()
{
_serverUrl = string.Empty;
_requestTimeout = 50;
}
/// <summary>
/// Send a request to a server that has been extended from the RequestBase class
/// </summary>
/// <typeparam name="TRequest">Your request type that has been extended from the RequestBase class</typeparam>
/// <typeparam name="TResponse">SimpleResponse type or RequestResponse type that has been extended from the SimpleResponse class</typeparam>
/// <param name="requestData">Data container type that has been extended from the RequestDataBase class</param>
/// <param name="resultCallback">Callback which signals the completion of communication with the server</param>
public void Send<TRequest, TResponse>(RequestDataBase requestData, Action<TResponse> resultCallback) where TRequest : RequestBase<TRequest>, new() where TResponse : SimpleResponse, new()
{
Debug.Log("Send - " + typeof(TRequest));
Debug.Log(requestData);
var request = RequestBase<TRequest>.Create(_serverUrl, requestData);
StartCoroutine(SendRequestWithTimer(request, compliteCallback: () =>
{
request.OnSendCompleted();
if (resultCallback != null)
{
resultCallback.Invoke(ParseResponse<TResponse, TRequest>(request));
}
}, timeoutCallback: () =>
{
if (resultCallback != null)
{
resultCallback.Invoke(new TResponse {error = "Timeout", errorCode = 408});
}
}));
}
private IEnumerator SendRequestWithTimer<T>(RequestBase<T> request, Action compliteCallback, Action timeoutCallback)
where T : RequestBase<T>, new()
{
Cookies.Instance.TrySetCookieInRequest(request.UnityWebRequest);
var asyncOperation = request.Send();
float timer = _requestTimeout;
while (!asyncOperation.isDone)
{
if (timer <= 0)
{
request.Abort();
timeoutCallback.Invoke();
yield break;
}
timer -= Time.fixedDeltaTime;
yield return null;
}
compliteCallback.Invoke();
}
private static TResponse ParseResponse<TResponse, TRequest>(RequestBase<TRequest> request)
where TResponse : SimpleResponse, new() where TRequest : RequestBase<TRequest>, new()
{
var json = "{}";
if (request.UnityWebRequest.downloadHandler != null)
{
json = request.UnityWebRequest.downloadHandler.text;
}
if (request.UnityWebRequest.responseCode < 300 && request.UnityWebRequest.responseCode >= 200)
{
try
{
if (json[0] == '[')
{
json = "{\"data\":" + json + "}";
}
return JsonUtility.FromJson<TResponse>(json);
}
catch (Exception e)
{
return new TResponse()
{
error = "Exception! " + e.Message + "\nJSON: " + json
};
}
}
if (request.UnityWebRequest.responseCode == 400)
{
var respounse = JsonUtility.FromJson<TResponse>(json);
respounse.errorCode = request.UnityWebRequest.responseCode;
return respounse;
}
return new TResponse()
{
errorCode = request.UnityWebRequest.responseCode,
error = request.UnityWebRequest.error
};
}
}
}