Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit c601cfa

Browse files
com.rest.blockadelabs 1.0.0 (#7)
- Initial Release! 🎉
1 parent 9c47d33 commit c601cfa

27 files changed

+3653
-37
lines changed

Documentation~/README.md

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ I am not affiliated with BlockadeLabs and an account with api access is required
1212

1313
Requires Unity 2021.3 LTS or higher.
1414

15-
The recommended installation method is though the unity package manager and [OpenUPM](https://openupm.com/packages/com.openai.unity).
15+
The recommended installation method is though the unity package manager and [OpenUPM](https://openupm.com/packages/com.rest.blockadelabs).
1616

1717
### Via Unity Package Manager and OpenUPM
1818

@@ -35,6 +35,7 @@ The recommended installation method is though the unity package manager and [Ope
3535
- Add package from git url: `https://github.com/RageAgainstThePixel/com.rest.blockadelabs.git#upm`
3636
> Note: this repo has dependencies on other repositories! You are responsible for adding these on your own.
3737
- [com.utilities.async](https://github.com/RageAgainstThePixel/com.utilities.async)
38+
- [com.utilities.extensions](https://github.com/RageAgainstThePixel/com.utilities.extensions)
3839
- [com.utilities.rest](https://github.com/RageAgainstThePixel/com.utilities.rest)
3940

4041
## Documentation
@@ -46,6 +47,10 @@ The recommended installation method is though the unity package manager and [Ope
4647
- [Get Skybox Styles](#get-skybox-styles)
4748
- [Generate Skybox](#generate-skybox)
4849
- [Get Skybox by Id](#get-skybox)
50+
- [Delete Skybox by Id](#delete-skybox)
51+
- [Get Skybox History](#get-skybox-history)
52+
- [Cancel Skybox Generation](#cancel-skybox-generation)
53+
- [Cancel All Pending Skybox Generations](#cancel-all-pending-skybox-generations)
4954

5055
### Authentication
5156

@@ -108,7 +113,7 @@ var api = new BlockadeLabsClient(BlockadeLabsAuthentication.Default.LoadFromEnvi
108113

109114
### Skyboxes
110115

111-
#### [Get Skybox Styles](https://blockade.cloudshell.run/redoc#tag/skybox/operation/Get_Skybox_Styles_api_v1_skybox_styles_get)
116+
#### [Get Skybox Styles](https://api-documentation.blockadelabs.com/api/skybox.html#get-skybox-styles)
112117

113118
Returns the list of predefined styles that can influence the overall aesthetic of your skybox generation.
114119

@@ -122,23 +127,73 @@ foreach (var skyboxStyle in skyboxStyles)
122127
}
123128
```
124129

125-
#### [Generate Skybox](https://blockade.cloudshell.run/redoc#tag/skybox/operation/Generate_Skybox_api_v1_skybox_generate_post)
130+
#### [Generate Skybox](https://api-documentation.blockadelabs.com/api/skybox.html#generate-skybox)
126131

127-
Generate a skybox image
132+
Generate a skybox.
128133

129134
```csharp
130135
var api = new BlockadeLabsClient();
131-
var request = new SkyboxRequest("underwater", depth: true);
136+
var request = new SkyboxRequest("mars", depth: true);
132137
var skyboxInfo = await api.SkyboxEndpoint.GenerateSkyboxAsync(request);
133138
skyboxMaterial.mainTexture = skyboxInfo.MainTexture;
134139
skyboxMaterial.depthTexture = skyboxInfo.DepthTexture;
135140
```
136141

137-
#### [Get Skybox](https://blockade.cloudshell.run/redoc#tag/skybox/operation/Get_Skybox_By_Id_api_v1_skybox_info__id__get)
142+
#### [Get Skybox](https://api-documentation.blockadelabs.com/api/skybox.html#get-skybox-by-id)
138143

139144
Returns the skybox metadata for the given skybox id.
140145

141146
```csharp
142-
var skyboxInfo = await api.SkyboxEndpoint.GetSkyboxInfoAsync("skybox-id");
147+
var skyboxId = 42;
148+
var skyboxInfo = await api.SkyboxEndpoint.GetSkyboxInfoAsync(skyboxId);
143149
Debug.Log($"Skybox: {result.Id} | {result.MainTextureUrl}");
150+
// Note: If you wish to use the returned skybox textures you'll need to additionally call await SkyboxInfo.LoadTexturesAsync(); before you can assign them to a material property.
151+
await skyboxInfo.LoadTexturesAsync();
152+
skyboxMaterial.mainTexture = skyboxInfo.MainTexture;
153+
skyboxMaterial.depthTexture = skyboxInfo.DepthTexture;
154+
```
155+
156+
#### [Delete Skybox](https://api-documentation.blockadelabs.com/api/skybox.html#delete)
157+
158+
Deletes a skybox by id.
159+
160+
```csharp
161+
var skyboxId = 42;
162+
var result = await api.SkyboxEndpoint.DeleteSkyboxAsync(skybox);
163+
// result == true
164+
```
165+
166+
#### [Get Skybox History](https://api-documentation.blockadelabs.com/api/skybox.html#get-history)
167+
168+
Gets the previously generated skyboxes.
169+
170+
```csharp
171+
var history = await api.SkyboxEndpoint.GetSkyboxHistoryAsync();
172+
Debug.Log($"Found {history.TotalCount} skyboxes");
173+
174+
foreach (var skybox in history.Skyboxes)
175+
{
176+
Debug.Log($"{skybox.Id} {skybox.Title} status: {skybox.Status}");
177+
}
178+
```
179+
180+
#### [Cancel Skybox Generation](https://api-documentation.blockadelabs.com/api/skybox.html#cancel-generation)
181+
182+
Cancels a pending skybox generation request by id.
183+
184+
```csharp
185+
var skyboxId = 42;
186+
var result = await CancelSkyboxGenerationAsync(skyboxId);
187+
// result == true
188+
```
189+
190+
> Note: This is automatically done when cancelling a skybox generation using cancellation token.
191+
192+
#### [Cancel All Pending Skybox Generations](https://api-documentation.blockadelabs.com/api/skybox.html#cancel-all-pending-generations)
193+
194+
Cancels ALL pending skybox generation requests.
195+
196+
```csharp
197+
var result = await api.SkyboxEndpoint.CancelAllPendingSkyboxGenerationsAsync();
198+
Debug.Log(result ? "All pending generations successfully cancelled" : "No pending generations");
144199
```

Runtime/BlockadeLabsAuthInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Licensed under the MIT License. See LICENSE in the project root for license information.
2+
13
using System;
24
using System.Security.Authentication;
35
using UnityEngine;

Runtime/BlockadeLabsBaseEndpoint.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Licensed under the MIT License. See LICENSE in the project root for license information.
2+
13
using Utilities.WebRequestRest;
24

35
namespace BlockadeLabs

Runtime/BlockadeLabsConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Licensed under the MIT License. See LICENSE in the project root for license information.
2+
13
using UnityEngine;
24
using Utilities.WebRequestRest.Interfaces;
35

Runtime/BlockadeLabsSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Licensed under the MIT License. See LICENSE in the project root for license information.
2+
13
using System.Linq;
24
using UnityEngine;
35
using Utilities.WebRequestRest.Interfaces;

Runtime/BlockadeLabsSettingsInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Licensed under the MIT License. See LICENSE in the project root for license information.
2+
13
using System;
24
using Utilities.WebRequestRest.Interfaces;
35

Runtime/Skyboxes/SkyboxEndpoint.cs

Lines changed: 120 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ public SkyboxInfoRequest([JsonProperty("request")] SkyboxInfo skyboxInfo)
2929
public SkyboxInfo SkyboxInfo { get; }
3030
}
3131

32+
[Preserve]
33+
private class SkyboxOperation
34+
{
35+
[Preserve]
36+
[JsonConstructor]
37+
public SkyboxOperation(
38+
[JsonProperty("success")] string success,
39+
[JsonProperty("error")] string error)
40+
{
41+
Success = success;
42+
Error = error;
43+
}
44+
45+
[Preserve]
46+
[JsonProperty("success")]
47+
public string Success { get; }
48+
49+
[Preserve]
50+
[JsonProperty("Error")]
51+
public string Error { get; }
52+
}
53+
3254
public SkyboxEndpoint(BlockadeLabsClient client) : base(client) { }
3355

3456
protected override string Root => string.Empty;
@@ -40,8 +62,7 @@ public SkyboxEndpoint(BlockadeLabsClient client) : base(client) { }
4062
/// <returns>A list of <see cref="SkyboxStyle"/>s.</returns>
4163
public async Task<IReadOnlyList<SkyboxStyle>> GetSkyboxStylesAsync(CancellationToken cancellationToken = default)
4264
{
43-
var endpoint = GetUrl("skybox/styles");
44-
var response = await Rest.GetAsync(endpoint, parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
65+
var response = await Rest.GetAsync(GetUrl("skybox/styles"), parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
4566
response.Validate();
4667
return JsonConvert.DeserializeObject<IReadOnlyList<SkyboxStyle>>(response.Body, client.JsonSerializationOptions);
4768
}
@@ -102,39 +123,36 @@ public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, i
102123

103124
while (!cancellationToken.IsCancellationRequested)
104125
{
105-
await Task.Delay(pollingInterval ?? 3 * 1000, cancellationToken)
126+
await Task.Delay(pollingInterval ?? 3 * 1000, CancellationToken.None)
106127
.ConfigureAwait(true); // Configure await to make sure we're still in Unity context
107-
skyboxInfo = await GetSkyboxInfoAsync(skyboxInfo, cancellationToken);
128+
skyboxInfo = await GetSkyboxInfoAsync(skyboxInfo, CancellationToken.None);
108129

109-
if (skyboxInfo.Status is "pending" or "processing")
130+
if (skyboxInfo.Status is Status.Pending or Status.Processing or Status.Dispatched)
110131
{
111132
continue;
112133
}
113134

114135
break;
115136
}
116137

117-
if (skyboxInfo.Status != "complete")
138+
if (cancellationToken.IsCancellationRequested)
118139
{
119-
throw new Exception($"Failed to generate skybox! {skyboxInfo.Id} -> {skyboxInfo.Status}\nError: {skyboxInfo.ErrorMessage}\n{skyboxInfo}");
140+
var cancelResult = await CancelSkyboxGenerationAsync(skyboxInfo, CancellationToken.None);
141+
142+
if (!cancelResult)
143+
{
144+
throw new Exception($"Failed to cancel generation for {skyboxInfo.Id}");
145+
}
120146
}
121147

122-
var downloadTasks = new List<Task>(2)
148+
cancellationToken.ThrowIfCancellationRequested();
149+
150+
if (skyboxInfo.Status != Status.Complete)
123151
{
124-
Task.Run(async () =>
125-
{
126-
skyboxInfo.MainTexture = await Rest.DownloadTextureAsync(skyboxInfo.MainTextureUrl, parameters: null, cancellationToken: cancellationToken);
127-
}, cancellationToken),
128-
Task.Run(async () =>
129-
{
130-
if (!string.IsNullOrWhiteSpace(skyboxInfo.DepthTextureUrl))
131-
{
132-
skyboxInfo.DepthTexture = await Rest.DownloadTextureAsync(skyboxInfo.DepthTextureUrl, parameters: null, cancellationToken: cancellationToken);
133-
}
134-
}, cancellationToken)
135-
};
136-
137-
await Task.WhenAll(downloadTasks);
152+
throw new Exception($"Failed to generate skybox! {skyboxInfo.Id} -> {skyboxInfo.Status}\nError: {skyboxInfo.ErrorMessage}\n{skyboxInfo}");
153+
}
154+
155+
await skyboxInfo.LoadTexturesAsync(cancellationToken);
138156
return skyboxInfo;
139157
}
140158

@@ -150,5 +168,85 @@ public async Task<SkyboxInfo> GetSkyboxInfoAsync(int id, CancellationToken cance
150168
response.Validate();
151169
return JsonConvert.DeserializeObject<SkyboxInfoRequest>(response.Body, client.JsonSerializationOptions).SkyboxInfo;
152170
}
171+
172+
/// <summary>
173+
/// Deletes a skybox by id.
174+
/// </summary>
175+
/// <param name="id">The id of the skybox.</param>
176+
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
177+
/// <returns>True, if skybox was successfully deleted.</returns>
178+
public async Task<bool> DeleteSkyboxAsync(int id, CancellationToken cancellationToken = default)
179+
{
180+
var response = await Rest.DeleteAsync(GetUrl($"imagine/deleteImagine/{id}"), new RestParameters(client.DefaultRequestHeaders), cancellationToken);
181+
response.Validate();
182+
var skyboxOp = JsonConvert.DeserializeObject<SkyboxOperation>(response.Body, client.JsonSerializationOptions);
183+
184+
const string successStatus = "Item deleted successfully";
185+
186+
if (skyboxOp is not { Success: successStatus })
187+
{
188+
throw new Exception($"Failed to cancel generation for skybox {id}!\n{skyboxOp?.Error}");
189+
}
190+
191+
return skyboxOp.Success.Equals(successStatus);
192+
}
193+
194+
/// <summary>
195+
/// Gets the previously generated skyboxes.
196+
/// </summary>
197+
/// <param name="parameters">Optional, <see cref="SkyboxHistoryParameters"/>.</param>
198+
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
199+
/// <returns><see cref="SkyboxHistory"/>.</returns>
200+
public async Task<SkyboxHistory> GetSkyboxHistoryAsync(SkyboxHistoryParameters parameters = null, CancellationToken cancellationToken = default)
201+
{
202+
var historyRequest = parameters ?? new SkyboxHistoryParameters();
203+
var response = await Rest.GetAsync(GetUrl($"imagine/myRequests{historyRequest}"), parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
204+
response.Validate();
205+
return JsonConvert.DeserializeObject<SkyboxHistory>(response.Body, client.JsonSerializationOptions);
206+
}
207+
208+
/// <summary>
209+
/// Cancels a pending skybox generation request by id.
210+
/// </summary>
211+
/// <param name="id">The id of the skybox.</param>
212+
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
213+
/// <returns>True, if generation was cancelled.</returns>
214+
public async Task<bool> CancelSkyboxGenerationAsync(int id, CancellationToken cancellationToken = default)
215+
{
216+
var response = await Rest.DeleteAsync(GetUrl($"imagine/requests/{id}"), new RestParameters(client.DefaultRequestHeaders), cancellationToken);
217+
response.Validate();
218+
var skyboxOp = JsonConvert.DeserializeObject<SkyboxOperation>(response.Body, client.JsonSerializationOptions);
219+
220+
if (skyboxOp is not { Success: "true" })
221+
{
222+
throw new Exception($"Failed to cancel generation for skybox {id}!\n{skyboxOp?.Error}");
223+
}
224+
225+
return skyboxOp.Success.Equals("true");
226+
}
227+
228+
/// <summary>
229+
/// Cancels ALL pending skybox generation requests.
230+
/// </summary>
231+
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
232+
public async Task<bool> CancelAllPendingSkyboxGenerationsAsync(CancellationToken cancellationToken = default)
233+
{
234+
var response = await Rest.DeleteAsync(GetUrl("imagine/requests/pending"), new RestParameters(client.DefaultRequestHeaders), cancellationToken);
235+
response.Validate();
236+
var skyboxOp = JsonConvert.DeserializeObject<SkyboxOperation>(response.Body, client.JsonSerializationOptions);
237+
238+
if (skyboxOp is not { Success: "true" })
239+
{
240+
if (skyboxOp != null &&
241+
skyboxOp.Error.Contains("You don't have any pending"))
242+
{
243+
return false;
244+
}
245+
246+
throw new Exception($"Failed to cancel all pending skybox generations!\n{skyboxOp?.Error}");
247+
}
248+
249+
return skyboxOp.Success.Equals("true");
250+
}
153251
}
154252
}

Runtime/Skyboxes/SkyboxHistory.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed under the MIT License. See LICENSE in the project root for license information.
2+
3+
using System.Collections.Generic;
4+
using Newtonsoft.Json;
5+
6+
namespace BlockadeLabs.Skyboxes
7+
{
8+
public sealed class SkyboxHistory
9+
{
10+
[JsonConstructor]
11+
public SkyboxHistory(
12+
[JsonProperty("data")] List<SkyboxInfo> skyboxes,
13+
[JsonProperty("totalCount")] int totalCount,
14+
[JsonProperty("has_more")] bool hasMore)
15+
{
16+
Skyboxes = skyboxes;
17+
TotalCount = totalCount;
18+
HasMore = hasMore;
19+
}
20+
21+
[JsonProperty("data")]
22+
public IReadOnlyList<SkyboxInfo> Skyboxes { get; }
23+
24+
[JsonProperty("totalCount")]
25+
public int TotalCount { get; }
26+
27+
[JsonProperty("has_more")]
28+
public bool HasMore { get; }
29+
}
30+
}

Runtime/Skyboxes/SkyboxHistory.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)