Link Search Menu Expand Document

Exercise: Build an HTTP API Client

Create a .NET console application that will add a randomly-generated vehicles to the Autobarn platform every time the user presses a key.

Dylan’s server is running at https://autobarn.dev

  • The modelCode should be selected at random from the codes returned by the API endpoint.
  • The list of model codes should be cached so it’s only retrieved once, when your application first starts
  • The year should be a random integer between 1960 and 2025
  • The color should be one of the CSS named colours:
  • The registration should be 8 randomly generated characters in the range A-Z 0-9.

Hints

  • Use the System.Net.HttpClient class to make HTTP requests and handle responses; you’ll find lots of information about how to use this class at https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient

  • Wrap the raw HttpClient in a wrapper class which exposes strongly-typed methods that abstract away the HTTP details in favour of business-level operations:

    public class AutobarnApiClient(HttpClient http) {
      
    	public string[] ListModelCodes() {
            // TODO: list all model codes from GET /api/models
        }
      
        public VehicleDto CreateRandomVehicle() {
            // TODO: generate a random vehicle 
    	}
    }
    
  • System.Random.Shared provides an instance of the System.Random random number generator you can use to generate random numbers; use the .Next(10) method to generate a random integer between 0 and 10