Ejemplo n.º 1
0
        /// <summary>
        /// Sends a POST request to an API function using a <paramref name="token"/>, and returns
        /// the response as a JSON string.
        /// </summary>
        /// <param name="function">
        /// The name of the function.
        /// </param>
        /// <param name="args">
        /// A set of parameters to pass along with the request.
        /// </param>
        /// <param name="token">
        /// The auth token.
        /// </param>
        /// <returns>
        /// Upon success, the JSON string; otherwise, <c>null</c>.
        /// </returns>
        public async Task <string> PostToJsonAsync(string function, RequestParameters args, string token)
        {
            Contract.Requires <ArgumentNullException>(function != null);
            Contract.Requires <ArgumentNullException>(token != null);

            var response = await PostAsync(function, args, token);

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            // Obtain the JSON data (and decompress it if it is gzipped).
            string jsonData;

            if (response.Content.Headers.ContentEncoding.Contains(HttpContentCodingHeaderValue.Parse("gzip")))
            {
                var compressedData = (await response.Content.ReadAsBufferAsync()).ToArray();
                jsonData = await Gzip.DecompressAsync(compressedData, Encoding.UTF8);
            }
            else
            {
                jsonData = await response.Content.ReadAsStringAsync();
            }
            Debug.WriteLine("[EndpointManager] Received JSON data: {0}", jsonData);

            // Return the JSON data.
            return(jsonData);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sends a GET request to an API function, and receives binary data from the server.
        /// </summary>
        /// <param name="function">
        /// The name of the function.
        /// </param>
        /// <returns>
        /// Upon success, a byte array containing the data received from the server; otherwise,
        /// <c>null</c>.
        /// </returns>
        public async Task <byte[]> GetAsync(string function)
        {
            Contract.Requires <ArgumentNullException>(function != null);

            var response = await GetAsync(function, null);

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            // Obtain the binary data (and decompress it if it is gzipped).
            var data = (await response.Content.ReadAsBufferAsync()).ToArray();

            if (response.Content.Headers.ContentEncoding.Contains(HttpContentCodingHeaderValue.Parse("gzip")))
            {
                data = await Gzip.DecompressAsync(data);
            }
            Debug.WriteLine("[EndpointManager] Received binary data ({0} bytes)", data.Length);

            return(data);
        }