示例#1
0
        private async Task <HttpResponseMessage> ContainerGetRequestAsync(
            Int64 containerId,
            String itemPath,
            String contentType,
            CancellationToken cancellationToken,
            Guid scopeIdentifier,
            Object userState = null)
        {
            if (containerId < 1)
            {
                throw new ArgumentException(WebApiResources.ContainerIdMustBeGreaterThanZero(), "containerId");
            }

            List <KeyValuePair <String, String> > query = AppendItemQueryString(itemPath, scopeIdentifier);
            HttpRequestMessage requestMessage           = await CreateRequestMessageAsync(HttpMethod.Get, FileContainerResourceIds.FileContainer, routeValues : new { containerId = containerId }, version : s_currentApiVersion, queryParameters : query, userState : userState, cancellationToken : cancellationToken).ConfigureAwait(false);

            if (!String.IsNullOrEmpty(contentType))
            {
                requestMessage.Headers.Accept.Clear();
                var header = new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType);
                header.Parameters.Add(new System.Net.Http.Headers.NameValueHeaderValue(ApiResourceVersionExtensions.c_apiVersionHeaderKey, "1.0"));
                header.Parameters.Add(new System.Net.Http.Headers.NameValueHeaderValue(ApiResourceVersionExtensions.c_legacyResourceVersionHeaderKey, "1"));
                requestMessage.Headers.Accept.Add(header);
            }

            return(await SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, userState, cancellationToken).ConfigureAwait(false));
        }
