Skip to content

Commit

Permalink
com.utilities.rest 2.5.0 (#66)
Browse files Browse the repository at this point in the history
- moved optional debug parameter for multimedia requests into RestParameters
- added optional DownloadCache flag to RestParameters (defaults to true)
- added Rest.DownloadFIleBytesAsync
- added Rest.DownloadBytesAsync
- enabled DownloadLocation to be set (must be an approved download location)

---------

Co-authored-by: Simon (Darkside) Jackson <[email protected]>
  • Loading branch information
StephenHodgson and SimonDarksideJ authored Jan 15, 2024
1 parent c9f40be commit 0e48121
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 136 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ var restParameters = new RestParameters(
disposeDownloadHandler, // Optional, dispose the DownloadHandler. Default is true.
disposeUploadHandler, // Optional, dispose the UploadHandler. Default is true.
certificateHandler, // Optional, certificate handler for the request.
disposeCertificateHandler); // Optional, dispose the CertificateHandler. Default is true.
disposeCertificateHandler, // Optional, dispose the CertificateHandler. Default is true.
cacheDownloads, // Optional, cache downloaded content. Default is true
debug); // Optional, enable debug output of the request. Default is false.
var response = await Rest.GetAsync("www.your.api/endpoint", restParameters);
```

Expand Down Expand Up @@ -158,9 +160,15 @@ response.Validate(debug: true);

```csharp
// cache directory defaults to {Application.temporaryCachePath}/download_cache/
// it is currently not possible to set this value, but is likely a nice feature request.
Debug.Log(Rest.DownloadCacheDirectory);

// cache directory can be set to one of:
// Application.temporaryCachePath,
// Application.persistentDataPath,
// Application.dataPath,
// Application.streamingAssetsPath
Rest.DownloadCacheDirectory = Application.dataPath;

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

if (Rest.TryGetDownloadCacheItem(uri, out var cachedFilePath))
Expand All @@ -181,6 +189,8 @@ Rest.DeleteDownloadCache();

#### Files

Download a file.

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

Expand All @@ -190,6 +200,30 @@ if (!string.IsNullOrWhiteSpace(downloadedFilePath))
}
```

Download a file, and get the raw byte data.

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

if (downloadedBytes != null && downloadedBytes.Length > 0)
{
Debug.Log(downloadedBytes.Length);
}
```

Download raw file bytes.

> This request does not cache data
```csharp
var downloadedBytes = await Rest.DownloadBytesAsync("www.your.api/your_file.pdf");

if (downloadedBytes != null && downloadedBytes.Length > 0)
{
Debug.Log(downloadedBytes.Length);
}
```

#### Textures

> Pro Tip: This also works with local file paths to load textures async at runtime!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ A Utilities.Rest package for the [Unity](https://unity.com/) Game Engine.

## Installing

Requires Unity 2021.3 LTS or higher.

The recommended installation method is though the unity package manager and [OpenUPM](https://openupm.com/packages/com.utilities.rest).

### Via Unity Package Manager and OpenUPM

- Open your Unity project settings
Expand Down Expand Up @@ -77,7 +81,9 @@ var restParameters = new RestParameters(
disposeDownloadHandler, // Optional, dispose the DownloadHandler. Default is true.
disposeUploadHandler, // Optional, dispose the UploadHandler. Default is true.
certificateHandler, // Optional, certificate handler for the request.
disposeCertificateHandler); // Optional, dispose the CertificateHandler. Default is true.
disposeCertificateHandler, // Optional, dispose the CertificateHandler. Default is true.
cacheDownloads, // Optional, cache downloaded content. Default is true
debug); // Optional, enable debug output of the request. Default is false.
var response = await Rest.GetAsync("www.your.api/endpoint", restParameters);
```

Expand Down Expand Up @@ -154,9 +160,15 @@ response.Validate(debug: true);

```csharp
// cache directory defaults to {Application.temporaryCachePath}/download_cache/
// it is currently not possible to set this value, but is likely a nice feature request.
Debug.Log(Rest.DownloadCacheDirectory);

// cache directory can be set to one of:
// Application.temporaryCachePath,
// Application.persistentDataPath,
// Application.dataPath,
// Application.streamingAssetsPath
Rest.DownloadCacheDirectory = Application.dataPath;

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

if (Rest.TryGetDownloadCacheItem(uri, out var cachedFilePath))
Expand All @@ -177,6 +189,8 @@ Rest.DeleteDownloadCache();

#### Files

Download a file.

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

Expand All @@ -186,6 +200,30 @@ if (!string.IsNullOrWhiteSpace(downloadedFilePath))
}
```

Download a file, and get the raw byte data.

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

if (downloadedBytes != null && downloadedBytes.Length > 0)
{
Debug.Log(downloadedBytes.Length);
}
```

Download raw file bytes.

> This request does not cache data
```csharp
var downloadedBytes = await Rest.DownloadBytesAsync("www.your.api/your_file.pdf");

if (downloadedBytes != null && downloadedBytes.Length > 0)
{
Debug.Log(downloadedBytes.Length);
}
```

#### Textures

> Pro Tip: This also works with local file paths to load textures async at runtime!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public virtual TAuthentication LoadDefaultsReversed()
LoadFromDirectory() ??
LoadFromAsset();

[Obsolete("Use LoadFromAsset (remove angle bracket type specification)")]
public TAuthentication LoadFromAsset<T>() => LoadFromAsset();

/// <summary>
/// Attempts to load the authentication from a <see cref="ScriptableObject"/> asset that implements <see cref="IConfiguration"/>.
/// </summary>
Expand Down
Loading

0 comments on commit 0e48121

Please sign in to comment.