11using  System ; 
22using  System . Collections ; 
33using  System . Collections . Generic ; 
4- using  System . Text ; 
4+ using  Duck . Http . Service ; 
5+ using  Duck . Http . Service . Unity ; 
56using  UnityEngine ; 
67using  UnityEngine . Networking ; 
78
@@ -14,19 +15,30 @@ public static Http Instance
1415			get 
1516			{ 
1617				if  ( instance  !=  null )  return  instance ; 
17- 				instance  =  new  GameObject ( typeof ( Http ) . Name ) . AddComponent < Http > ( ) ; 
18- 				instance . gameObject . hideFlags  =  HideFlags . HideInHierarchy ; 
19- 				instance . superHeaders  =  new  Dictionary < string ,  string > ( ) ; 
20- 				instance . httpRequests  =  new  Dictionary < HttpRequest ,  Coroutine > ( ) ; 
21- 				DontDestroyOnLoad ( instance . gameObject ) ; 
18+ 				Init ( new  UnityHttpService ( ) ) ; 
2219				return  instance ; 
2320			} 
2421		} 
2522
2623		private  static Http  instance ; 
2724
25+ 		private  IHttpService  service ; 
2826		private  Dictionary < string ,  string >  superHeaders ; 
29- 		private  Dictionary < HttpRequest ,  Coroutine >  httpRequests ; 
27+ 		private  Dictionary < IHttpRequest ,  Coroutine >  httpRequests ; 
28+ 
29+ 		public  static void  Init ( IHttpService  service ) 
30+ 		{ 
31+ 			if  ( instance )  return ; 
32+ 
33+ 			instance  =  new  GameObject ( typeof ( Http ) . Name ) . AddComponent < Http > ( ) ; 
34+ 			instance . gameObject . hideFlags  =  HideFlags . HideInHierarchy ; 
35+ 			instance . superHeaders  =  new  Dictionary < string ,  string > ( ) ; 
36+ 			instance . httpRequests  =  new  Dictionary < IHttpRequest ,  Coroutine > ( ) ; 
37+ 			instance . service  =  service ; 
38+ 			DontDestroyOnLoad ( instance . gameObject ) ; 
39+ 		} 
40+ 
41+ 		#region Super Headers
3042
3143		/// <summary> 
3244		/// Super headers are key value pairs that will be added to every subsequent HttpRequest. 
@@ -72,168 +84,108 @@ public static bool RemoveSuperHeader(string key)
7284			return  Instance . superHeaders . Remove ( key ) ; 
7385		} 
7486
75- 		/// <summary> 
76- 		 /// Creates a HttpRequest configured for HTTP GET. 
77- 		/// </summary> 
78- 		 /// <param name="uri">The URI of the resource to retrieve via HTTP GET.</param> 
79- 		/// <returns>A HttpRequest object configured to retrieve data from uri.</returns > 
80- 		public  static HttpRequest  Get ( string  uri ) 
87+ 		#endregion 
88+ 
89+ 		#region Static Requests 
90+ 
91+ 		/// <see cref="Duck.Http.Service.IHttpService.Get"/ > 
92+ 		public  static IHttpRequest  Get ( string  uri ) 
8193		{ 
82- 			return  new   HttpRequest ( UnityWebRequest . Get ( uri ) ) ; 
94+ 			return  Instance . service . Get ( uri ) ; 
8395		} 
8496
85- 		/// <summary> 
86- 		/// Creates a HttpRequest configured for HTTP GET. 
87- 		/// </summary> 
88- 		/// <param name="uri">The URI of the resource to retrieve via HTTP GET.</param> 
89- 		/// <returns>A HttpRequest object configured to retrieve data from uri.</returns> 
90- 		public  static HttpRequest  GetTexture ( string  uri ) 
97+ 		/// <see cref="Duck.Http.Service.IHttpService.GetTexture"/> 
98+ 		public  static IHttpRequest  GetTexture ( string  uri ) 
9199		{ 
92- 			return  new   HttpRequest ( UnityWebRequestTexture . GetTexture ( uri ) ) ; 
100+ 			return  Instance . service . GetTexture ( uri ) ; 
93101		} 
94102
95- 		/// <summary> 
96- 		/// Creates a HttpRequest configured to send form data to a server via HTTP POST. 
97- 		/// </summary> 
98- 		/// <param name="uri">The target URI to which form data will be transmitted.</param> 
99- 		/// <param name="postData">Form body data. Will be URLEncoded via WWWTranscoder.URLEncode prior to transmission.</param> 
100- 		/// <returns>A HttpRequest configured to send form data to uri via POST.</returns> 
101- 		public  static HttpRequest  Post ( string  uri ,  string  postData ) 
103+ 		/// <see cref="Duck.Http.Service.IHttpService.Post(string, string)"/> 
104+ 		public  static IHttpRequest  Post ( string  uri ,  string  postData ) 
102105		{ 
103- 			return  new   HttpRequest ( UnityWebRequest . Post ( uri ,  postData ) ) ; 
106+ 			return  Instance . service . Post ( uri ,  postData ) ; 
104107		} 
105108
106- 		/// <summary> 
107- 		/// Creates a HttpRequest configured to send form data to a server via HTTP POST. 
108- 		/// </summary> 
109- 		/// <param name="uri">The target URI to which form data will be transmitted.</param> 
110- 		/// <param name="formData">Form fields or files encapsulated in a WWWForm object, for formatting and transmission to the remote server.</param> 
111- 		/// <returns> A HttpRequest configured to send form data to uri via POST. </returns> 
112- 		public  static HttpRequest  Post ( string  uri ,  WWWForm  formData ) 
109+ 		/// <see cref="Duck.Http.Service.IHttpService.Post(string, WWWForm)"/> 
110+ 		public  static IHttpRequest  Post ( string  uri ,  WWWForm  formData ) 
113111		{ 
114- 			return  new   HttpRequest ( UnityWebRequest . Post ( uri ,  formData ) ) ; 
112+ 			return  Instance . service . Post ( uri ,  formData ) ; 
115113		} 
116114
117- 		/// <summary> 
118- 		/// Creates a HttpRequest configured to send form data to a server via HTTP POST. 
119- 		/// </summary> 
120- 		/// <param name="uri">The target URI to which form data will be transmitted.</param> 
121- 		/// <param name="formData">Form fields in the form of a Key Value Pair, for formatting and transmission to the remote server.</param> 
122- 		/// <returns>A HttpRequest configured to send form data to uri via POST.</returns> 
123- 		public  static HttpRequest  Post ( string  uri ,  Dictionary < string ,  string >  formData ) 
115+ 		/// <see cref="Duck.Http.Service.IHttpService.Post(string, Dictionary<string, string>)"/> 
116+ 		public  static IHttpRequest  Post ( string  uri ,  Dictionary < string ,  string >  formData ) 
124117		{ 
125- 			return  new   HttpRequest ( UnityWebRequest . Post ( uri ,  formData ) ) ; 
118+ 			return  Instance . service . Post ( uri ,  formData ) ; 
126119		} 
127120
128- 		/// <summary> 
129- 		/// Creates a HttpRequest configured to send form multipart form to a server via HTTP POST. 
130- 		/// </summary> 
131- 		/// <param name="uri">The target URI to which form data will be transmitted.</param> 
132- 		/// <param name="multipartForm">MultipartForm data for formatting and transmission to the remote server.</param> 
133- 		/// <returns>A HttpRequest configured to send form data to uri via POST.</returns> 
134- 		public  static HttpRequest  Post ( string  uri ,  List < IMultipartFormSection >  multipartForm ) 
121+ 		/// <see cref="Duck.Http.Service.IHttpService.Post(string, List<IMultipartFormSection>)"/> 
122+ 		public  static IHttpRequest  Post ( string  uri ,  List < IMultipartFormSection >  multipartForm ) 
135123		{ 
136- 			return  new   HttpRequest ( UnityWebRequest . Post ( uri ,  multipartForm ) ) ; 
124+ 			return  Instance . service . Post ( uri ,  multipartForm ) ; 
137125		} 
138126
139- 		/// <summary> 
140- 		/// Creates a HttpRequest configured to send raw bytes to a server via HTTP POST. 
141- 		/// </summary> 
142- 		/// <param name="uri">The target URI to which bytes will be transmitted.</param> 
143- 		/// <param name="bytes">Byte array data.</param> 
144- 		/// <param name="contentType">String representing the MIME type of the data (e.g. image/jpeg).</param> 
145- 		/// <returns>A HttpRequest configured to send raw bytes to a server via POST.</returns> 
146- 		public  static HttpRequest  Post ( string  uri ,  byte [ ]  bytes ,  string  contentType ) 
127+ 		/// <see cref="Duck.Http.Service.IHttpService.Post(string, byte[], string)"/> 
128+ 		public  static IHttpRequest  Post ( string  uri ,  byte [ ]  bytes ,  string  contentType ) 
147129		{ 
148- 			var  unityWebRequest  =  new  UnityWebRequest ( uri ,  UnityWebRequest . kHttpVerbPOST ) 
149- 			{ 
150- 				uploadHandler  =  new  UploadHandlerRaw ( bytes ) 
151- 				{ 
152- 					contentType  =  contentType 
153- 				} , 
154- 				downloadHandler  =  new  DownloadHandlerBuffer ( ) 
155- 			} ; 
156- 			return  new  HttpRequest ( unityWebRequest ) ; 
130+ 			return  Instance . service . Post ( uri ,  bytes ,  contentType ) ; 
157131		} 
158132
159- 		/// <summary> 
160- 		/// Creates a HttpRequest configured to send json data to a server via HTTP POST. 
161- 		/// </summary> 
162- 		/// <param name="uri">The target URI to which json data will be transmitted.</param> 
163- 		/// <param name="json">Json body data.</param> 
164- 		/// <returns>A HttpRequest configured to send json data to uri via POST.</returns> 
165- 		public  static HttpRequest  PostJson ( string  uri ,  string  json ) 
133+ 		/// <see cref="Duck.Http.Service.IHttpService.PostJson"/> 
134+ 		public  static IHttpRequest  PostJson ( string  uri ,  string  json ) 
166135		{ 
167- 			return  Post ( uri ,   Encoding . UTF8 . GetBytes ( json ) ,   "application/ json" ) ; 
136+ 			return  Instance . service . PostJson ( uri ,   json ) ; 
168137		} 
169138
170- 		/// <summary> 
171- 		/// Creates a HttpRequest configured to send json data to a server via HTTP POST. 
172- 		/// </summary> 
173- 		/// <param name="uri">The target URI to which json data will be transmitted.</param> 
174- 		/// <param name="payload">The object to be parsed to json data.</param> 
175- 		/// <returns>A HttpRequest configured to send json data to uri via POST.</returns> 
176- 		public  static HttpRequest  PostJson < T > ( string  uri ,  T  payload )  where  T  :  class 
139+ 		/// <see cref="Duck.Http.Service.IHttpService.PostJson{T}(string, T)"/> 
140+ 		public  static IHttpRequest  PostJson < T > ( string  uri ,  T  payload )  where  T  :  class 
177141		{ 
178- 			return  PostJson ( uri ,  JsonUtility . ToJson ( payload ) ) ; 
142+ 			return  Instance . service . PostJson ( uri ,  payload ) ; 
179143		} 
180144
181- 		/// <summary> 
182- 		/// Creates a HttpRequest configured to upload raw data to a remote server via HTTP PUT. 
183- 		/// </summary> 
184- 		/// <param name="uri">The URI to which the data will be sent.</param> 
185- 		/// <param name="bodyData">The data to transmit to the remote server.</param> 
186- 		/// <returns>A HttpRequest configured to transmit bodyData to uri via HTTP PUT.</returns> 
187- 		public  static HttpRequest  Put ( string  uri ,  byte [ ]  bodyData ) 
145+ 		/// <see cref="Duck.Http.Service.IHttpService.Put(string, byte[])"/> 
146+ 		public  static IHttpRequest  Put ( string  uri ,  byte [ ]  bodyData ) 
188147		{ 
189- 			return  new   HttpRequest ( UnityWebRequest . Put ( uri ,  bodyData ) ) ; 
148+ 			return  Instance . service . Put ( uri ,  bodyData ) ; 
190149		} 
191150
192- 		/// <summary> 
193- 		/// Creates a HttpRequest configured to upload raw data to a remote server via HTTP PUT. 
194- 		/// </summary> 
195- 		/// <param name="uri">The URI to which the data will be sent.</param> 
196- 		/// <param name="bodyData">The data to transmit to the remote server. 
197- 		/// The string will be converted to raw bytes via <a href="http:msdn.microsoft.comen-uslibrarysystem.text.encoding.utf8">System.Text.Encoding.UTF8<a>.</param> 
198- 		/// <returns>A HttpRequest configured to transmit bodyData to uri via HTTP PUT.</returns> 
199- 		public  static HttpRequest  Put ( string  uri ,  string  bodyData ) 
151+ 		/// <see cref="Duck.Http.Service.IHttpService.Put(string, string)"/> 
152+ 		public  static IHttpRequest  Put ( string  uri ,  string  bodyData ) 
200153		{ 
201- 			return  new   HttpRequest ( UnityWebRequest . Put ( uri ,  bodyData ) ) ; 
154+ 			return  Instance . service . Put ( uri ,  bodyData ) ; 
202155		} 
203156
204- 		/// <summary> 
205- 		/// Creates a HttpRequest configured for HTTP DELETE. 
206- 		/// </summary> 
207- 		/// <param name="uri">The URI to which a DELETE request should be sent.</param> 
208- 		/// <returns>A HttpRequest configured to send an HTTP DELETE request.</returns> 
209- 		public  static HttpRequest  Delete ( string  uri ) 
157+ 		/// <see cref="Duck.Http.Service.IHttpService.Delete"/> 
158+ 		public  static IHttpRequest  Delete ( string  uri ) 
210159		{ 
211- 			return  new   HttpRequest ( UnityWebRequest . Delete ( uri ) ) ; 
160+ 			return  Instance . service . Delete ( uri ) ; 
212161		} 
213162
214- 		/// <summary> 
215- 		/// Creates a HttpRequest configured to send a HTTP HEAD request. 
216- 		/// </summary> 
217- 		/// <param name="uri">The URI to which to send a HTTP HEAD request.</param> 
218- 		/// <returns>A HttpRequest configured to transmit a HTTP HEAD request.</returns> 
219- 		public  static HttpRequest  Head ( string  uri ) 
163+ 		/// <see cref="Duck.Http.Service.IHttpService.Head"/> 
164+ 		public  static IHttpRequest  Head ( string  uri ) 
220165		{ 
221- 			return  new   HttpRequest ( UnityWebRequest . Head ( uri ) ) ; 
166+ 			return  Instance . service . Head ( uri ) ; 
222167		} 
223168
224- 		internal  void  Send ( HttpRequest  request ,  Action < HttpResponse >  onSuccess  =  null , 
169+ 		#endregion
170+ 
171+ 		internal  void  Send ( IHttpRequest  request ,  Action < HttpResponse >  onSuccess  =  null , 
225172			Action < HttpResponse >  onError  =  null ,  Action < HttpResponse >  onNetworkError  =  null ) 
226173		{ 
227- 			var  coroutine  =  StartCoroutine ( SendCoroutine ( request ,  onSuccess ,  onError ,  onNetworkError ) ) ; 
174+ 			var  enumerator  =  SendCoroutine ( request ,  onSuccess ,  onError ,  onNetworkError ) ; 
175+ 			var  coroutine  =  StartCoroutine ( enumerator ) ; 
228176			httpRequests . Add ( request ,  coroutine ) ; 
229177		} 
230178
231- 		internal  void  Abort ( HttpRequest  request ) 
179+ 		private  IEnumerator  SendCoroutine ( IHttpRequest  request ,  Action < HttpResponse >  onSuccess  =  null , 
180+ 			Action < HttpResponse >  onError  =  null ,  Action < HttpResponse >  onNetworkError  =  null ) 
232181		{ 
233- 			if  ( request . UnityWebRequest  !=  null  &&  ! request . UnityWebRequest . isDone ) 
234- 			{ 
235- 				request . UnityWebRequest . Abort ( ) ; 
236- 			} 
182+ 			yield  return  service . Send ( request ,  onSuccess ,  onError ,  onNetworkError ) ; 
183+ 			Instance . httpRequests . Remove ( request ) ; 
184+ 		} 
185+ 
186+ 		internal  void  Abort ( IHttpRequest  request ) 
187+ 		{ 
188+ 			Instance . service . Abort ( request ) ; 
237189
238190			if  ( httpRequests . ContainsKey ( request ) ) 
239191			{ 
@@ -247,38 +199,8 @@ private void Update()
247199		{ 
248200			foreach  ( var  httpRequest  in  httpRequests . Keys ) 
249201			{ 
250- 				httpRequest . UpdateProgress ( ) ; 
202+ 				( httpRequest   as   IUpdateProgress ) ? . UpdateProgress ( ) ; 
251203			} 
252204		} 
253- 
254- 		private  static IEnumerator  SendCoroutine ( HttpRequest  request ,  Action < HttpResponse >  onSuccess  =  null , 
255- 			Action < HttpResponse >  onError  =  null ,  Action < HttpResponse >  onNetworkError  =  null ) 
256- 		{ 
257- 			var  unityWebRequest  =  request . UnityWebRequest ; 
258- 			yield  return  unityWebRequest . SendWebRequest ( ) ; 
259- 
260- 			var  response  =  new  HttpResponse ( unityWebRequest ) ; 
261- 
262- 			if  ( unityWebRequest . isNetworkError ) 
263- 			{ 
264- 				if  ( onNetworkError  !=  null ) 
265- 				{ 
266- 					onNetworkError . Invoke ( response ) ; 
267- 				} 
268- 			} 
269- 			else  if  ( unityWebRequest . isHttpError ) 
270- 			{ 
271- 				if  ( onError  !=  null ) 
272- 				{ 
273- 					onError . Invoke ( response ) ; 
274- 				} 
275- 			} 
276- 			else  if  ( onSuccess  !=  null ) 
277- 			{ 
278- 				onSuccess . Invoke ( response ) ; 
279- 			} 
280- 
281- 			Instance . httpRequests . Remove ( request ) ; 
282- 		} 
283205	} 
284206} 
0 commit comments