Example #1
0
        internal static async Task <MessageWithStatusCode> GetFileRequest(PyrusClient client, string url, string token)
        {
            using (var httpClient = client.ClientSettings.NewHttpClient(_fileRequestTimeout))
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgent);

                using (var response = await httpClient.GetAsync(url))
                {
                    var result = new MessageWithStatusCode {
                        StatusCode = response.StatusCode
                    };
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        result.Content = new MemoryStream();
                        await response.Content.CopyToAsync(result.Content);

                        result.Content.Position = 0;
                        result.FileName         = !String.IsNullOrWhiteSpace(response.Content.Headers.ContentDisposition.FileNameStar)
                                                        ? response.Content.Headers.ContentDisposition.FileNameStar
                                                        : response.Content.Headers.ContentDisposition.FileName;
                        result.FileName = result.FileName.TrimStart('"').TrimEnd('"');
                    }

                    return(result);
                }
            }
        }
Example #2
0
        internal static async Task <MessageWithStatusCode> DeleteRequest(PyrusClient client, string url, string token = null)
        {
            using (var httpClient = client.ClientSettings.NewHttpClient(_requestTimeout))
            {
                SetHeaders(httpClient, token, UserAgent);
                using (var response = await httpClient.DeleteAsync(url))
                {
                    var message = await response.Content.ReadAsStringAsync();

                    return(new MessageWithStatusCode {
                        Message = message, StatusCode = response.StatusCode, ResponseMessage = response
                    });
                }
            }
        }
Example #3
0
        internal static async Task <MessageWithStatusCode> PutRequest(PyrusClient client, string url, object request, string token = null)
        {
            using (var httpClient = client.ClientSettings.NewHttpClient(_requestTimeout))
            {
                SetHeaders(httpClient, token, UserAgent);
                using (var response = await httpClient.PutAsync(url,
                                                                new StringContent(
                                                                    JsonConvert.SerializeObject(request,
                                                                                                JsonSerializerSettings),
                                                                    Encoding.UTF8, "application/json")))
                {
                    var message = await response.Content.ReadAsStringAsync();

                    return(new MessageWithStatusCode {
                        Message = message, StatusCode = response.StatusCode, ResponseMessage = response
                    });
                }
            }
        }
Example #4
0
        internal static async Task <MessageWithStatusCode> DeleteRequest(PyrusClient client, string url, object request, string token = null)
        {
            using (var httpClient = client.ClientSettings.NewHttpClient(_requestTimeout))
                using (var httpRequest = new HttpRequestMessage())
                {
                    SetHeaders(httpClient, token, UserAgent);

                    httpRequest.Content    = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
                    httpRequest.Method     = HttpMethod.Delete;
                    httpRequest.RequestUri = new Uri(url);

                    using (var response = await httpClient.SendAsync(httpRequest))
                    {
                        var message = await response.Content.ReadAsStringAsync();

                        return(new MessageWithStatusCode {
                            Message = message, StatusCode = response.StatusCode, ResponseMessage = response
                        });
                    }
                }
        }
Example #5
0
        internal static async Task <MessageWithStatusCode> PostFileRequest(PyrusClient client, string url, NoDisposeStreamWrapperFactory streamFactory, string fileName, string token)
        {
            using (var httpClient = client.ClientSettings.NewHttpClient(_fileRequestTimeout))
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                httpClient.DefaultRequestHeaders.Add("ContentType", "multipart/form-data");
                httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgent);

                var streamContent = new StreamContent(streamFactory.Create());

                var multipart = new MultipartFormDataContent {
                    { streamContent, "file", $"{fileName}" }
                };
                using (var response = await httpClient.PostAsync(url, multipart))
                {
                    var message = await response.Content.ReadAsStringAsync();

                    return(new MessageWithStatusCode {
                        Message = message, StatusCode = response.StatusCode, ResponseMessage = response
                    });
                }
            }
        }