Exemple #1
0
        async Task <ImageResult> IImageParsingStrategy.ParseContentAsync(string keyword, int index)
        {
            if (string.IsNullOrWhiteSpace(keyword))
            {
                ThrowHelper.Argument($"'{nameof(keyword)}' cannot be null or whitespace", nameof(keyword));
            }

            if (index < 0)
            {
                ThrowHelper.ArgumentOutOfRange(nameof(index), index, "Value must be 0 or greater!");
            }

            var uri = new Uri(_url).AddParameter("query", keyword)
                      .AddParameter("per_page", 1)
                      .AddParameter("page", index + 1);

            var response = await _httpClient.GetAsync <Response>(uri);

            var result   = response.Result.Results.First();
            var download = await _httpClient.GetAsync(result.Links.Download);

            var stream = await download.Content.ReadAsStreamAsync();

            var contentType = download.Content.Headers.ContentType.MediaType;

            return(new ImageResult {
                Stream = stream, ContentType = contentType
            });
        }
Exemple #2
0
        IEnumerable <ParsedImage> IImageParsingStrategy.Parse(ParseRequest request, IProgress <ParseProgress> progress)
        {
            if (request is null)
            {
                ThrowHelper.Argument($"'{nameof(request)}' cannot be null or whitespace", nameof(request));
            }

            var collection = AsyncHelpers.RunSync(() => ((IImageParsingStrategy)this).ParseAsync(request, progress).ToListAsync());

            return(collection);
        }
Exemple #3
0
 public Message AddLocationQuery(String query)
 {
     if (query == null)
     {
         throw ThrowHelper.ArgumentNull("query");
     }
     if (query.Length > 255)
     {
         throw ThrowHelper.Argument("query", "Location Query option's length must be between 0 and 255 inclusive");
     }
     return(AddOption(Option.Create(OptionType.LocationQuery, query)));
 }
Exemple #4
0
 public Message AddLocationPath(String path)
 {
     if (path == null)
     {
         throw ThrowHelper.ArgumentNull("path");
     }
     if (path.Length > 255)
     {
         throw ThrowHelper.Argument("path", "Location Path option's length must be between 0 and 255 inclusive");
     }
     return(AddOption(Option.Create(OptionType.LocationPath, path)));
 }
Exemple #5
0
 public Message AddIfMatch(Byte[] opaque)
 {
     if (opaque == null)
     {
         throw ThrowHelper.ArgumentNull("opaque");
     }
     if (opaque.Length > 8)
     {
         throw ThrowHelper.Argument("opaque", "Content of If-Match option is too large: " + ByteArrayUtils.ToHexString(opaque));
     }
     return(AddOption(Option.Create(OptionType.IfMatch, opaque)));
 }
 /// <inheritdoc/>
 public void DeliverResponse(Exchange exchange, Response response)
 {
     if (exchange == null)
     {
         throw ThrowHelper.ArgumentNull("exchange");
     }
     if (response == null)
     {
         throw ThrowHelper.ArgumentNull("response");
     }
     if (exchange.Request == null)
     {
         throw ThrowHelper.Argument("exchange", "Request should not be empty.");
     }
     exchange.Request.Response = response;
 }
Exemple #7
0
 /// <summary>
 /// Calculates the value used in the extended option fields as specified
 /// in draft-ietf-core-coap-14, section 3.1.
 /// </summary>
 /// <param name="nibble">the 4-bit option header value</param>
 /// <param name="datagram">the datagram</param>
 /// <returns>the value calculated from the nibble and the extended option value</returns>
 private static Int32 GetValueFromOptionNibble(Int32 nibble, DatagramReader datagram)
 {
     if (nibble < 13)
     {
         return(nibble);
     }
     else if (nibble == 13)
     {
         return(datagram.Read(8) + 13);
     }
     else if (nibble == 14)
     {
         return(datagram.Read(16) + 269);
     }
     else
     {
         throw ThrowHelper.Argument("nibble", "Unsupported option delta " + nibble);
     }
 }
Exemple #8
0
 /// <summary>
 /// Returns the 4-bit option header value.
 /// </summary>
 /// <param name="optionValue">the option value (delta or length) to be encoded</param>
 /// <returns>the 4-bit option header value</returns>
 private static Int32 GetOptionNibble(Int32 optionValue)
 {
     if (optionValue <= 12)
     {
         return(optionValue);
     }
     else if (optionValue <= 255 + 13)
     {
         return(13);
     }
     else if (optionValue <= 65535 + 269)
     {
         return(14);
     }
     else
     {
         throw ThrowHelper.Argument("optionValue", "Unsupported option delta " + optionValue);
     }
 }