Skip to content

Commit

Permalink
com.utilities.rest 2.5.7 (#76)
Browse files Browse the repository at this point in the history
- updated com.utilities.async -> 2.1.5
- updated docs

---------

Co-authored-by: Simon (Darkside) Jackson <[email protected]>
  • Loading branch information
StephenHodgson and SimonDarksideJ committed May 10, 2024
1 parent a9a3258 commit 650c92a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
56 changes: 47 additions & 9 deletions Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ var basicAuthentication = Rest.GetBasicAuthentication("username", "password");
var bearerToken = Rest.GetBearerOAuthToken("authToken");
```

These methods will generate the necessary data to apply to your authentication headers, they DO NOT set the authentication for any Rest calls.

### Rest Parameters

Rest parameters have been bundled into a single object to make the method signatures a bit more uniform.
Rest parameters have been bundled into a single object to make the method signatures a bit more uniform, these can be provided with all requests made using the `com.utilities.rest` library to control how the operation performs.

```csharp
var restParameters = new RestParameters(
Expand All @@ -82,11 +84,17 @@ var restParameters = new RestParameters(
disposeUploadHandler, // Optional, dispose the UploadHandler. Default is true.
certificateHandler, // Optional, certificate handler for the request.
disposeCertificateHandler, // Optional, dispose the CertificateHandler. Default is true.
cacheDownloads, // Optional, cache downloaded content. Default is true
cacheDownloads, // Optional, cache downloaded content. Default is true.
debug); // Optional, enable debug output of the request. Default is false.
// Rest call passing a pre-configured set of RestParameters
var response = await Rest.GetAsync("www.your.api/endpoint", restParameters);
```

If you require any of the above options when making calls, ensure to add a `RestParameters` construct to the request, as shown above.

> Pro Tip: If you have multiple requests using the same "Parameters", then cache a single instance of the `RestParameters` and use this for all calls instead of recreating it for each request.
### Get

```csharp
Expand Down Expand Up @@ -149,7 +157,7 @@ response.Validate(debug: true);
### Delete

```csharp
var response = await Rest.DeleteAsync("www.your.api/endpoint", restParameters);
var response = await Rest.DeleteAsync("www.your.api/endpoint");
// Validates the response for you and will throw a RestException if the response is unsuccessful.
response.Validate(debug: true);
```
Expand All @@ -167,7 +175,7 @@ Debug.Log(Rest.DownloadCacheDirectory);
// Application.persistentDataPath,
// Application.dataPath,
// Application.streamingAssetsPath
Rest.DownloadCacheDirectory = Application.dataPath;
Rest.DownloadLocation = Application.streamingAssetsPath;

var uri = "www.url.to/remote/resource";

Expand All @@ -182,6 +190,10 @@ if (Rest.TryGetDownloadCacheItem(uri, out var cachedFilePath))
Debug.Log($"Deleted {cachedFilePath}");
}
}
else
{
Debug.Log($"Unable to download from {uri}");
}

// Clear the whole download cache directory
Rest.DeleteDownloadCache();
Expand All @@ -194,24 +206,33 @@ Download a file.
```csharp
var downloadedFilePath = await Rest.DownloadFileAsync("www.your.api/your_file.pdf");

if (!string.IsNullOrWhiteSpace(downloadedFilePath))
if (!string.IsNullOrWhiteSpace(downloadedFilePath) && File.Exists(downloadedFilePath))
{
Debug.Log(downloadedFilePath);
}
else
{
Debug.LogError($"Failed to download file!");
}
```

Download a file, and get the raw byte data.
Download a file, then return the raw byte data.

```csharp
var downloadedBytes = await Rest.DownloadFileBytesAsync("www.your.api/your_file.pdf");

if (downloadedBytes != null && downloadedBytes.Length > 0)
{
// do some custom loading of your binary file.
Debug.Log(downloadedBytes.Length);
}
else
{
Debug.LogError($"Failed to download file!");
}
```

Download raw file bytes.
Download raw file bytes directly into memory.

> This request does not cache data
Expand All @@ -222,6 +243,10 @@ if (downloadedBytes != null && downloadedBytes.Length > 0)
{
Debug.Log(downloadedBytes.Length);
}
else
{
Debug.LogError($"Failed to download file!");
}
```

#### Textures
Expand All @@ -236,6 +261,10 @@ if (texture != null)
// assign it to your renderer
rawImage.texture = texture;
}
else
{
Debug.LogError($"Failed to download texture!");
}
```

#### Audio
Expand All @@ -251,6 +280,10 @@ if (audioClip != null)
audioSource.clip = audioClip;
audioSource.PlayOneShot(audioClip);
}
else
{
Debug.LogError($"Failed to download audio clip!");
}
```

##### Audio Streaming
Expand All @@ -273,11 +306,16 @@ audioSource.clip = audioClip;
#### Asset Bundles

```csharp
var assetBundle = await Rest.DownloadAssetBundleAsync("www.your.api/asset.bundle");
var bundleOptions = new AssetBundleRequestOptions();
var assetBundle = await Rest.DownloadAssetBundleAsync("www.your.api/asset.bundle", bundleOptions);

if (assetBundle != null)
{
var cube = bundle.LoadAsset<GameObject>("Cube");
var cube = assetBundle.LoadAsset<GameObject>("Cube");
Instantiate(cube);
}
else
{
Debug.LogError($"Failed to download and load asset bundle!");
}
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Utilities.Rest",
"description": "This package contains useful RESTful utilities for the Unity Game Engine.",
"keywords": [],
"version": "2.5.6",
"version": "2.5.7",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest/releases",
Expand All @@ -15,7 +15,7 @@
"author": "Stephen Hodgson",
"url": "https://github.com/StephenHodgson",
"dependencies": {
"com.utilities.async": "2.1.3",
"com.utilities.async": "2.1.5",
"com.utilities.extensions": "1.1.15",
"com.unity.modules.unitywebrequest": "1.0.0",
"com.unity.modules.unitywebrequestassetbundle": "1.0.0",
Expand Down

0 comments on commit 650c92a

Please sign in to comment.