Example #1
0
        public virtual async Task Delete(object[] keys, ODataContext?oDataContext = default, CancellationToken cancellationToken = default)
        {
            if (keys is null)
            {
                throw new ArgumentNullException(nameof(keys));
            }

            string qs = oDataContext?.Query is not null ? $"?{oDataContext.Query}" : string.Empty;

            (await HttpClient.DeleteAsync($"odata/{ODataRoute}/{ControllerName}({string.Join(",", keys)}){qs}", cancellationToken).ConfigureAwait(false))
            .EnsureSuccessStatusCode();
        }
Example #2
0
        public virtual async Task <List <TDto> > Get(ODataContext?oDataContext = default, CancellationToken cancellationToken = default)
        {
            string qs = oDataContext?.Query is not null ? $"?{oDataContext.Query}" : string.Empty;

            using Stream responseStream = await(await HttpClient.GetAsync($"odata/{ODataRoute}/{ControllerName}(){qs}", cancellationToken).ConfigureAwait(false))
                                          .EnsureSuccessStatusCode()
                                          .Content
                                          .ReadAsStreamAsync().ConfigureAwait(false);

            List <TDto> odataResponse = await DeserializeAsync <List <TDto> >(responseStream, oDataContext, cancellationToken).ConfigureAwait(false);

            return(odataResponse);
        }
Example #3
0
        public virtual async Task <TDto> Get(object[] keys, ODataContext?oDataContext = default, CancellationToken cancellationToken = default)
        {
            if (keys is null)
            {
                throw new ArgumentNullException(nameof(keys));
            }

            string qs = oDataContext?.Query is not null ? $"?{oDataContext.Query}" : string.Empty;

            using Stream responseStream = await(await HttpClient.GetAsync($"odata/{ODataRoute}/{ControllerName}({string.Join(",", keys)}){qs}", cancellationToken).ConfigureAwait(false))
                                          .EnsureSuccessStatusCode()
                                          .Content
                                          .ReadAsStreamAsync(cancellationToken)
                                          .ConfigureAwait(false);

            return(await DeserializeAsync <TDto>(responseStream, null, cancellationToken).ConfigureAwait(false));
        }
Example #4
0
        public virtual async Task <T> DeserializeAsync <T>(Stream responseStream, ODataContext?context, CancellationToken cancellationToken)
        {
            using StreamReader streamReader = new StreamReader(responseStream);
            JsonElement json = await JsonSerializer.DeserializeAsync <JsonElement>(responseStream).ConfigureAwait(false);

            if (!json.TryGetProperty("value", out JsonElement _))
            {
                return(await json.ToObjectAsync <T>().ConfigureAwait(false));
            }

            ODataResponse <T> oDataResponse = await json.ToObjectAsync <ODataResponse <T> >().ConfigureAwait(false);

            if (context != null)
            {
                context.TotalCount = oDataResponse.TotalCount;
            }

            return(oDataResponse.Value);
        }
Example #5
0
        public virtual async Task <T> DeserializeAsync <T>(Stream responseStream, ODataContext?context, CancellationToken cancellationToken)
        {
            JsonSerializer serializer = JsonSerializer.CreateDefault(DefaultJsonContentFormatter.DeserializeSettings());

            using StreamReader streamReader = new StreamReader(responseStream);
            using JsonTextReader jsonReader = new JsonTextReader(streamReader);
            JToken json = await JToken.LoadAsync(jsonReader, cancellationToken).ConfigureAwait(false);

            if (json["value"] == null)
            {
                return(json.ToObject <T>(serializer));
            }

            ODataResponse <T> oDataResponse = json.ToObject <ODataResponse <T> >(serializer);

            if (context != null)
            {
                context.TotalCount = oDataResponse.TotalCount;
            }

            return(oDataResponse.Value);
        }
Example #6
0
 public virtual async Task <TDto> PartialUpdate(TDto dto, ODataContext?oDataContext = default, CancellationToken cancellationToken = default)
 {
     return(await SendAsync(DtoMetadataWorkspace.Current.GetKeys(dto), dto, new HttpMethod("Patch"), oDataContext, cancellationToken).ConfigureAwait(false));
 }
Example #7
0
 public virtual async Task <TDto> PartialUpdate(object[] keys, object dto, ODataContext?oDataContext = default, CancellationToken cancellationToken = default)
 {
     return(await SendAsync(keys, dto, new HttpMethod("Patch"), oDataContext, cancellationToken).ConfigureAwait(false));
 }
Example #8
0
 public virtual async Task <TDto> Create(TDto dto, ODataContext?oDataContext = default, CancellationToken cancellationToken = default)
 {
     return(await SendAsync(Array.Empty <object>(), dto, HttpMethod.Post, oDataContext, cancellationToken).ConfigureAwait(false));
 }
Example #9
0
        protected virtual async Task <TDto> SendAsync(object[] keys, object dto, HttpMethod method, ODataContext?oDataContext, CancellationToken cancellationToken)
        {
            if (dto is null)
            {
                throw new ArgumentNullException(nameof(dto));
            }

            string qs = oDataContext?.Query is not null ? $"?{oDataContext.Query}" : string.Empty;

            using StringContent content = new StringContent(DefaultJsonContentFormatter.Current.Serialize(dto), Encoding.UTF8, DefaultJsonContentFormatter.Current.ContentType);

            using HttpRequestMessage request = new HttpRequestMessage(method, $"odata/{ODataRoute}/{ControllerName}({string.Join(",", keys)}){qs}");

            request.Content = content;

            using Stream responseStream = await(await HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false))
                                          .EnsureSuccessStatusCode()
                                          .Content.
#if DotNetStandard2_0 || UWP || Android || iOS || DotNetStandard2_1
                                          ReadAsStreamAsync()
#else
                                          ReadAsStreamAsync(cancellationToken)
#endif
                                          .ConfigureAwait(false);

            return(await DeserializeAsync <TDto>(responseStream, null, cancellationToken).ConfigureAwait(false));
        }