private static ServiceRoute MapServiceRoute(Type serviceType, string routePrefix, HttpConfiguration configuration = null, object constraints = null, bool useMethodPrefixForHttpMethod = true) { if (configuration == null) configuration = new WebApiConfiguration(useMethodPrefixForHttpMethod); var serviceHostFactory = new HttpServiceHostFactory { Configuration = configuration }; var webApiRoute = new WebApiRoute(routePrefix, serviceHostFactory, serviceType) { Constraints = new RouteValueDictionary(constraints) }; return webApiRoute; }
public T Get <T>(WebApiRoute route, ServerConfiguration webApi, object id) { if (MockValues.ContainsKey(route)) { return((T)MockValues[route]); } var client = new HttpClient { BaseAddress = webApi == null ? BaseAddress : new Uri(webApi.ToString()) }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token); var response = client.GetAsync(WebApiRoutes[route] + "?id=" + id ?? "").Result; if (response.IsSuccessStatusCode) { return(JsonConvert.DeserializeObject <T>(response.Content.ReadAsStringAsync().Result)); } throw new HttpRequestException(response.Headers.ToString()); }
public T Patch <T>(WebApiRoute route, object id, object data, ServerConfiguration webApi) { if (MockValues.ContainsKey(route)) { return((T)MockValues[route]); } var client = new HttpClient { BaseAddress = webApi == null ? BaseAddress : new Uri(webApi.ToString()) }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token); var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.Default, "application/json"); var postResponse = client.PatchAsync(WebApiRoutes[route] + "?id=" + id ?? "", content).Result; if (postResponse.IsSuccessStatusCode) { return(JsonConvert.DeserializeObject <T>(postResponse.Content.ReadAsStringAsync().Result)); } throw new HttpRequestException(postResponse.Headers.ToString()); }
public T Patch <T>(WebApiRoute route, object id, object data) => Patch <T>(route, id, data, null);
public T Patch <T>(WebApiRoute route, object id, ServerConfiguration webApi) => Patch <T>(route, id, null, webApi);
public T Put <T>(WebApiRoute route, object data) => Put <T>(route, data, null);
public T Put <T>(WebApiRoute route, ServerConfiguration webApi) => Put <T>(route, null, webApi);
public T Get <T>(WebApiRoute route) => Get <T>(route, null, null);
public T Get <T>(WebApiRoute route, ServerConfiguration webApi) => Get <T>(route, webApi, null);
public T Get <T>(WebApiRoute route, object id) => Get <T>(route, null, id);
public T Delete <T>(WebApiRoute route) => Delete <T>(route, null, null);
public T Delete <T>(WebApiRoute route, object id) => Delete <T>(route, null, id);
public T Delete <T>(WebApiRoute route, ServerConfiguration webApi) => Delete <T>(route, webApi, null);