protected virtual async Task <T> PutAsync <T>(string path, object body, string helperType, object queryParams = null, [CallerMemberName] string callingMethod = "") { var helper = new GenericApiHelper(ConnectionSettings, helperType); var uri = new ClientUriBuilder(helper.ApiRoot).BuildUri(path, null, queryParams); var client = GetClient(uri, SessionToken, null, "application/json"); var val = body != null ? new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json") : null; var res = await client.PutAsync(uri, val); var msg = await res.Content.ReadAsStringAsync(); if (res.StatusCode == HttpStatusCode.NotFound) { throw new EndpointNotFoundException(uri.AbsoluteUri); } if (!res.IsSuccessStatusCode) { throw new Exception(msg); } try { return(string.IsNullOrWhiteSpace(msg) ? default(T) : JsonConvert.DeserializeObject <T>(msg)); } catch (Exception) { return(default(T)); } }
protected virtual async Task <T> PostBinaryAsyncOld <T>(string path, byte[] file, object queryParams, string helperType, [CallerMemberName] string callingMethod = "") { var helper = new GenericApiHelper(ConnectionSettings, helperType); var builder = new ClientUriBuilder(helper.ApiRoot); var uri = builder.BuildUri(path, null, queryParams); var client = GetClient(uri, SessionToken); var response = await client.PostAsync(uri, new ByteArrayContent(file)); if (response == null || !response.IsSuccessStatusCode) { return(default(T)); } var retString = await response.Content.ReadAsStringAsync(); try { return(JsonConvert.DeserializeObject <T>(retString)); } catch (Exception) { return(default(T)); } }
protected virtual async Task <bool> PutVoid(string path, object body, string helperType, object queryParams = null, [CallerMemberName] string callingMethod = "") { var helper = new GenericApiHelper(ConnectionSettings, helperType); var uri = new ClientUriBuilder(helper.ApiRoot).BuildUri(path, null, queryParams); var client = GetClient(uri, SessionToken, null, "application/json"); var val = JsonConvert.SerializeObject(body); var response = await client.PutAsync(uri, new StringContent(val, Encoding.UTF8, "application/json")); if (response.StatusCode == HttpStatusCode.NotFound) { throw new EndpointNotFoundException(uri.AbsoluteUri); } if (!response.IsSuccessStatusCode) { var msg = await response.Content.ReadAsStringAsync(); throw new Exception(msg); } return(response.IsSuccessStatusCode); }
protected virtual async Task <bool> DeleteAsync(string path, string helperType, [CallerMemberName] string callingMethod = "") { var helper = new GenericApiHelper(ConnectionSettings, helperType); var uri = new ClientUriBuilder(helper.ApiRoot).BuildUri(path); var client = GetClient(uri, SessionToken); var res = await client.DeleteAsync(uri); return(res.IsSuccessStatusCode); }
protected virtual async Task <T> GetResultAsync <T>(string path, string helperType, [CallerMemberName] string callingMethod = "") { var helper = helperType == "socinternal" ? (IRestApiClientHelper) new InternalSOCHelper(ConnectionSettings, "soc") : new GenericApiHelper(ConnectionSettings, helperType); var uri = new ClientUriBuilder(helper.ApiRoot).BuildUri(path); var client = GetClient(uri, SessionToken); var res = await client.GetStringAsync(uri); return(JsonConvert.DeserializeObject <T>(res)); }
protected virtual async Task <bool> DeleteAsync(string path, object body, string helperType, [CallerMemberName] string callingMethod = "") { var helper = new GenericApiHelper(ConnectionSettings, helperType); var uri = new ClientUriBuilder(helper.ApiRoot).BuildUri(path); var client = GetClient(uri, SessionToken, null, "application/json"); var val = body != null ? new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json") : null; var res = await client.SendAsync(new HttpRequestMessage(HttpMethod.Delete, uri) { Content = val }); return(res.IsSuccessStatusCode); }
protected virtual T GetResultSync <T>(string path, string helperType, [CallerMemberName] string callingMethod = "") { var helper = helperType == "socinternal" ? (IRestApiClientHelper) new InternalSOCHelper(ConnectionSettings, "soc") : new GenericApiHelper(ConnectionSettings, helperType); var uri = new ClientUriBuilder(helper.ApiRoot).BuildUri(path); var client = GetClient(uri, SessionToken); var res = client.Get <T>(uri.AbsoluteUri); return(res); }
protected virtual async Task <T> PostBinaryAsync <T>(string path, byte[] file, object queryParams, string helperType, [CallerMemberName] string callingMethod = "") { var helper = new GenericApiHelper(ConnectionSettings, helperType); var builder = new ClientUriBuilder(helper.ApiRoot); var uri = builder.BuildUri(path, null, queryParams); using (var client = GetClient(uri, SessionToken)) { using (var content = new MultipartFormDataContent()) { using (var stream = new MemoryStream(file)) { var streamContent = new StreamContent(stream); streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); content.Add(streamContent, path, path); using (var message = await client.PostAsync(uri, content)) { var response = await message.Content.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(response)) { return(default(T)); } try { var res = JsonConvert.DeserializeObject <T>(response); return(res); } catch (Exception e) { throw new MismatchedReturnTypeException(typeof(T), response, e); } } } } } }