This commit is contained in:
runette committed 2024-04-22 15:52:51 +01:00
commit e0afef912e
1285 files changed
+83219

No files matched your search

+142
View File
@@ -0,0 +1,142 @@
namespace Mapbox.Tokens
{
using Mapbox.Json;
using System;
using System.Text;
/// <summary>
/// Mapbox Token: https://www.mapbox.com/api-documentation/accounts/#retrieve-a-token
/// </summary>
public class MapboxToken
{
/// <summary>String representation of the token' status </summary>
[JsonProperty("code")]
public string Code;
/// <summary>Token metadata</summary>
[JsonProperty("token")]
public TokenMetadata TokenMetadata;
/// <summary>Parsed token status from 'code'</summary>
[JsonIgnore]
public MapboxTokenStatus Status = MapboxTokenStatus.StatusNotYetSet;
/// <summary>True if there was an error during requesting or parsing the token</summary>
[JsonIgnore]
public bool HasError;
/// <summary>Error message if the token could not be requested or parsed</summary>
[JsonIgnore]
public string ErrorMessage;
public static MapboxToken FromResponseData(byte[] data)
{
if (null == data || data.Length < 1)
{
return new MapboxToken()
{
HasError = true,
ErrorMessage = "No data received from token endpoint."
};
}
string jsonTxt = Encoding.UTF8.GetString(data);
MapboxToken token = new MapboxToken();
try
{
token = JsonConvert.DeserializeObject<MapboxToken>(jsonTxt);
MapboxTokenStatus status = (MapboxTokenStatus)Enum.Parse(typeof(MapboxTokenStatus), token.Code);
if (!Enum.IsDefined(typeof(MapboxTokenStatus), status))
{
throw new Exception(string.Format("could not convert token.code '{0}' to MapboxTokenStatus", token.Code));
}
token.Status = status;
}
catch (Exception ex)
{
token.HasError = true;
token.ErrorMessage = ex.Message;
}
return token;
}
}
/// <summary>
/// Every token has a metadata object that contains information about the capabilities of the token.
/// https://www.mapbox.com/api-documentation/accounts/#token-metadata-object
/// </summary>
public class TokenMetadata
{
/// <summary>the identifier for the token</summary>
[JsonProperty("id")]
public string ID;
/// <summary>the type of token</summary>
[JsonProperty("usage")]
public string Usage;
/// <summary>if the token is a default token</summary>
[JsonProperty("default")]
public bool Default;
/// <summary></summary>
[JsonProperty("user")]
public string User;
/// <summary></summary>
[JsonProperty("authorization")]
public string Authorization;
/// <summary>date and time the token was created</summary>
[JsonProperty("created")]
public string Created;
/// <summary>date and time the token was last modified</summary>
[JsonProperty("modified")]
public string Modified;
/// <summary>array of scopes granted to the token</summary>
[JsonProperty("scopes")]
public string[] Scopes;
/// <summary>the client for the token, always 'api'</summary>
[JsonProperty("client")]
public string Client;
/// <summary>the token itself</summary>
[JsonProperty("token")]
public string Token;
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 6ac4a9ce5130afa49a675b6b40b746f3
timeCreated: 1512084369
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,80 @@
using Mapbox.Unity;
namespace Mapbox.Tokens
{
using Mapbox.Platform;
using System;
using System.ComponentModel;
using Mapbox.VectorTile.Geometry;
public enum MapboxTokenStatus
{
/// <summary>The token is valid and active </summary>
[Description("The token is valid and active")]
TokenValid,
/// <summary>the token can not be parsed </summary>
[Description("the token can not be parsed")]
TokenMalformed,
/// <summary>the signature for the token does not validate </summary>
[Description("the signature for the token does not validate")]
TokenInvalid,
/// <summary> the token was temporary and expired</summary>
[Description("the token was temporary and expired")]
TokenExpired,
/// <summary>the token's authorization has been revoked </summary>
[Description("the token's authorization has been revoked")]
TokenRevoked,
/// <summary>inital value </summary>
StatusNotYetSet
}
/// <summary>
/// Wrapper class to retrieve details about a token
/// </summary>
public class MapboxTokenApi
{
public MapboxTokenApi() { }
// use internal FileSource without(!) passing access token from config into constructor
// otherwise access token would be appended to url twice
// https://www.mapbox.com/api-documentation/accounts/#retrieve-a-token
// if we should ever implement other API methods: creating, deleting, updating ... tokens
// we will need another FileSource with the token from the config
private FileSource _fs;
public void Retrieve(Func<string> skuToken, string accessToken, Action<MapboxToken> callback)
{
if (_fs == null)
{
_fs = new FileSource(skuToken);
}
_fs.Request(
Utils.Constants.BaseAPI + "tokens/v2?access_token=" + accessToken,
(Response response) =>
{
if (response.HasError)
{
callback(new MapboxToken()
{
HasError = true,
ErrorMessage = response.ExceptionsAsString
});
return;
}
callback(MapboxToken.FromResponseData(response.Data));
}
);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 048525bbaeb7aaa41b37e458c98090ee
timeCreated: 1512083739
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: