namespace Mapbox.Tokens { using Mapbox.Json; using System; using System.Text; /// /// Mapbox Token: https://www.mapbox.com/api-documentation/accounts/#retrieve-a-token /// public class MapboxToken { /// String representation of the token' status [JsonProperty("code")] public string Code; /// Token metadata [JsonProperty("token")] public TokenMetadata TokenMetadata; /// Parsed token status from 'code' [JsonIgnore] public MapboxTokenStatus Status = MapboxTokenStatus.StatusNotYetSet; /// True if there was an error during requesting or parsing the token [JsonIgnore] public bool HasError; /// Error message if the token could not be requested or parsed [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(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; } } /// /// 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 /// public class TokenMetadata { /// the identifier for the token [JsonProperty("id")] public string ID; /// the type of token [JsonProperty("usage")] public string Usage; /// if the token is a default token [JsonProperty("default")] public bool Default; /// [JsonProperty("user")] public string User; /// [JsonProperty("authorization")] public string Authorization; /// date and time the token was created [JsonProperty("created")] public string Created; /// date and time the token was last modified [JsonProperty("modified")] public string Modified; /// array of scopes granted to the token [JsonProperty("scopes")] public string[] Scopes; /// the client for the token, always 'api' [JsonProperty("client")] public string Client; /// the token itself [JsonProperty("token")] public string Token; } }