internal async Task Replace <T>(string id, T item, CancellationToken ct) { try { await using var cosmosItem = new CosmosItem <T>(item, id, _partitionKeyStr); var payload = await cosmosItem.ToStream(_partitionKeyHeader, _partitionKeyStr, ct); using var response = await _container.UpsertItemStreamAsync(payload, _partitionKey, cancellationToken : ct); if (response.IsSuccessStatusCode) { return; } throw new CosmosClientException( $"Unable to replace/upsert: {typeof(T).FullName} with id: {id}. Status Code: {response.StatusCode.ToString()}"); } catch (ConfigurationException ex) { // There's an issue with Android and the System.Configuration.ConfigurationManager: https://github.com/xamarin/Xamarin.Forms/issues/5935 // For now we are ignoring ConfigurationExceptions as it seems that the operation works despite the exception. } catch (Exception ex) { throw new CosmosClientException($"Unable to replace/upsert: {typeof(T).FullName} with id: {id}. Unhandled exception: {ex}", ex); } }
internal async Task <IEnumerable <T> > GetPartitionObjects <T>(CancellationToken ct) { try { var queryRequestOption = new QueryRequestOptions { PartitionKey = new PartitionKey(_partitionKeyStr) }; var feedIterator = _container .GetItemQueryStreamIterator(requestOptions: queryRequestOption); var jsonItemList = new List <IEnumerable <T> >(); while (feedIterator.HasMoreResults) { using var response = await feedIterator.ReadNextAsync(cancellationToken : ct); await using var cosmosItem = new CosmosItem <T>(); var items = await cosmosItem.GetItemsFromStream(response.Content, ct); if (items?.Any() ?? false) { jsonItemList.Add(items); } } return(jsonItemList.SelectMany(d => d)); } catch (Exception ex) { throw new CosmosClientException($"Unable to read partition: {_partitionKeyStr}.", ex); } }
public Task UpdatePlayer(string source, string playerId, ICollection <PointsEvent> newEvents) { var container = _containerFactory(_eventFeedOptions.ContainerName); var updatedPlayer = new CosmosItem { id = $"{source}_{playerId}", source = source, Events = newEvents }; return(container.UpsertItemAsync(updatedPlayer)); }
internal async Task <T> Read <T>(string id, CancellationToken ct) { ResponseMessage response; try { response = await _container.ReadItemStreamAsync(id, _partitionKey, cancellationToken : ct); } catch (Exception ex) { Debug.WriteLine($"{ex}"); throw new CosmosClientException($"Unable to read: {id} to Stream", ex); } try { if (response.IsSuccessStatusCode) { await using var cosmosItem = new CosmosItem <T>(); var item = await cosmosItem.GetItemFromStream(response.Content, ct); return(item); } } catch (Exception ex) { Debug.WriteLine($"{ex}"); throw new CosmosClientException($"Unable deserialize document with '{id}' from Stream.", ex); } if (response.StatusCode == HttpStatusCode.NotFound) { throw new DataException($"Document with id '{id}' not found"); } throw new CosmosClientException($"Unable to read: {id} to Stream. Status code: {response.StatusCode.ToString()}"); }