Ejemplo n.º 1
0
 internal PageableResult(
     CanvasApiClient apiClient,
     PageLinks pageLinks
     )
 {
     this._initalPageLinks = pageLinks ?? throw new ArgumentNullException(nameof(pageLinks), "Paging details not supplied.");
     this._ApiClient       = apiClient;
 }
Ejemplo n.º 2
0
 internal PageableResult(
     IEnumerable <TResult> initialBuffer,
     CanvasApiClient apiClient,
     PageLinks pageLinks
     ) : this(apiClient, pageLinks)
 {
     this.InitialBuffer = initialBuffer?.ToArray() ?? Array.Empty <TResult>();
 }
Ejemplo n.º 3
0
        internal async Task <IEnumerable <TResult> > PagableApiOperation <TResult, TBody>(
            HttpMethod verb,
            string url,
            TBody body,
            PagingOptions pagingOptions         = null,
            CancellationToken cancellationToken = default)
            where TBody : class
        {
            var pageLinks     = new PageLinks((pagingOptions ?? this.DefaultPagingOptions)?.AddPagingUrl(url) ?? url);
            var initialBuffer = await this.ApiOperation <IEnumerable <TResult>, TBody>(verb, pageLinks.OriginalUrl, body, pageLinks, cancellationToken);

            return(new PageableResult <TResult>(initialBuffer, this, pageLinks));
        }
Ejemplo n.º 4
0
        internal async Task <TResult> ApiOperation <TResult, TBody>(
            HttpMethod verb,
            string url,
            TBody body,
            PageLinks pageLinks = null,
            CancellationToken cancellationToken = default)
            where TBody : class
        {
            var httpMessage = this.GenerateHttpRequest(verb, url, body);

            var result = await this.Client
                         .SendAsync(httpMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken);


            if (result.IsSuccessStatusCode)
            {
                var content = await result.Content.ReadAsStringAsync();

                if (pageLinks != null)
                {
                    pageLinks.SetHeaders(result.Headers);
                }

                await using var contentStream = await result.Content.ReadAsStreamAsync();

                using var streamReader = new StreamReader(contentStream);
                using var reader       = new JsonTextReader(streamReader);

                var serializedCollection = new JsonSerializer()
                {
                    ContractResolver = new DefaultNemesContractResolver()
                }.Deserialize <TResult>(reader);

                return(serializedCollection ?? default(TResult));
            }

            throw result.ToException();
        }