|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | | -using UnityEngine; |
4 | 3 | using UnityEngine.Networking; |
5 | 4 |
|
6 | 5 | namespace DUCK.Http |
7 | 6 | { |
8 | 7 | public class HttpRequest |
9 | 8 | { |
10 | | - public UnityWebRequest UnityWebRequest { get; private set; } |
| 9 | + internal UnityWebRequest UnityWebRequest { get { return unityWebRequest; } } |
11 | 10 |
|
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; |
14 | 22 |
|
15 | 23 | public HttpRequest(UnityWebRequest unityWebRequest) |
16 | 24 | { |
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 | + } |
18 | 35 |
|
19 | | - headers = Http.Instance.GetSuperHeaders(); |
| 36 | + return this; |
20 | 37 | } |
21 | 38 |
|
22 | | - public void SetHeader(string key, string value) |
| 39 | + public HttpRequest SetHeader(string key, string value) |
23 | 40 | { |
24 | 41 | headers[key] = value; |
| 42 | + return this; |
25 | 43 | } |
26 | 44 |
|
27 | | - public void SetHeaders(IEnumerable<KeyValuePair<string, string>> headers) |
| 45 | + public HttpRequest SetHeaders(IEnumerable<KeyValuePair<string, string>> headers) |
28 | 46 | { |
29 | | - if (headers == null) return; |
30 | | - |
31 | 47 | foreach (var kvp in headers) |
32 | 48 | { |
33 | 49 | SetHeader(kvp.Key, kvp.Value); |
34 | 50 | } |
| 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; |
35 | 83 | } |
36 | 84 |
|
37 | 85 | public bool RemoveHeader(string key) |
38 | 86 | { |
39 | 87 | return headers.Remove(key); |
40 | 88 | } |
41 | 89 |
|
42 | | - public void Send(Action<HttpResponse> onSuccess = null, Action<HttpResponse> onError = null) |
| 90 | + public HttpRequest Send() |
43 | 91 | { |
44 | 92 | foreach (var header in headers) |
45 | 93 | { |
46 | | - UnityWebRequest.SetRequestHeader(header.Key, header.Value); |
| 94 | + unityWebRequest.SetRequestHeader(header.Key, header.Value); |
47 | 95 | } |
48 | 96 |
|
49 | | - coroutine = Http.Instance.Send(this, onSuccess, onError); |
| 97 | + Http.Instance.Send(this, onSuccess, onError, onNetworkError); |
| 98 | + return this; |
50 | 99 | } |
51 | 100 |
|
52 | 101 | public void Abort() |
53 | 102 | { |
54 | | - if (UnityWebRequest != null && !UnityWebRequest.isDone) |
55 | | - { |
56 | | - UnityWebRequest.Abort(); |
57 | | - } |
| 103 | + Http.Instance.Abort(this); |
| 104 | + } |
58 | 105 |
|
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) |
60 | 115 | { |
61 | | - Http.Instance.Abort(coroutine); |
| 116 | + currentProgress = progress; |
| 117 | + if (onProgress != null) |
| 118 | + { |
| 119 | + onProgress.Invoke(downloadProgress); |
| 120 | + } |
62 | 121 | } |
63 | 122 | } |
64 | 123 | } |
|
0 commit comments