Skip to content

3. Dynamic Endpoints

Maciej Chmiel edited this page Apr 25, 2023 · 1 revision

Welcome to the second getting started tutorial. Here you will learn how to use dynamic endpoints for your APIs.

What are we building today?

You will create an API for clients to get weather data from server.

Prerequisites

Step 1 - Create a new console application, install SAPI and initialize it

I won't go into the details since you should read this tutorial after the first one.

// Program.cs
using SAPI;

namespace WeatherAPI;

public class WeatherAPI
{
	public static void Main()
	{
		Server sapi = new();
		
		// Leave some space here - we will come back here later
		
		sapi.Start();
	}
}

Step 2 - Create your data model & mock database

Create a model for our data.

// Models/City.cs
namespace WeatherAPI.Models;

public class City
{
	public string Name { get; }
	public string Country { get; }
	public short Temperature { get; }
	public string WeatherType { get; }
	public ushort Humidity { get; }
	public ushort Pressure { get; }
	public ushort Visibility { get; }

	public City(string name, string country, short temperature, string weatherType, ushort humidity, ushort pressure, ushort visibility)
	{
		Name = name;
		Country = country;
		Temperature = temperature;
		WeatherType = weatherType;
		Humidity = humidity;
		Pressure = pressure;
		Visibility = visibility;
	}
}

Now create a service for managing our data.

// Services/Database.cs
using WeatherAPI.Models;

namespace WeatherAPI.Services;

public static class Database
{
	private static List<City> cities = new()
	{
		new City("Sydney", "Australia", 22, "Clouds and sun", 52, 1021, 16),
		new City("Vancouver", "Canada", 4, "Light Rain", 59, 1007, 13),
		new City("New_Delhi", "India", 21, "Clear", 74, 1014, 10),
		new City("Tokyo", "Japan", 14, "Sunny", 67, 1018, 18),
		new City("Krakow", "Poland", 3, "Fog", 100, 1019, 8),
		new City("Pretoria", "South_Africa", 14, "Cloudy", 90, 1016, 5),
		new City("New_York", "USA", 22, "Clear", 15, 1023, 16)
	};

	public static List<City> GetCities() => cities;
}

Step 3 - Create a new endpoint that will return data about the weather to our client.

// Endpoints/GetWeather.cs
using System.Net;
using SAPI.Endpoints;
using SAPI.Utilities;
using WeatherAPI.Models;
using WeatherAPI.Services;


namespace WeatherAPI.Endpoints;

public class GetWeather : IEndpoint
{

	public string url { get; } = "get-weather/:country/:city";
	public Method method { get; } = Method.GET;

	private List<City> cities;
	
	public void Task(ref HttpListenerRequest request, ref HttpListenerResponse response, Dictionary<string, string> parameters)
	{
		cities = Database.GetCities();

		foreach (City city in cities)
		{
			if (city.Country == parameters["country"] && city.Name == parameters["city"])
				Utilities.JsonResponse(city, ref response);
		}
	}
}

We are iterating over all cities to find which corresponds to provided parameters.

Remember to add our new endpoint to Program.cs.

// Program.cs
using SAPI;
using WeatherAPI.Endpoints;

namespace ServerAPI;

public class ServerAPI
{
	public static void Main()
	{
		Server sapi = new();

		sapi.MountEndpoint(new GetWeather());
		
		sapi.Start();
	}
}

Step 4 - Check our results

Start up your project, and in your REST client of choice send a few GET requests on http://localhost:8000/get-weather/<country-name>/<city-name>. The country-name and city-name needs to match a record in Database.cs.

weather request

Summary

That's it - you now have an API with dynamic endpoints - isn't that cool?