Skip to content

Commit

Permalink
fixed acax bug issue
Browse files Browse the repository at this point in the history
  • Loading branch information
michael811125 committed Sep 22, 2023
1 parent f726e00 commit 0bfffb8
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 278 deletions.
Original file line number Diff line number Diff line change
@@ -1,69 +1,131 @@
using Cysharp.Threading.Tasks;
using OxGFrame.AgencyCenter.APICenter;

public class #SCRIPTNAME# : APIBase
{
/* Declare Your Delegate (Callback) */

public void Req(/* Custom Your Parameters */)
{
Http.Acax(
"url",
// Method
"POST",
// Header
new string[,] {
{ "Content-Type", "application/json"}
},
// Body
new object[,] {
// { "key", "value"},
},
// Response
(json) =>
{
// Response Json Example:
// {
// "status": true,
// "message": "success",
// "data": []
// }

/*
Do Data Callback
*/
}
);
}

public async UniTask ReqAsync(/* Custom Your Parameters */)
{
await Http.AcaxAsync(
"url",
// Method
"POST",
// Header
new string[,] {
{ "Content-Type", "application/json"}
},
// Body
new object[,] {
// { "key", "value"},
},
// Response
(json) =>
{
// Response Json Example:
// {
// "status": true,
// "message": "success",
// "data": []
// }

/*
Do Data Callback
*/
}
);
}
}
using Cysharp.Threading.Tasks;
using OxGFrame.AgencyCenter.APICenter;

public class #SCRIPTNAME# : APIBase
{
/* Declare Your Delegate (Callback) */

public void ReqGet(/* Custom Your Parameters */)
{
Http.Acax(
"url" + "?"
// + $"key={value}&"
,
// Method
"GET",
// Header
new string[,] {
{ "Content-Type", "application/json"}
},
// Body
null,
// Response
(json) =>
{
// Response Json Example:
// {
// "status": true,
// "message": "success",
// "data": []
// }

/*
Do Data Callback
*/
}
);
}

public async UniTask ReqGetAsync(/* Custom Your Parameters */)
{
await Http.AcaxAsync(
"url" + "?"
// + $"key={value}&"
,
// Method
"GET",
// Header
new string[,] {
{ "Content-Type", "application/json"}
},
// Body
null,
// Response
(json) =>
{
// Response Json Example:
// {
// "status": true,
// "message": "success",
// "data": []
// }

/*
Do Data Callback
*/
}
);
}

public void ReqPost(/* Custom Your Parameters */)
{
Http.Acax(
"url",
// Method
"POST",
// Header
new string[,] {
{ "Content-Type", "application/json"}
},
// Body
new object[,] {
// { "key", "value"},
},
// Response
(json) =>
{
// Response Json Example:
// {
// "status": true,
// "message": "success",
// "data": []
// }

/*
Do Data Callback
*/
}
);
}

public async UniTask ReqPostAsync(/* Custom Your Parameters */)
{
await Http.AcaxAsync(
"url",
// Method
"POST",
// Header
new string[,] {
{ "Content-Type", "application/json"}
},
// Body
new object[,] {
// { "key", "value"},
},
// Response
(json) =>
{
// Response Json Example:
// {
// "status": true,
// "message": "success",
// "data": []
// }

/*
Do Data Callback
*/
}
);
}
}
174 changes: 89 additions & 85 deletions Assets/OxGFrame/AgencyCenter/Scripts/Runtime/APICenter/Acax/Acax.cs
Original file line number Diff line number Diff line change
@@ -1,86 +1,90 @@
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using System.Collections.Generic;
using UnityEngine.Networking;

