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
modelCodeshould 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
yearshould be a random integer between 1960 and 2025 - The
colorshould be one of the CSS named colours: - The
registrationshould be 8 randomly generated characters in the range A-Z 0-9.
Hints
-
Use the
System.Net.HttpClientclass 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
HttpClientin 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.Sharedprovides an instance of theSystem.Randomrandom number generator you can use to generate random numbers; use the.Next(10)method to generate a random integer between 0 and 10