Skip to content

Commit

Permalink
Merge pull request #22 from JulianStaab/post_put_fix
Browse files Browse the repository at this point in the history
Fixed Post Issue in WebRequestConnector
  • Loading branch information
BenediktHensen authored Aug 6, 2023
2 parents 52b9fa1 + cc913f0 commit 6352d8a
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public GitHubOidcProvider() : base()
authorizationEndpoint = "https://github.com/login/oauth/authorize";
tokenEndpoint = "https://github.com/login/oauth/access_token";
userInfoEndpoint = "https://api.github.com/user";
RestConnector = new JsonEncodeUnityWebRequestRestConnector();
}


Expand All @@ -39,7 +40,7 @@ public override async Task<string> GetAccessTokenFromCodeAsync(string code, stri
}

string uri = tokenEndpoint + $"?client_id={ClientData.ClientId}" +
$"&redirect_uri={redirectUri}&client_secret={ClientData.ClientSecret}&code={code}&grant_type=authorization_code";
$"&redirect_uri={redirectUri}" + $"&client_secret={ClientData.ClientSecret}&code={code}&grant_type=authorization_code";
WebResponse<string> response = await RestConnector.PostAsync(uri, "");

if (response.Successful)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ public interface IRestConnector
{
Task<WebResponse<string>> GetAsync(string uri, Dictionary<string, string> headers = null);

Task<WebResponse<string>> PostAsync(string uri, string postData, Dictionary<string, string> headers = null);
Task<WebResponse<string>> PostAsync(string uri, string postJson, Dictionary<string, string> headers = null);

Task<WebResponse<string>> PostAsync(string uri, byte[] postData, Dictionary<string, string> headers = null);

Task<WebResponse<string>> PutAsync(string uri, string postData, Dictionary<string, string> headers = null);
Task<WebResponse<string>> PutAsync(string uri, string putJson, Dictionary<string, string> headers = null);

Task<WebResponse<string>> DeleteAsync(string uri, Dictionary<string, string> headers = null);
Task<WebResponse<string>> PutAsync(string uri, byte[] putData, Dictionary<string, string> headers = null);

Task<WebResponse<string>> DeleteAsync(string uri, Dictionary<string, string> headers = null);
}
}
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2bcd49101686deb46985b2d4caf07398, type: 3}
m_Name:
m_EditorClassIdentifier:
deepLinkClient: {fileID: 11400000, guid: 8c071ab4ddaa3dc48bba3f9f4a947800, type: 2}
serverClient: {fileID: 11400000, guid: f165a3fb78409a7469f4d0ffe7ffb340, type: 2}
deepLinkClient: {fileID: 11400000, guid: f4c3133019218bc4bbb46b2b4bac3ad9, type: 2}
serverClient: {fileID: 11400000, guid: f4c3133019218bc4bbb46b2b4bac3ad9, type: 2}
--- !u!4 &1780600243
Transform:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override void RegisterServices()
googleOidc.OidcProvider = new GoogleOidcProvider();
// this example shows how the service can be used on an app for multiple platforms
#if !UNITY_EDITOR
googleOidc.RedirectURI = "i5:/";
googleOidc.RedirectURI = "com.i5.i5-toolkit-for-unity:/";
#else
googleOidc.RedirectURI = "https://www.google.com";
#endif
Expand Down

0 comments on commit 6352d8a

Please sign in to comment.