This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
generated from RageAgainstThePixel/upm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BlockadeLabsClient.cs
60 lines (52 loc) · 2.56 KB
/
BlockadeLabsClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed under the MIT License. See LICENSE in the project root for license information.
using BlockadeLabs.Skyboxes;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Security.Authentication;
using Utilities.WebRequestRest;
namespace BlockadeLabs
{
public sealed class BlockadeLabsClient : BaseClient<BlockadeLabsAuthentication, BlockadeLabsSettings>
{
/// <summary>
/// Creates a new client for the BlockadeLabs API, handling auth and allowing for access to various API endpoints.
/// </summary>
/// <param name="authentication">The API authentication information to use for API calls,
/// or <see langword="null"/> to attempt to use the <see cref="BlockadeLabsAuthentication.Default"/>,
/// potentially loading from environment vars or from a config file.</param>
/// <param name="settings">Optional, <see cref="BlockadeLabsSettings"/> for specifying a proxy domain.</param>
/// <exception cref="AuthenticationException">Raised when authentication details are missing or invalid.</exception>
public BlockadeLabsClient(BlockadeLabsAuthentication authentication = null, BlockadeLabsSettings settings = null)
: base(authentication ?? BlockadeLabsAuthentication.Default, settings ?? BlockadeLabsSettings.Default)
{
SkyboxEndpoint = new SkyboxEndpoint(this);
}
protected override void ValidateAuthentication()
{
if (Authentication?.Info == null)
{
throw new InvalidCredentialException($"Invalid {nameof(BlockadeLabsAuthentication)}");
}
if (!HasValidAuthentication)
{
throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/RageAgainstThePixel/com.rest.blockadelabs#authentication for details.");
}
}
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 static JsonSerializerSettings JsonSerializationOptions { get; } = new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore
};
public SkyboxEndpoint SkyboxEndpoint { get; }
}
}