String GetKeyParam(CacheMethod cacheMethod, IParameterCollection inputs) { StringBuilder sb = new StringBuilder(); sb.ToString(); sb.Append(cacheMethod.ToString()); if (inputs != null && inputs.Count > 0) { foreach (var item in inputs) { sb.Append("-"); sb.Append(item.ToString()); } } return(sb.ToString()); }
public async Task <RestClientResponse> ExecuteAsync(byte[] requestBody = null, CacheMethod cacheMethod = CacheMethod.OnlyOnline) { if (RequestMessage.Method != HttpMethod.Get && cacheMethod != CacheMethod.Default) { throw new Exception(string.Format("Http method {0} does not support CacheMethod {1}.", RequestMessage.Method.ToString(), cacheMethod.ToString())); } Log.Info("HTTP {0} {1} (CacheMethod={2})", RequestMessage.Method.ToString(), RequestMessage.RequestUri.ToString(), cacheMethod.ToString()); if (requestBody == null) { Log.Verbose("HTTP Body: null"); } else { Log.Verbose("HTTP Body: {0}", Encoding.UTF8.GetString(requestBody, 0, requestBody.Length)); } byte[] responseBody = null; HttpResponseMessage httpResponseMessage = null; CacheControlHeaderValue cacheControl = new CacheControlHeaderValue(); RequestMessage.Headers.CacheControl = cacheControl; switch (cacheMethod) { case CacheMethod.OnlyCached: { cacheControl.OnlyIfCached = (cacheMethod == CacheMethod.OnlyCached); break; } case CacheMethod.PreferCached: { cacheControl.Extensions.Add(new NameValueHeaderValue("Straaw", "PreferCached")); break; } case CacheMethod.Default: { break; } case CacheMethod.PreferOnline: { cacheControl.Extensions.Add(new NameValueHeaderValue("Straaw", "PreferOnline")); break; } case CacheMethod.OnlyOnline: { cacheControl.NoCache = (cacheMethod == CacheMethod.OnlyOnline); break; } } if (requestBody != null) { RequestMessage.Content = new ByteArrayContent(requestBody); RequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); } using (Log.Scope(LogLevel.Verbose, "HTTP Request Timing")) { try { httpResponseMessage = await _httpClient.SendAsync(RequestMessage, HttpCompletionOption.ResponseHeadersRead); } catch (Exception e) { Log.Warning(e.Message); throw; } } using (Log.Scope(LogLevel.Verbose, "HTTP Response Timing")) { if (httpResponseMessage.Content != null) { responseBody = await httpResponseMessage.Content.ReadAsByteArrayAsync(); } } if (responseBody != null && responseBody.Length > 0) { Log.Verbose("HTTP Response body {0} bytes: {1}", responseBody.Length, Encoding.UTF8.GetString(responseBody, 0, responseBody.Length)); } else { Log.Verbose("HTTP Response body 0 bytes"); } return(new RestClientResponse(httpResponseMessage, responseBody)); }