Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. namespace ConsoleApp3
  2. {
  3.  
  4.     public class JobsJson
  5.     {
  6.         public List<Job> Jobs { get; set; }
  7.     }
  8.     public class Job
  9.     {
  10.         public string Id { get; set; }
  11.         public string Title { get; set; }
  12.         public Location Location { get; set; }
  13.         public DateTime LastUpdated { get; set; }
  14.     }
  15.  
  16.     public class Location
  17.     {
  18.         public string Name { get; set; }
  19.     }
  20.  
  21.     class GreenhouseJobsClient
  22.     {
  23.         static HttpClient client = new HttpClient();
  24.  
  25.         static void ShowJobs(List<Job> jobs)
  26.         {
  27.             foreach (var job in jobs)
  28.             {
  29.                 Console.WriteLine($"Id: {job.Id}\tTitle: " +
  30.                                   $"{job.Title}\tLocation: {job.Location}\tLast Updated: {job.LastUpdated}");
  31.             }
  32.         }
  33.        
  34.  
  35.         static async Task<List<Job>> GetJobAsync(string path)
  36.         {
  37.             var jobs = new List<Job>();
  38.             HttpResponseMessage response = await client.GetAsync(path);
  39.  
  40.             if (response.IsSuccessStatusCode)
  41.             {
  42.                 var stringResponse = await response.Content.ReadAsStringAsync();
  43.                 var re = JsonConvert.DeserializeObject<JobsJson>(stringResponse);
  44.                 jobs = re.Jobs;
  45.             }
  46.             return jobs;
  47.         }
  48.  
  49.         static void Main()
  50.         {
  51.             RunAsync().GetAwaiter().GetResult();
  52.             Console.ReadLine();
  53.         }
  54.  
  55.         static async Task RunAsync()
  56.         {
  57.             // Update port # in the following line.
  58.             client.BaseAddress = new Uri("https://boards-api.greenhouse.io/v1/boards/");
  59.             client.DefaultRequestHeaders.Accept.Clear();
  60.             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  61.  
  62.             try
  63.             {
  64.                 // Get the product
  65.                 var jobs = await GetJobAsync("vaulttec/jobs");
  66.                 ShowJobs(jobs);
  67.             }
  68.             catch (Exception e)
  69.             {
  70.                 Console.WriteLine(e.Message);
  71.             }
  72.  
  73.             Console.ReadLine();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement