public async Task <Item> GetItemByIdAsync(Guid id) { Item result = null; try { using (var client = new HttpClient()) { var resource = new Uri(this.ItemsResource, $"{ id }"); var response = await InventoryClient.GetWithTimeoutAsync(resource, client); InventoryClient.ThrowExceptionOnError(response); var content = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <Item>(content); } } catch (TaskCanceledException e) { InventoryClient.ThrowTimeoutException(e); } return(result); }
public async Task <IEnumerable <Item> > GetItemsInCategoryAsync(string category) { List <Item> result = null; try { using (var client = new HttpClient()) { var resource = new Uri(this.ItemsResource, $"?category={ WebUtility.UrlEncode(category) }"); var response = await InventoryClient.GetWithTimeoutAsync(resource, client); InventoryClient.ThrowExceptionOnError(response); var content = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <List <Item> >(content); } } catch (TaskCanceledException e) { InventoryClient.ThrowTimeoutException(e); } return(result); }
public async Task RemovePhotos(Guid itemId, string concurrencyId, Uri removed) { try { using (var client = new HttpClient()) { var resource = new Uri(this.ItemsResource, $"{ itemId }"); var payload = new { Id = itemId.ToString(), ConcurrencyId = concurrencyId, Photo = removed }; var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json"); content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("domain-model", "RemovePhotoCommand")); var response = await InventoryClient.PutWithTimeoutAsync(resource, content, client); InventoryClient.ThrowExceptionOnError(response); } } catch (TaskCanceledException e) { InventoryClient.ThrowTimeoutException(e); } }
public async Task SaveItemAsync(Item update) { try { using (var client = new HttpClient()) { var resource = new Uri(this.ItemsResource, $"{ update.Id }"); var content = new StringContent(JsonConvert.SerializeObject(update), Encoding.UTF8, "application/json"); content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("domain-model", "UpdateItemCommand")); var response = await InventoryClient.PutWithTimeoutAsync(resource, content, client); InventoryClient.ThrowExceptionOnError(response); } } catch (TaskCanceledException e) { InventoryClient.ThrowTimeoutException(e); } }
public async Task <IReadOnlyCollection <string> > GetAllCategoriesAsync() { List <string> result = null; try { using (var client = new HttpClient()) { var response = await InventoryClient.GetWithTimeoutAsync(this.CategoriesResource, client); InventoryClient.ThrowExceptionOnError(response); var content = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <List <string> >(content); } } catch (TaskCanceledException e) { InventoryClient.ThrowTimeoutException(e); } return(result); }