示例#2
0
 public MediaTypeWithQualityHeaderValue(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue containedObject)
 {
     if ((containedObject == null))
     {
         throw new System.ArgumentNullException("containedObject");
     }
     this.containedObject = containedObject;
 }
        //public async Task<HttpResponseMessage> PostFormData()
        public Task<HttpResponseMessage> PostFormData()
        {
            string root = HttpContext.Current.Server.MapPath("~/App_Data");
            /*StreamWriter log1;
            string file1 = Path.Combine(@"d:\inetpub\wwwroot\rapportfrastedet\app_data\", "log1.txt");
            if (!File.Exists(file1))
            {
                log1 = new StreamWriter(file1);
            }
            else
            {
                log1 = File.AppendText(file1);
            }

            // Write to the file:
            log1.WriteLine(DateTime.Now);
            log1.WriteLine(root);

            // Close the stream:
            log1.Close();
            */
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = new MultipartFormDataStreamProvider(root);

            /**/
            // Read the form data.
            //await Request.Content.ReadAsMultipartAsync(provider);
            return Request.Content.ReadAsMultipartAsync(provider).ContinueWith(data =>
            {//.Wait();

                // This illustrates how to get the file names.
                /*foreach (MultipartFileData file in provider.FileData)
                {
                    Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                    Trace.WriteLine("Server file path: " + file.LocalFileName);
                }*/
                try
                {
                    HttpContextWrapper http = (HttpContextWrapper)Request.Properties["MS_HttpContext"];
                    HttpContext.Current = http.ApplicationInstance.Context;
                    RepositorySaveData r = new RepositorySaveData();
                    DataCreateModel dataCreateModel = r.CreateModel(data.Result.FormData, data.Result.FileData, Operation.Create);
                    DataViewModel model = dataCreateModel.Model;

                    foreach (Field field in model.Form.Fields)
                    {
                        if (field.FieldTypeId == 11)
                            field.Data = http.Request.UserHostAddress;
                    }
                    RepositoryMapguide repositoryMapGuide = new RepositoryMapguide();
                    model = repositoryMapGuide.Add(model);
                    dataCreateModel.Model = model;
                    repositoryMapGuide.SendEmails(dataCreateModel);
                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                catch (System.Exception e)
                {
                    StreamWriter log;
                    string file = Path.Combine(root, "log.txt");
                    if (!File.Exists(file))
                    {
                        log = new StreamWriter(file);
                    }
                    else
                    {
                        log = File.AppendText(file);
                    }

                    // Write to the file:
                    log.WriteLine(DateTime.Now);
                    log.WriteLine(e.Message);
                    log.WriteLine(e.StackTrace);

                    // Close the stream:
                    log.Close();
                    HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
                    System.Net.Http.Headers.MediaTypeWithQualityHeaderValue json = new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json");
                    System.Net.Http.Headers.MediaTypeWithQualityHeaderValue plain = new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/plain");
                    if (!Request.Headers.Accept.Contains(json))
                    {
                        r.Content.Headers.ContentType = plain;
                    }
                    return r;

                }
            });
示例#4
0
        private static void GenerateGithubReport()
        {
            var client = new HttpClient();

            var mediaTypeWithQualityHeaderValue =
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(
                    "application/vnd.github.cloak-preview+json");
            var typeWithQualityHeaderValue =
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(
                    "application/vnd.github.groot-preview+json");

            var results = new List <Result>();

            for (int i = 0; i <= 9; i++)
            {
                client = new HttpClient();
                client.DefaultRequestHeaders.UserAgent.Add(
                    new System.Net.Http.Headers.ProductInfoHeaderValue("AppName", "1.0"));
                client.DefaultRequestHeaders.Accept.Add(mediaTypeWithQualityHeaderValue);
                client.DefaultRequestHeaders.Authorization =
                    new System.Net.Http.Headers.AuthenticationHeaderValue("Token",
                                                                          "");

                var result1 = new Rootobject
                {
                    items = new Item[] { }
                };
                var formattableString = "";
                try
                {
                    formattableString =
                        $"https://api.github.com/search/commits?q=org:eg-brs+author:DanielDziubecki+committer-date:2020-01-01..2020-12-31&sort=committer-date&order=asc&page={i}&per_page=100";
                    // result1 = client.GetFromJsonAsync<Rootobject>(formattableString).GetAwaiter().GetResult();

                    var rawRes = client.GetAsync(formattableString).GetAwaiter().GetResult();
                    if (rawRes.IsSuccessStatusCode)
                    {
                        result1 = rawRes.Content.ReadFromJsonAsync <Rootobject>().GetAwaiter().GetResult();
                    }
                    else
                    {
                        Console.WriteLine(formattableString);
                        Console.WriteLine($"Status: {rawRes.StatusCode}");
                        Console.WriteLine($"Reason: {rawRes.ReasonPhrase}");
                        Console.WriteLine($"Content: {rawRes?.Content.ReadAsStringAsync().Result}");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Console.WriteLine(formattableString);
                }

                foreach (var result1Item in result1.items)
                {
                    Task.Delay(500).GetAwaiter().GetResult();
                    client = new HttpClient();
                    client.DefaultRequestHeaders.UserAgent.Add(
                        new System.Net.Http.Headers.ProductInfoHeaderValue("AppName", "1.0"));
                    client.DefaultRequestHeaders.Authorization =
                        new System.Net.Http.Headers.AuthenticationHeaderValue("Token",
                                                                              "");
                    client.DefaultRequestHeaders.Accept.Add(typeWithQualityHeaderValue);
                    var    res2       = new Class1[] { };
                    string requestUri = "";
                    try
                    {
                        requestUri =
                            $"https://api.github.com/repos/EG-BRS/{result1Item.repository.name}/commits/{result1Item.sha}/pulls";
                        var rawRes = client.GetAsync(requestUri).GetAwaiter().GetResult();
                        if (rawRes.IsSuccessStatusCode)
                        {
                            res2 = rawRes.Content.ReadFromJsonAsync <Class1[]>().GetAwaiter().GetResult();
                        }
                        else
                        {
                            Console.WriteLine(requestUri);
                            Console.WriteLine($"Status: {rawRes.StatusCode}");
                            Console.WriteLine($"Reason: {rawRes.ReasonPhrase}");
                            Console.WriteLine($"Content: {rawRes?.Content.ReadAsStringAsync().Result}");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(requestUri);
                        Console.WriteLine(e.Message);
                    }


                    var result = new Result
                    {
                        Project = result1Item.repository.name,
                        Branch  = res2.Length == 0
                            ? "probably master"
                            : string.Join(Environment.NewLine, res2.Select(class1 => class1.head.label)),
                        Comment  = result1Item.commit.message,
                        DateTime = result1Item.commit.author.date,
                        Sha      = result1Item.sha,
                        PrUrl    = res2.Length == 0
                            ? ""
                            : string.Join(Environment.NewLine, res2.Select(class1 => class1.url))
                    };

                    results.Add(result);
                }
            }

            var _memoryStream = new MemoryStream();
            var _streamWriter = new StreamWriter(_memoryStream);
            var config        = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                Delimiter = ";"
            };
            var _csvWriter = new CsvWriter(_streamWriter, config);

            _csvWriter.WriteRecordsAsync(results.OrderBy(result => result.DateTime));
            _streamWriter.FlushAsync();
            _memoryStream.Seek(0, SeekOrigin.Begin);


            using (var fileStream = File.Create(Directory.GetCurrentDirectory() + "report.csv"))
            {
                _memoryStream.CopyTo(fileStream);
            }
        }
示例#5
0
 public static bool TryParse(string input, out System.Net.Http.Headers.MediaTypeWithQualityHeaderValue parsedValue)
 {
     throw null;
 }