Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.rest.blockadelabs 1.0.0-preview.1 #1

Merged
merged 18 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updated rest client for webgl support
  • Loading branch information
StephenHodgson committed Jun 9, 2023
commit dc53cd1f9894cda648bd95a3f85cc1c67baf9f6c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BlockadeLabs;
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEditor;
using Utilities.Rest.Editor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.IO;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using BlockadeLabs.Skyboxes;
using Newtonsoft.Json;
using System.Net.Http;
using System.Collections.Generic;
using System.Security.Authentication;
using Utilities.WebRequestRest;

namespace BlockadeLabs
{
public sealed class BlockadeLabsClient : BaseClient<BlockadeLabsAuthentication, BlockadeLabsSettings>
{
public BlockadeLabsClient(BlockadeLabsAuthentication authentication = null, BlockadeLabsSettings settings = null, HttpClient httpClient = null)
: base(authentication ?? BlockadeLabsAuthentication.Default, settings ?? BlockadeLabsSettings.Default, httpClient)
public BlockadeLabsClient(BlockadeLabsAuthentication authentication = null, BlockadeLabsSettings settings = null)
: base(authentication ?? BlockadeLabsAuthentication.Default, settings ?? BlockadeLabsSettings.Default)
{
JsonSerializationOptions = new JsonSerializerSettings
{
Expand All @@ -19,14 +21,6 @@ public BlockadeLabsClient(BlockadeLabsAuthentication authentication = null, Bloc
SkyboxEndpoint = new SkyboxEndpoint(this);
}

protected override HttpClient SetupClient(HttpClient httpClient = null)
{
httpClient ??= new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", "com.rest.blockadelabs");
httpClient.DefaultRequestHeaders.Add("X-API-Key", Authentication.Info.ApiKey);
return httpClient;
}

protected override void ValidateAuthentication()
{
if (!HasValidAuthentication)
Expand All @@ -35,6 +29,17 @@ protected override void ValidateAuthentication()
}
}

protected override void SetupDefaultRequestHeaders()
{
DefaultRequestHeaders = new Dictionary<string, string>
{
#if !UNITY_WEBGL
{"User-Agent", "com.rest.blockadelabs" },
#endif
{"X-API-Key", Authentication.Info.ApiKey }
};
}

public override bool HasValidAuthentication => !string.IsNullOrWhiteSpace(Authentication?.Info?.ApiKey);

internal JsonSerializerSettings JsonSerializationOptions { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using Utilities.Rest.Extensions;
using UnityEngine.Scripting;
using Utilities.WebRequestRest;

namespace BlockadeLabs.Skyboxes
{
public sealed class SkyboxEndpoint : BlockadeLabsBaseEndpoint
{
[Preserve]
private class SkyboxMetadata
{
[Preserve]
public SkyboxMetadata([JsonProperty("request")] SkyboxInfo request)
{
Request = request;
}

[Preserve]
[JsonProperty("request")]
public SkyboxInfo Request { get; }
}
Expand All @@ -34,9 +37,9 @@ public SkyboxMetadata([JsonProperty("request")] SkyboxInfo request)
public async Task<IReadOnlyList<SkyboxStyle>> GetSkyboxStylesAsync(CancellationToken cancellationToken = default)
{
var endpoint = GetUrl("/styles");
var response = await client.Client.GetAsync(endpoint, cancellationToken);
var responseAsString = await response.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IReadOnlyList<SkyboxStyle>>(responseAsString, client.JsonSerializationOptions);
var response = await Rest.GetAsync(endpoint, parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
response.Validate();
return JsonConvert.DeserializeObject<IReadOnlyList<SkyboxStyle>>(response.Body, client.JsonSerializationOptions);
}

/// <summary>
Expand All @@ -47,22 +50,22 @@ public async Task<IReadOnlyList<SkyboxStyle>> GetSkyboxStylesAsync(CancellationT
/// <returns></returns>
public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, CancellationToken cancellationToken = default)
{
var jsonContent = JsonConvert.SerializeObject(skyboxRequest, client.JsonSerializationOptions).ToJsonStringContent();
var response = await client.Client.PostAsync(GetUrl("/generate"), jsonContent, cancellationToken);
var responseAsString = await response.ReadAsStringAsync();
var skyboxInfo = JsonConvert.DeserializeObject<SkyboxInfo>(responseAsString, client.JsonSerializationOptions);
var jsonContent = JsonConvert.SerializeObject(skyboxRequest, client.JsonSerializationOptions);
var response = await Rest.PostAsync(GetUrl("/generate"), jsonContent, parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
response.Validate();
var skyboxInfo = JsonConvert.DeserializeObject<SkyboxInfo>(response.Body, client.JsonSerializationOptions);

var downloadTasks = new List<Task>(2)
{
Task.Run(async () =>
{
skyboxInfo.MainTexture = await Rest.DownloadTextureAsync(skyboxInfo.MainTextureUrl, cancellationToken: cancellationToken);
skyboxInfo.MainTexture = await Rest.DownloadTextureAsync(skyboxInfo.MainTextureUrl, parameters: null, cancellationToken: cancellationToken);
}, cancellationToken),
Task.Run(async () =>
{
if (!string.IsNullOrWhiteSpace(skyboxInfo.DepthTextureUrl))
{
skyboxInfo.DepthTexture = await Rest.DownloadTextureAsync(skyboxInfo.DepthTextureUrl, cancellationToken: cancellationToken);
skyboxInfo.DepthTexture = await Rest.DownloadTextureAsync(skyboxInfo.DepthTextureUrl, parameters: null, cancellationToken: cancellationToken);
}
}, cancellationToken)
};
Expand All @@ -79,9 +82,9 @@ public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, C
/// <returns><see cref="SkyboxInfo"/>.</returns>
public async Task<SkyboxInfo> GetSkyboxInfoAsync(int id, CancellationToken cancellationToken = default)
{
var response = await client.Client.GetAsync(GetUrl($"/info/{id}"), cancellationToken);
var responseAsString = await response.ReadAsStringAsync();
return JsonConvert.DeserializeObject<SkyboxMetadata>(responseAsString, client.JsonSerializationOptions).Request;
var response = await Rest.GetAsync(GetUrl($"/info/{id}"), parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
response.Validate();
return JsonConvert.DeserializeObject<SkyboxMetadata>(response.Body, client.JsonSerializationOptions).Request;
}
}
}
2 changes: 1 addition & 1 deletion BlockadeLabs/Packages/com.rest.blockadelabs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"url": "https://github.com/StephenHodgson"
},
"dependencies": {
"com.utilities.rest": "1.4.4"
"com.utilities.rest": "2.0.0"
},
"publishConfig": {
"registry": "https://package.openupm.com"
Expand Down
6 changes: 2 additions & 4 deletions BlockadeLabs/Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"dependencies": {
"com.unity.ide.rider": "3.0.21",
"com.unity.ide.rider": "3.0.22",
"com.unity.ide.visualstudio": "2.0.18",
"com.unity.test-framework": "1.1.33",
"com.unity.textmeshpro": "3.0.6",
"com.utilities.extensions": "1.1.1",
"com.utilities.rest": "file:E:/Dev/Rage/com.utilities.rest/Utilities.Rest/Packages/com.utilities.rest"
"com.unity.textmeshpro": "3.0.6"
},
"scopedRegistries": [
{
Expand Down