public async Task <BatchResponse <T>[]> BatchGet <T>(string endpoint, object queryParams = null,
                                                             RestRequestConfig restRequestConfig = null)
        {
            // if no multiple IDs specified
            if (!endpoint.Contains(","))
            {
                throw new ArgumentException(
                          "In order to make a BatchGet, endpoint should contain multiple IDs delimited by ','");
            }

            var httpResponseMessage = await Get <HttpResponseMessage>(endpoint, queryParams, restRequestConfig);

            var multipart = await httpResponseMessage.Content.ReadAsMultipartAsync();

            var summariesString = await multipart.Contents.First().ReadAsStringAsync();

            var batchSummaries = JsonConvert.DeserializeObject <BatchSummaries>(summariesString);
            var result         = new List <BatchResponse <T> >();

            for (var i = 0; i < batchSummaries.response.Length; i++)
            {
                var batchSummary  = batchSummaries.response[i];
                var batchResponse = new BatchResponse <T> {
                    summary = batchSummary
                };
                result.Add(batchResponse);
                var responseString = await multipart.Contents[i + 1].ReadAsStringAsync();
                if (batchSummary.isError)
                {
                    batchResponse.error = JsonConvert.DeserializeObject <ErrorResponse>(responseString);
                }
                else
                {
                    batchResponse.content = JsonConvert.DeserializeObject <T>(responseString);
                }
            }

            return(result.ToArray());
        }
        public async Task <HttpResponseMessage> Request(HttpRequestMessage httpRequestMessage,
                                                        RestRequestConfig restRequestConfig = null)
        {
            restRequestConfig = restRequestConfig ?? RestRequestConfig.DefaultInstance;

            httpRequestMessage.Headers.Add("X-User-Agent", $"{appName}/{appVersion} RingCentral.Net/5.1.0");
            httpRequestMessage.Headers.Authorization =
                BasicAuthPaths.Contains(httpRequestMessage.RequestUri.AbsolutePath)
                    ? new AuthenticationHeaderValue("Basic",
                                                    Convert.ToBase64String(
                                                        Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}")))
                    : new AuthenticationHeaderValue("Bearer", token.access_token);

            var httpResponseMessage =
                await httpClient.SendAsync(httpRequestMessage, restRequestConfig.cancellationToken);

            if (!httpResponseMessage.IsSuccessStatusCode)
            {
                throw new RestException(httpResponseMessage, httpRequestMessage);
            }

            return(httpResponseMessage);
        }
 public async Task <T> Delete <T>(string endpoint, object queryParams = null,
                                  RestRequestConfig restRequestConfig = null)
 {
     return(await Request <T>(HttpMethod.Delete, endpoint, null, queryParams, restRequestConfig));
 }
        public async Task <T> Request <T>(HttpMethod httpMethod, string endpoint,
                                          object content = null, object queryParams = null, RestRequestConfig restRequestConfig = null)
        {
            var httpResponseMessage = await Request(httpMethod, endpoint, content, queryParams, restRequestConfig);

            if (typeof(T) == typeof(HttpResponseMessage))
            {
                return((T)(object)httpResponseMessage);
            }

            if (typeof(T) == typeof(byte[]))
            {
                var bytes = await httpResponseMessage.Content.ReadAsByteArrayAsync();

                return((T)(object)bytes);
            }

            var httpContent = await httpResponseMessage.Content.ReadAsStringAsync();

            if (typeof(T) == typeof(string))
            {
                return((T)(object)httpContent);
            }

            try
            {
                return(JsonConvert.DeserializeObject <T>(httpContent));
            }
            catch (Exception e)
            {
                if (e is JsonReaderException || e is JsonSerializationException)
                {
                    throw new JsonDeserializeException(
                              $"Unable to deserialize json string to type {typeof(T)}\n\n{e.Message}\n\nJson string: {httpContent}",
                              e);
                }

                throw;
            }
        }
 public async Task <HttpResponseMessage> Delete(string endpoint, object queryParams = null,
                                                RestRequestConfig restRequestConfig = null)
 {
     return(await Request(HttpMethod.Delete, endpoint, null, queryParams, restRequestConfig));
 }
        public async Task <HttpResponseMessage> Request(HttpMethod httpMethod, string endpoint,
                                                        object content = null, object queryParams = null, RestRequestConfig restRequestConfig = null)
        {
            HttpContent httpContent = null;

            if (content is HttpContent)
            {
                httpContent = (HttpContent)content;
            }
            else if (content is string s)
            {
                httpContent = new StringContent(s);
            }
            else if (content != null)
            {
                httpContent = new StringContent(
                    JsonConvert.SerializeObject(content, Formatting.None, jsonSerializerSettings), Encoding.UTF8,
                    "application/json"
                    );
            }

            UriBuilder uriBuilder = null;

            if (endpoint.StartsWith("https://"))
            {
                uriBuilder = new UriBuilder(endpoint);
            }
            else
            {
                uriBuilder = new UriBuilder(server)
                {
                    Path = endpoint
                };
            }

            if (queryParams != null)
            {
                var fields = Utils.GetPairs(queryParams).Select(t =>
                {
                    if (t.value.GetType().IsArray)
                    {
                        return(string.Join("&",
                                           (t.value as object[]).Select(o => $"{t.name}={Uri.EscapeUriString(o.ToString())}")
                                           .ToArray()));
                    }
                    else
                    {
                        return($"{t.name}={Uri.EscapeUriString(t.value.ToString())}");
                    }
                });
                uriBuilder.Query = string.Join("&", fields);
            }

            var httpRequestMessage = new HttpRequestMessage
            {
                Method     = httpMethod,
                RequestUri = uriBuilder.Uri,
                Content    = httpContent
            };

            return(await extensibleRequest(httpRequestMessage, restRequestConfig ?? RestRequestConfig.DefaultInstance));
        }
 public async Task <T> Patch <T>(string endpoint, object content     = null, object queryParams = null,
                                 RestRequestConfig restRequestConfig = null)
 {
     return(await Request <T>(new HttpMethod("PATCH"), endpoint, content, queryParams, restRequestConfig));
 }
 public async Task <T> Put <T>(string endpoint, object content     = null, object queryParams = null,
                               RestRequestConfig restRequestConfig = null)
 {
     return(await Request <T>(HttpMethod.Put, endpoint, content, queryParams, restRequestConfig));
 }
 public async Task <HttpResponseMessage> Post(string endpoint, object content     = null, object queryParams = null,
                                              RestRequestConfig restRequestConfig = null)
 {
     return(await Request(HttpMethod.Post, endpoint, content, queryParams, restRequestConfig));
 }