GitHub Repos


namespace consoleapp1 
{
  class Program
  {
    private static readonly
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

//--------------------------------------------------------------------------------------
    static async
    System.Threading.Tasks.Task
    Main(string[] args)
    {
      var repositories = await Method_ProcessRepositories();

      foreach (Class_Repository repo in repositories)
      {
        Console.WriteLine(repo.Property_Name);
        Console.WriteLine(repo.Property_Description);
        Console.WriteLine(repo.Property_GitHubHomeUrl);
        Console.WriteLine();
      }
    }
//--------------------------------------------------------------------------------------
    private static async
    System.Threading.Tasks.Task<System.Collections.Generic.List<Class_Repository>>
    Method_ProcessRepositories()
    {
      client.DefaultRequestHeaders.Accept.Clear();

      client.DefaultRequestHeaders.Accept.Add(
          new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));

      client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

      var streamTask = client.GetStreamAsync("https://api.github.com/users/officeaddins/repos");

      var repositories =
      await System.Text.Json.JsonSerializer.DeserializeAsync<System.Collections.Generic.List<Class_Repository>>(await streamTask);

      if (repositories is not null)
      {
        return repositories;
      }
      else
      {
        return new System.Collections.Generic.List<Class_Repository>();
      }
    }
  }
//--------------------------------------------------------------------------------------
  public class
  Class_Repository
  {
    [System.Text.Json.Serialization.JsonPropertyName("name")]
    public string? Property_Name { get; set; }

    [System.Text.Json.Serialization.JsonPropertyName("description")]
    public string? Property_Description { get; set; }

    [System.Text.Json.Serialization.JsonPropertyName("html_url")]
    public System.Uri? Property_GitHubHomeUrl { get; set; }

  }
}
//--------------------------------------------------------------------------------------


public class Class1 
    {
        private const string URL = "https://sub.domain.com/objects.json?api_key=123";
        private const string DATA = @"{""object"":{""name"":""Name""}}";

        static void Main(string[] args)
        {
            Class1.CreateObject();
        }

        private static void CreateObject()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = DATA.Length;
            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
            requestWriter.Write(DATA);
            requestWriter.Close();

             try {
                WebResponse webResponse = request.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();
                Console.Out.WriteLine(response);
                responseReader.Close();
            } catch (Exception e) {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(e.Message);
            }

        }
    }



© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext