Skip to content

Commit 9454a58

Browse files
committed
Merge branch 'development'
2 parents 374d33d + e0ac952 commit 9454a58

File tree

9 files changed

+359
-348
lines changed

9 files changed

+359
-348
lines changed

Http.cs

Lines changed: 103 additions & 192 deletions
Large diffs are not rendered by default.

HttpHelpers.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

HttpHelpers.cs.meta

Lines changed: 0 additions & 13 deletions
This file was deleted.

HttpRequest.cs

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,123 @@
11
using System;
22
using System.Collections.Generic;
3-
using UnityEngine;
43
using UnityEngine.Networking;
54

65
namespace DUCK.Http
76
{
87
public class HttpRequest
98
{
10-
public UnityWebRequest UnityWebRequest { get; private set; }
9+
internal UnityWebRequest UnityWebRequest { get { return unityWebRequest; } }
1110

12-
private readonly Dictionary<string, string> headers;
13-
private Coroutine coroutine;
11+
private readonly UnityWebRequest unityWebRequest;
12+
private Dictionary<string, string> headers;
13+
14+
private event Action<float> onUploadProgress;
15+
private event Action<float> onDownloadProgress;
16+
private event Action<HttpResponse> onSuccess;
17+
private event Action<HttpResponse> onError;
18+
private event Action<HttpResponse> onNetworkError;
19+
20+
private float downloadProgress;
21+
private float uploadProgress;
1422

1523
public HttpRequest(UnityWebRequest unityWebRequest)
1624
{
17-
UnityWebRequest = unityWebRequest;
25+
this.unityWebRequest = unityWebRequest;
26+
headers = new Dictionary<string, string>(Http.GetSuperHeaders());
27+
}
28+
29+
public HttpRequest RemoveSuperHeaders()
30+
{
31+
foreach (var kvp in Http.GetSuperHeaders())
32+
{
33+
headers.Remove(kvp.Key);
34+
}
1835

19-
headers = Http.Instance.GetSuperHeaders();
36+
return this;
2037
}
2138

22-
public void SetHeader(string key, string value)
39+
public HttpRequest SetHeader(string key, string value)
2340
{
2441
headers[key] = value;
42+
return this;
2543
}
2644

27-
public void SetHeaders(IEnumerable<KeyValuePair<string, string>> headers)
45+
public HttpRequest SetHeaders(IEnumerable<KeyValuePair<string, string>> headers)
2846
{
29-
if (headers == null) return;
30-
3147
foreach (var kvp in headers)
3248
{
3349
SetHeader(kvp.Key, kvp.Value);
3450
}
51+
52+
return this;
53+
}
54+
55+
public HttpRequest OnUploadProgress(Action<float> onProgress)
56+
{
57+
onUploadProgress += onProgress;
58+
return this;
59+
}
60+
61+
public HttpRequest OnDownloadProgress(Action<float> onProgress)
62+
{
63+
onDownloadProgress += onProgress;
64+
return this;
65+
}
66+
67+
public HttpRequest OnSuccess(Action<HttpResponse> onSuccess)
68+
{
69+
this.onSuccess += onSuccess;
70+
return this;
71+
}
72+
73+
public HttpRequest OnError(Action<HttpResponse> onError)
74+
{
75+
this.onError += onError;
76+
return this;
77+
}
78+
79+
public HttpRequest OnNetworkError(Action<HttpResponse> onNetworkError)
80+
{
81+
this.onNetworkError += onNetworkError;
82+
return this;
3583
}
3684

3785
public bool RemoveHeader(string key)
3886
{
3987
return headers.Remove(key);
4088
}
4189

42-
public void Send(Action<HttpResponse> onSuccess = null, Action<HttpResponse> onError = null)
90+
public HttpRequest Send()
4391
{
4492
foreach (var header in headers)
4593
{
46-
UnityWebRequest.SetRequestHeader(header.Key, header.Value);
94+
unityWebRequest.SetRequestHeader(header.Key, header.Value);
4795
}
4896

49-
coroutine = Http.Instance.Send(this, onSuccess, onError);
97+
Http.Instance.Send(this, onSuccess, onError, onNetworkError);
98+
return this;
5099
}
51100

52101
public void Abort()
53102
{
54-
if (UnityWebRequest != null && !UnityWebRequest.isDone)
55-
{
56-
UnityWebRequest.Abort();
57-
}
103+
Http.Instance.Abort(this);
104+
}
58105

59-
if (coroutine != null)
106+
internal void UpdateProgress()
107+
{
108+
UpdateProgress(ref downloadProgress, unityWebRequest.downloadProgress, onDownloadProgress);
109+
UpdateProgress(ref uploadProgress, unityWebRequest.uploadProgress, onUploadProgress);
110+
}
111+
112+
private void UpdateProgress(ref float currentProgress, float progress, Action<float> onProgress)
113+
{
114+
if (currentProgress < progress)
60115
{
61-
Http.Instance.Abort(coroutine);
116+
currentProgress = progress;
117+
if (onProgress != null)
118+
{
119+
onProgress.Invoke(downloadProgress);
120+
}
62121
}
63122
}
64123
}

HttpResponse.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using UnityEngine;
43
using UnityEngine.Networking;
54

@@ -11,7 +10,7 @@ public class HttpResponse
1110
public bool IsSuccessful { get; private set; }
1211
public bool IsHttpError { get; private set; }
1312
public bool IsNetworkError { get; private set; }
14-
public long ResponseCode { get; private set; }
13+
public long StatusCode { get; private set; }
1514
public ResponseType ResponseType { get; private set; }
1615
public byte[] Bytes { get; private set; }
1716
public string Text { get; private set; }
@@ -26,8 +25,8 @@ public HttpResponse(UnityWebRequest unityWebRequest)
2625
IsSuccessful = !unityWebRequest.isHttpError && !unityWebRequest.isNetworkError;
2726
IsHttpError = unityWebRequest.isHttpError;
2827
IsNetworkError = unityWebRequest.isNetworkError;
29-
ResponseCode = unityWebRequest.responseCode;
30-
ResponseType = HttpUtils.GetResponseType(ResponseCode);
28+
StatusCode = unityWebRequest.responseCode;
29+
ResponseType = HttpUtils.GetResponseType(StatusCode);
3130
ResponseHeaders = unityWebRequest.GetResponseHeaders();
3231

3332
var downloadHandlerTexture = unityWebRequest.downloadHandler as DownloadHandlerTexture;
@@ -36,29 +35,5 @@ public HttpResponse(UnityWebRequest unityWebRequest)
3635
Texture = downloadHandlerTexture.texture;
3736
}
3837
}
39-
40-
public T ParseBodyAs<T>()
41-
{
42-
try
43-
{
44-
return JsonUtility.FromJson<T>(Text);
45-
}
46-
catch
47-
{
48-
return default(T);
49-
}
50-
}
51-
52-
public object ParseBodyAs(Type type)
53-
{
54-
try
55-
{
56-
return JsonUtility.FromJson(Text, type);
57-
}
58-
catch
59-
{
60-
return default(Type);
61-
}
62-
}
6338
}
64-
}
39+
}

HttpUtils.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Text;
4-
using UnityEngine;
54

65
namespace DUCK.Http
76
{
@@ -70,7 +69,7 @@ public static bool RemoveCustomResponseMessage(long responseCode)
7069

7170
public static string GetResponseTypeMessage(HttpResponse response)
7271
{
73-
return GetResponseTypeMessage(response.ResponseCode, response.Url);
72+
return GetResponseTypeMessage(response.StatusCode, response.Url);
7473
}
7574

7675
public static string GetResponseTypeMessage(long responseCode, string url)

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017-2018 Dubit Ltd.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LICENSE.md.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)