Skip to content

Commit

Permalink
com.utilities.rest 1.2.3 (#10)
Browse files Browse the repository at this point in the history
- added PATCH
- updated dependencies
  • Loading branch information
StephenHodgson committed Mar 10, 2023
1 parent 46e3116 commit fd52007
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 53 deletions.
19 changes: 19 additions & 0 deletions Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Advanced features includes progress notifications, authentication and native mul
- [Get](#get)
- [Post](#post)
- [Put](#put)
- [Patch](#patch)
- [Delete](#delete)
- [Download Multimedia](#multimedia)
- [Caching](#caching)
Expand Down Expand Up @@ -112,6 +113,24 @@ catch (Exception e)
Debug.Log($"[{response.ResponseCode}] {response.ResponseBody}");
```

### Patch

```csharp
Response response;
string jsonData = "{\"data\":\"content\"}";

try
{
response = await Rest.PatchAsync("www.your.api/endpoint", jsonData);
}
catch (Exception e)
{
Debug.LogError(e);
}

Debug.Log($"[{response.ResponseCode}] {response.ResponseBody}");
```

### Delete

```csharp
Expand Down
8 changes: 0 additions & 8 deletions Editor.meta

This file was deleted.

1 change: 0 additions & 1 deletion Editor/AssemblyInfo.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Editor/AssemblyInfo.cs.meta

This file was deleted.

15 changes: 0 additions & 15 deletions Editor/Utilities.Rest.Editor.asmdef

This file was deleted.

7 changes: 0 additions & 7 deletions Editor/Utilities.Rest.Editor.asmdef.meta

This file was deleted.

65 changes: 60 additions & 5 deletions Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace Utilities.WebRequestRest
/// </summary>
public static class Rest
{
private const string khttpVerbPATCH = "PATCH";

#region Authentication

/// <summary>
Expand Down Expand Up @@ -131,9 +133,9 @@ public static string GetBearerOAuthToken(string authToken)
CancellationToken cancellationToken = default)
{
#if UNITY_2022_2_OR_NEWER
using var webRequest = UnityWebRequest.PostWwwForm(query, "POST");
using var webRequest = UnityWebRequest.PostWwwForm(query, UnityWebRequest.kHttpVerbPOST);
#else
using var webRequest = UnityWebRequest.Post(query, "POST");
using var webRequest = UnityWebRequest.Post(query, UnityWebRequest.kHttpVerbPOST);
#endif
var data = new UTF8Encoding().GetBytes(jsonData);
webRequest.uploadHandler = new UploadHandlerRaw(data);
Expand Down Expand Up @@ -162,9 +164,9 @@ public static string GetBearerOAuthToken(string authToken)
CancellationToken cancellationToken = default)
{
#if UNITY_2022_2_OR_NEWER
using var webRequest = UnityWebRequest.PostWwwForm(query, "POST");
using var webRequest = UnityWebRequest.PostWwwForm(query, UnityWebRequest.kHttpVerbPOST);
#else
using var webRequest = UnityWebRequest.Post(query, "POST");
using var webRequest = UnityWebRequest.Post(query, UnityWebRequest.kHttpVerbPOST);
#endif
webRequest.uploadHandler = new UploadHandlerRaw(bodyData);
webRequest.downloadHandler = new DownloadHandlerBuffer();
Expand Down Expand Up @@ -224,6 +226,58 @@ public static string GetBearerOAuthToken(string authToken)

#endregion PUT

#region PATCH

/// <summary>
/// Rest PATCH.
/// </summary>
/// <param name="query">Finalized Endpoint Query with parameters.</param>
/// <param name="jsonData">Data to be submitted.</param>
/// <param name="headers">Optional, header information for the request.</param>
/// <param name="progress">Optional, <see cref="IProgress{T}"/> handler.</param>
/// <param name="timeout">Optional, time in seconds before request expires.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>The response data.</returns>
public static async Task<Response> PatchAsync(
string query,
string jsonData,
Dictionary<string, string> headers = null,
IProgress<float> progress = null,
int timeout = -1,
CancellationToken cancellationToken = default)
{
using var webRequest = UnityWebRequest.Put(query, jsonData);
webRequest.method = khttpVerbPATCH;
webRequest.SetRequestHeader("Content-Type", "application/json");
return await ProcessRequestAsync(webRequest, headers, progress, timeout, cancellationToken);
}

/// <summary>
/// Rest PATCH.
/// </summary>
/// <param name="query">Finalized Endpoint Query with parameters.</param>
/// <param name="bodyData">Data to be submitted.</param>
/// <param name="headers">Optional, header information for the request.</param>
/// <param name="progress">Optional, <see cref="IProgress{T}"/> handler.</param>
/// <param name="timeout">Optional, time in seconds before request expires.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>The response data.</returns>
public static async Task<Response> PatchAsync(
string query,
byte[] bodyData,
Dictionary<string, string> headers = null,
IProgress<float> progress = null,
int timeout = -1,
CancellationToken cancellationToken = default)
{
using var webRequest = UnityWebRequest.Put(query, bodyData);
webRequest.method = khttpVerbPATCH;
webRequest.SetRequestHeader("Content-Type", "application/octet-stream");
return await ProcessRequestAsync(webRequest, headers, progress, timeout, cancellationToken);
}

#endregion PATCH

#region DELETE

/// <summary>
Expand Down Expand Up @@ -680,7 +734,8 @@ private static bool TryGetFileNameFromUrl(string url, out string fileName)

var isUpload = webRequest.method is
UnityWebRequest.kHttpVerbPOST or
UnityWebRequest.kHttpVerbPUT;
UnityWebRequest.kHttpVerbPUT or
khttpVerbPATCH;

// HACK: Workaround for extra quotes around boundary.
if (isUpload)
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Utilities.Rest.asmdef
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Utilities.Rest",
"rootNamespace": "",
"rootNamespace": "Utilities.Rest",
"references": [
"GUID:a6609af893242c7438d701ddd4cce46a",
"GUID:84651a3751eca9349aac36a66bba901b"
Expand Down
5 changes: 4 additions & 1 deletion Tests/Utilities.Rest.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "Utilities.Rest.Tests",
"rootNamespace": "Utilities.Rest.Tests",
"references": [
"GUID:27619889b8ba8c24980f49ee34dbb44a",
"GUID:0acc523941302664db1f4e527237feb3"
"GUID:0acc523941302664db1f4e527237feb3",
"GUID:a6609af893242c7438d701ddd4cce46a",
"GUID:7958db66189566541a6363568aee1575"
],
"includePlatforms": [
"Editor"
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
"displayName": "Utilities.Rest",
"description": "This package contains useful RESTful utilities for the Unity Game Engine.",
"keywords": [],
"version": "1.2.2",
"version": "1.2.3",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest/releases",
"license": "MIT",
"repository": {
"type": "git"
"type": "git",
"repository": "https://github.com/RageAgainstThePixel/com.utilities.rest.git"
},
"author": "Stephen Hodgson",
"url": "https://github.com/StephenHodgson",
"dependencies": {
"com.utilities.async": "1.2.0",
"com.utilities.async": "1.2.1",
"com.unity.modules.unitywebrequest": "1.0.0",
"com.unity.modules.unitywebrequestassetbundle": "1.0.0",
"com.unity.modules.unitywebrequestaudio": "1.0.0",
"com.unity.modules.unitywebrequesttexture": "1.0.0",
"com.unity.modules.unitywebrequestwww": "1.0.0"
},
"hideInEditor": true
"hideInEditor": true,
"publishConfig": {
"registry": "https://package.openupm.com"
}
}

0 comments on commit fd52007

Please sign in to comment.