private static async Task <GetSireneAccessTokenResponse> GetSireneAccessTokenAsync() { using (var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "client_credentials") })) { using (var message = new HttpRequestMessage(HttpMethod.Post, $"{UrlString}/token") { Content = content }) { using (var response = await Client.SendAsync(message)) { GetSireneAccessTokenResponse respAccessToken = null; if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); GetSireneAccessTokenResponse.TryParse(json, out respAccessToken); } else { Configuration.Instance.LogError($"Web request error '{UrlString}': {response.StatusCode} - {await response.Content.ReadAsStringAsync()}"); } return(respAccessToken); } } } }
private static async Task SetLocalCacheAsync(string key, GetSireneAccessTokenResponse token) { if (Configuration.Instance.UseLocalCache) { string fileName = LocalCacheUtils.GetFullPath("sirene_token.json"); using (var file = File.CreateText(fileName)) { await file.WriteLineAsync(JsonConvert.SerializeObject(token)); } } }
public static bool TryParse(string json, out GetSireneAccessTokenResponse response) { try { response = JsonConvert.DeserializeObject <GetSireneAccessTokenResponse>(json); response.DateOfRequest = DateTimeOffset.UtcNow; return(true); } catch { response = null; return(false); } }