public async Task <string> Get(ApiGetRequest request, bool isConfigureAwait) { var path = BuildGetPath(request); using (var response = await httpClient.GetAsync(path).ConfigureAwait(isConfigureAwait)) { if (!response.IsSuccessStatusCode) { switch ((int)response.StatusCode) { case (int)HttpStatusCode.Forbidden: throw new InvalidAPIKeyException($"request={request}"); case (int)HttpStatusCode.NotFound: throw new ResourceNotFoundException($"request={request}"); case 429: // Quota Error throw new QuotaException($"request={request}"); default: throw new BuffettCodeApiClientException($"request={request}"); } } var content = response.Content.ReadAsStringAsync().Result; return(content); } }
public string Get(ApiGetRequest request, bool isConfigureAwait, bool useCache) { if (useCache && cacheHelper.HasCache(request)) { return((string)cacheHelper.Get(request)); } else { return(apiClientCore.Get(request, isConfigureAwait).Result); } }
private static string BuildGetPath(ApiGetRequest request) { return($"{request.EndPoint}?{new FormUrlEncodedContent(request.Parameters).ReadAsStringAsync().Result}"); }
public ApiRequestCacheHelper Set(ApiGetRequest request, String value) { cache.Set(CacheKeyCreator.Create(baseUrl, request), value, policy); return(this); }
public object Get(ApiGetRequest request) => cache.Get(CacheKeyCreator.Create(baseUrl, request));
public bool HasCache(ApiGetRequest request) => cache.Contains(CacheKeyCreator.Create(baseUrl, request));
public static string Create(string baseUrl, ApiGetRequest request) { var paramsStr = String.Join("&", request.Parameters.Select(kv => $"{HttpUtility.UrlEncode(kv.Key)}={HttpUtility.UrlEncode(kv.Value)}").OrderBy(str => str)); return($"api/{baseUrl}/{request.EndPoint}?{paramsStr}"); }