namespace OxGFrame.AgencyCenter.APICenter
{
public static class Http
{
public delegate void ResponseHandle(string response);

/// <summary>
/// Callback C# and Xml = Acax
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="headers"></param>
/// <param name="body"></param>
/// <param name="success"></param>
/// <param name="error"></param>
public static void Acax(string url, string method, string[,] headers, object[,] body, ResponseHandle success = null, ResponseHandle error = null)
{
method = method.ToUpper();
RequestAPI(url, method, headers, body, success, error).Forget();
}

/// <summary>
/// Asynchronous with Callback C# and Xml = Acax
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="headers"></param>
/// <param name="body"></param>
/// <param name="success"></param>
/// <param name="error"></param>
/// <returns></returns>
public async static UniTask<string> AcaxAsync(string url, string method, string[,] headers, object[,] body, ResponseHandle success = null, ResponseHandle error = null)
{
method = method.ToUpper();
return await RequestAPI(url, method, headers, body, success, error);
}

internal static async UniTask<string> RequestAPI(string url, string method, string[,] headers, object[,] body, ResponseHandle success, ResponseHandle error)
{
using (UnityWebRequest request = new UnityWebRequest(url, method))
{
if (body.Length > 0)
{
Dictionary<string, object> jsonObj = new Dictionary<string, object>();
for (int row = 0; row < body.GetLength(0); row++)
{
if (body.GetLength(1) != 2) continue;
jsonObj.Add((string)body[row, 0], body[row, 1]);
}
string json = JsonConvert.SerializeObject(jsonObj);

byte[] jsonBinary = System.Text.Encoding.Default.GetBytes(json);
request.uploadHandler = new UploadHandlerRaw(jsonBinary);
request.downloadHandler = new DownloadHandlerBuffer();
}

if (headers.Length > 0)
{
for (int row = 0; row < headers.GetLength(0); row++)
{
if (headers.GetLength(1) != 2) continue;
request.SetRequestHeader(headers[row, 0], headers[row, 1]);
}
}

await request.SendWebRequest();

if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
{
if (error != null) error(request.error);
return null;
}
else
{
if (success != null) success(request.downloadHandler.text);
return request.downloadHandler.text;
}
}
}
}
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using System.Collections.Generic;
using UnityEngine.Networking;

namespace OxGFrame.AgencyCenter.APICenter
{
public delegate void ResponseHandle(string response);

public static class Http
{
/// <summary>
/// Callback C# and Xml = Acax
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="headers"></param>
/// <param name="body"></param>
/// <param name="success"></param>
/// <param name="error"></param>
public static void Acax(string url, string method, string[,] headers, object[,] body, ResponseHandle success = null, ResponseHandle error = null)
{
method = method.ToUpper();
RequestAPI(url, method, headers, body, success, error).Forget();
}

/// <summary>
/// Asynchronous with Callback C# and Xml = Acax
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="headers"></param>
/// <param name="body"></param>
/// <param name="success"></param>
/// <param name="error"></param>
/// <returns></returns>
public async static UniTask<string> AcaxAsync(string url, string method, string[,] headers, object[,] body, ResponseHandle success = null, ResponseHandle error = null)
{
method = method.ToUpper();
return await RequestAPI(url, method, headers, body, success, error);
}

internal static async UniTask<string> RequestAPI(string url, string method, string[,] headers, object[,] body, ResponseHandle success, ResponseHandle error)
{
using (UnityWebRequest request = new UnityWebRequest(url, method))
{
// Header args
if (headers != null && headers.Length > 0)
{
for (int row = 0; row < headers.GetLength(0); row++)
{
if (headers.GetLength(1) != 2) continue;
request.SetRequestHeader(headers[row, 0], headers[row, 1]);
}
}

// Body args
if (body != null && body.Length > 0)
{
Dictionary<string, object> jsonArgs = new Dictionary<string, object>();
for (int row = 0; row < body.GetLength(0); row++)
{
if (body.GetLength(1) != 2) continue;
jsonArgs.Add((string)body[row, 0], body[row, 1]);
}
string json = JsonConvert.SerializeObject(jsonArgs);
byte[] jsonBinary = System.Text.Encoding.Default.GetBytes(json);
request.uploadHandler = new UploadHandlerRaw(jsonBinary);
}

// Response download buffer
request.downloadHandler = new DownloadHandlerBuffer();

// Start send request
await request.SendWebRequest();

if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
{
if (error != null) error(request.error);
return null;
}
else
{
if (success != null) success(request.downloadHandler.text);
return request.downloadHandler.text;
}
}
}
}
}
4 changes: 4 additions & 0 deletions Assets/OxGFrame/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## [2.7.12] - 2023-09-22
- Fixed Acax GET method bug issue, download buffer is null reference if without body data.
- Modified Acax header args and body args can be null.

## [2.7.11] - 2023-09-12
- Added IsRetryActive for RetryCounter (retryCount > 0).
- Fixed RetryCounter reference bug issue.
Expand Down
Loading

0 comments on commit 0bfffb8

Please sign in to comment.