public async Task PutAsync <T>(string relativeUrl, T obj) { var json = LowercaseJsonSerializer.SerializeObject(obj); var response = await LimitConcurrentRequests(async() => await client.PutAsync(relativeUrl, new StringContent(json, Encoding.UTF8, JsonContentType))); var content = await response.Content.ReadAsStringAsync(); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { throw new InvalidOperationException(content, ex); } }
public async Task <TResult> PostMultipartAsync <TResult, YObject>(string relativeUrl, YObject obj, string resource, Stream stream, string fileName) { var form = new MultipartFormDataContent(); foreach (var prop in typeof(YObject).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { if (prop.GetSetMethod() == null || !prop.CanWrite || !prop.CanRead) { continue; //ignore not settable properties; } var name = prop.Name.ToLowerInvariant(); var value = prop.GetValue(obj, null); var defaultValue = GetDefault(prop.PropertyType); if (value == null || (name == "id" && value.Equals(defaultValue))) { continue; //ignore id property if it is null or default } form.Add(new StringContent(value.ToString()), $"{resource}[{name}]"); } if (stream != null) { var fileContent = new StreamContent(stream); fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream"); form.Add(fileContent, $"{resource}[attachment]", fileName); } var response = await LimitConcurrentRequests(async() => await client.PostAsync(relativeUrl, form)); var content = await response.Content.ReadAsStringAsync(); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { throw new InvalidOperationException(content, ex); } return(LowercaseJsonSerializer.DeserializeObject <TResult>(content)); }
public async Task <T> GetAsync <T>(string relativeUrl) { return(await LimitConcurrentRequests(async() => LowercaseJsonSerializer.DeserializeObject <T>(await client.GetStringAsync(relativeUrl)))); }