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

Commit 9c47d33

Browse files
com.rest.blockadelabs 1.0.0-preview.5 (#6)
1 parent 8d6c5f9 commit 9c47d33

File tree

8 files changed

+392
-39
lines changed

8 files changed

+392
-39
lines changed

Runtime/BlockadeLabsClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override void SetupDefaultRequestHeaders()
3636
#if !UNITY_WEBGL
3737
{"User-Agent", "com.rest.blockadelabs" },
3838
#endif
39-
{"X-API-Key", Authentication.Info.ApiKey }
39+
{"x-api-key", Authentication.Info.ApiKey }
4040
};
4141
}
4242

Runtime/BlockadeLabsSettingsInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace BlockadeLabs
55
{
66
public sealed class BlockadeLabsSettingsInfo : ISettingsInfo
77
{
8-
internal const string DefaultDomain = "blockade.cloudshell.run";
8+
internal const string DefaultDomain = "backend.blockadelabs.com";
99
internal const string DefaultVersion = "v1";
1010

1111
public BlockadeLabsSettingsInfo()

Runtime/Skyboxes/SkyboxEndpoint.cs

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,124 @@
11
// Licensed under the MIT License. See LICENSE in the project root for license information.
22

33
using Newtonsoft.Json;
4+
using System;
45
using System.Collections.Generic;
6+
using System.IO;
57
using System.Threading;
68
using System.Threading.Tasks;
9+
using UnityEngine;
10+
using UnityEngine.Scripting;
711
using Utilities.WebRequestRest;
812

913
namespace BlockadeLabs.Skyboxes
1014
{
1115
public sealed class SkyboxEndpoint : BlockadeLabsBaseEndpoint
1216
{
17+
[Preserve]
18+
private class SkyboxInfoRequest
19+
{
20+
[Preserve]
21+
[JsonConstructor]
22+
public SkyboxInfoRequest([JsonProperty("request")] SkyboxInfo skyboxInfo)
23+
{
24+
SkyboxInfo = skyboxInfo;
25+
}
26+
27+
[Preserve]
28+
[JsonProperty("request")]
29+
public SkyboxInfo SkyboxInfo { get; }
30+
}
31+
1332
public SkyboxEndpoint(BlockadeLabsClient client) : base(client) { }
1433

15-
protected override string Root => "skybox";
34+
protected override string Root => string.Empty;
1635

1736
/// <summary>
1837
/// Returns the list of predefined styles that can influence the overall aesthetic of your skybox generation.
1938
/// </summary>
20-
/// <param name="cancellationToken"></param>
39+
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
2140
/// <returns>A list of <see cref="SkyboxStyle"/>s.</returns>
2241
public async Task<IReadOnlyList<SkyboxStyle>> GetSkyboxStylesAsync(CancellationToken cancellationToken = default)
2342
{
24-
var endpoint = GetUrl("/styles");
43+
var endpoint = GetUrl("skybox/styles");
2544
var response = await Rest.GetAsync(endpoint, parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
2645
response.Validate();
2746
return JsonConvert.DeserializeObject<IReadOnlyList<SkyboxStyle>>(response.Body, client.JsonSerializationOptions);
2847
}
2948

3049
/// <summary>
31-
/// Generate a skybox image
50+
/// Generate a skybox image.
3251
/// </summary>
33-
/// <param name="skyboxRequest"></param>
34-
/// <param name="cancellationToken"></param>
35-
/// <returns></returns>
36-
public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, CancellationToken cancellationToken = default)
52+
/// <param name="skyboxRequest"><see cref="SkyboxRequest"/>.</param>
53+
/// <param name="pollingInterval">Optional, polling interval in seconds.</param>
54+
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
55+
/// <returns><see cref="SkyboxInfo"/>.</returns>
56+
public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, int? pollingInterval = null, CancellationToken cancellationToken = default)
3757
{
38-
var jsonContent = JsonConvert.SerializeObject(skyboxRequest, client.JsonSerializationOptions);
39-
var response = await Rest.PostAsync(GetUrl("/generate"), jsonContent, parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
58+
var formData = new WWWForm();
59+
formData.AddField("prompt", skyboxRequest.Prompt);
60+
61+
if (!string.IsNullOrWhiteSpace(skyboxRequest.NegativeText))
62+
{
63+
formData.AddField("negative_text", skyboxRequest.NegativeText);
64+
}
65+
66+
if (skyboxRequest.Seed.HasValue)
67+
{
68+
formData.AddField("seed", skyboxRequest.Seed.Value);
69+
}
70+
71+
if (skyboxRequest.SkyboxStyleId.HasValue)
72+
{
73+
formData.AddField("skybox_style_id", skyboxRequest.SkyboxStyleId.Value);
74+
}
75+
76+
if (skyboxRequest.RemixImagineId.HasValue)
77+
{
78+
formData.AddField("remix_imagine_id", skyboxRequest.RemixImagineId.Value);
79+
}
80+
81+
if (skyboxRequest.Depth)
82+
{
83+
formData.AddField("return_depth", skyboxRequest.Depth.ToString());
84+
}
85+
86+
if (skyboxRequest.ControlImage != null)
87+
{
88+
if (!string.IsNullOrWhiteSpace(skyboxRequest.ControlModel))
89+
{
90+
formData.AddField("control_model", skyboxRequest.ControlModel);
91+
}
92+
93+
using var imageData = new MemoryStream();
94+
await skyboxRequest.ControlImage.CopyToAsync(imageData, cancellationToken);
95+
formData.AddBinaryData("control_image", imageData.ToArray(), skyboxRequest.ControlImageFileName);
96+
skyboxRequest.Dispose();
97+
}
98+
99+
var response = await Rest.PostAsync(GetUrl("skybox"), formData, parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
40100
response.Validate();
41101
var skyboxInfo = JsonConvert.DeserializeObject<SkyboxInfo>(response.Body, client.JsonSerializationOptions);
42102

103+
while (!cancellationToken.IsCancellationRequested)
104+
{
105+
await Task.Delay(pollingInterval ?? 3 * 1000, cancellationToken)
106+
.ConfigureAwait(true); // Configure await to make sure we're still in Unity context
107+
skyboxInfo = await GetSkyboxInfoAsync(skyboxInfo, cancellationToken);
108+
109+
if (skyboxInfo.Status is "pending" or "processing")
110+
{
111+
continue;
112+
}
113+
114+
break;
115+
}
116+
117+
if (skyboxInfo.Status != "complete")
118+
{
119+
throw new Exception($"Failed to generate skybox! {skyboxInfo.Id} -> {skyboxInfo.Status}\nError: {skyboxInfo.ErrorMessage}\n{skyboxInfo}");
120+
}
121+
43122
var downloadTasks = new List<Task>(2)
44123
{
45124
Task.Run(async () =>
@@ -62,14 +141,14 @@ public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, C
62141
/// <summary>
63142
/// Returns the skybox metadata for the given skybox id.
64143
/// </summary>
65-
/// <param name="id"></param>
66-
/// <param name="cancellationToken"></param>
144+
/// <param name="id">Skybox Id.</param>
145+
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
67146
/// <returns><see cref="SkyboxInfo"/>.</returns>
68147
public async Task<SkyboxInfo> GetSkyboxInfoAsync(int id, CancellationToken cancellationToken = default)
69148
{
70-
var response = await Rest.GetAsync(GetUrl($"/info/{id}"), parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
149+
var response = await Rest.GetAsync(GetUrl($"imagine/requests/{id}"), parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
71150
response.Validate();
72-
return JsonConvert.DeserializeObject<SkyboxInfo>(response.Body, client.JsonSerializationOptions);
151+
return JsonConvert.DeserializeObject<SkyboxInfoRequest>(response.Body, client.JsonSerializationOptions).SkyboxInfo;
73152
}
74153
}
75154
}

Runtime/Skyboxes/SkyboxInfo.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public SkyboxInfo(
1919
[JsonProperty("title")] string title,
2020
[JsonProperty("obfuscated_id")] string obfuscatedId,
2121
[JsonProperty("created_at")] DateTime createdAt,
22-
[JsonProperty("updated_at")] DateTime updatedAt)
22+
[JsonProperty("updated_at")] DateTime updatedAt,
23+
[JsonProperty("error_message")] string errorMessage = null)
2324
{
2425
Id = id;
2526
SkyboxStyleId = skyboxStyleId;
@@ -33,6 +34,7 @@ public SkyboxInfo(
3334
ObfuscatedId = obfuscatedId;
3435
CreatedAt = createdAt;
3536
UpdatedAt = updatedAt;
37+
ErrorMessage = errorMessage;
3638
}
3739

3840
[JsonProperty("id")]
@@ -79,5 +81,12 @@ public SkyboxInfo(
7981

8082
[JsonProperty("updated_at")]
8183
public DateTime UpdatedAt { get; }
84+
85+
[JsonProperty("error_message")]
86+
public string ErrorMessage { get; set; }
87+
88+
public override string ToString() => JsonConvert.SerializeObject(this, Formatting.Indented);
89+
90+
public static implicit operator int(SkyboxInfo skyboxInfo) => skyboxInfo.Id;
8291
}
8392
}

0 commit comments

Comments
 (0)