-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from JulianStaab/post_put_fix
Fixed Post Issue in WebRequestConnector
- Loading branch information
Showing
7 changed files
with
170 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...kit for Unity/Runtime/Utilities/Rest Connectors/JsonEncodeUnityWebRequestRestConnector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using i5.Toolkit.Core.Utilities.Async; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEngine.Networking; | ||
|
||
namespace i5.Toolkit.Core.Utilities | ||
{ | ||
public class JsonEncodeUnityWebRequestRestConnector : UnityWebRequestRestConnector | ||
{ | ||
public override async Task<WebResponse<string>> PostAsync(string uri, string postJson, Dictionary<string, string> headers = null) | ||
{ | ||
byte[] data = new UTF8Encoding().GetBytes(postJson); | ||
return await PostPutDataAsync(uri, "POST", data, "application/json", headers); | ||
} | ||
|
||
public override async Task<WebResponse<string>> PutAsync(string uri, string postData, Dictionary<string, string> headers = null) | ||
{ | ||
byte[] data = new UTF8Encoding().GetBytes(postData); | ||
return await PostPutDataAsync(uri, "PUT", data, "application/json", headers); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...or Unity/Runtime/Utilities/Rest Connectors/JsonEncodeUnityWebRequestRestConnector.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
241 changes: 126 additions & 115 deletions
241
...ts/i5 Toolkit for Unity/Runtime/Utilities/Rest Connectors/UnityWebRequestRestConnector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,135 @@ | ||
using i5.Toolkit.Core.Utilities.Async; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEngine.Networking; | ||
|
||
namespace i5.Toolkit.Core.Utilities | ||
{ | ||
public class UnityWebRequestRestConnector : IRestConnector | ||
{ | ||
public async Task<WebResponse<string>> DeleteAsync(string uri, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = UnityWebRequest.Delete(uri)) | ||
{ | ||
AddHeaders(req, headers); | ||
req.downloadHandler = new DownloadHandlerBuffer(); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
public async Task<WebResponse<string>> GetAsync(string uri, Dictionary<string, string> headers = null) | ||
using i5.Toolkit.Core.Utilities.Async; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEngine.Networking; | ||
|
||
namespace i5.Toolkit.Core.Utilities | ||
{ | ||
public class UnityWebRequestRestConnector : IRestConnector | ||
{ | ||
public virtual async Task<WebResponse<string>> DeleteAsync(string uri, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = UnityWebRequest.Delete(uri)) | ||
{ | ||
AddHeaders(req, headers); | ||
req.downloadHandler = new DownloadHandlerBuffer(); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
public virtual async Task<WebResponse<string>> GetAsync(string uri, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = UnityWebRequest.Get(uri)) | ||
{ | ||
AddHeaders(req, headers); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
public virtual async Task<WebResponse<string>> PostAsync(string uri, string putJson, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = UnityWebRequest.Post(uri, putJson)) | ||
{ | ||
req.downloadHandler = new DownloadHandlerBuffer(); | ||
req.SetRequestHeader("Content-Type", "application/json"); | ||
req.SetRequestHeader("Accept", "application/json"); | ||
|
||
AddHeaders(req, headers); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
public virtual async Task<WebResponse<string>> PostAsync(string uri, byte[] postData, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = UnityWebRequest.Get(uri)) | ||
{ | ||
AddHeaders(req, headers); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
public async Task<WebResponse<string>> PostAsync(string uri, string postData, Dictionary<string, string> headers = null) | ||
return await PostPutDataAsync(uri, "POST", postData, "application/octet-stream", headers); | ||
} | ||
|
||
public virtual async Task<WebResponse<string>> PutAsync(string uri, string putJson, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = UnityWebRequest.Put(uri, putJson)) | ||
{ | ||
req.SetRequestHeader("Content-Type", "application/json"); | ||
AddHeaders(req, headers); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
public virtual async Task<WebResponse<string>> PutAsync(string uri, byte[] putData, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = UnityWebRequest.Post(uri, postData)) | ||
{ | ||
req.downloadHandler = new DownloadHandlerBuffer(); | ||
req.SetRequestHeader("Content-Type", "application/json"); | ||
req.SetRequestHeader("Accept", "application/json"); | ||
|
||
AddHeaders(req, headers); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
public async Task<WebResponse<string>> PostAsync(string uri, byte[] postData, Dictionary<string, string> headers = null) | ||
return await PostPutDataAsync(uri, "PUT", putData, "application/octet-stream", headers); | ||
} | ||
|
||
protected async Task<WebResponse<string>> PostPutDataAsync(string uri, string method, byte[] data, string contentType, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = new UnityWebRequest(uri, "POST")) | ||
{ | ||
req.uploadHandler = new UploadHandlerRaw(postData); | ||
req.downloadHandler = new DownloadHandlerBuffer(); | ||
req.SetRequestHeader("Content-Type", "application/octet-stream"); | ||
using (UnityWebRequest req = new UnityWebRequest(uri, method)) | ||
{ | ||
req.uploadHandler = new UploadHandlerRaw(data); | ||
req.downloadHandler = new DownloadHandlerBuffer(); | ||
req.SetRequestHeader("Content-Type", contentType); | ||
|
||
AddHeaders(req, headers); | ||
AddHeaders(req, headers); | ||
|
||
await req.SendWebRequest(); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
public async Task<WebResponse<string>> PutAsync(string uri, string postData, Dictionary<string, string> headers = null) | ||
{ | ||
using (UnityWebRequest req = UnityWebRequest.Put(uri, postData)) | ||
{ | ||
req.SetRequestHeader("Content-Type", "application/json"); | ||
AddHeaders(req, headers); | ||
await req.SendWebRequest(); | ||
|
||
if (req.isHttpError || req.isNetworkError) | ||
{ | ||
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error); | ||
} | ||
else | ||
{ | ||
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode); | ||
} | ||
} | ||
} | ||
|
||
private void AddHeaders(UnityWebRequest req, Dictionary<string, string> headers) | ||
{ | ||
if (headers == null) | ||
{ | ||
return; | ||
} | ||
foreach (KeyValuePair<string, string> header in headers) | ||
{ | ||
req.SetRequestHeader(header.Key, header.Value); | ||
} | ||
} | ||
} | ||
protected void AddHeaders(UnityWebRequest req, Dictionary<string, string> headers) | ||
{ | ||
if (headers == null) | ||
{ | ||
return; | ||
} | ||
foreach (KeyValuePair<string, string> header in headers) | ||
{ | ||
req.SetRequestHeader(header.Key, header.Value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters