Skip to content

Creating Api Endpoints

LukePulverenti edited this page Mar 24, 2013 · 20 revisions

Plugins are able to create their own api endpoints, whether it be for their own consumption, or for the purposes of publishing them to UI developers. This will demonstrate how to build an api endpoint.

Media Browser's api layer is built with Service Stack, so their examples are directly applicable.

Create the Service

Create a class that implements IRestfulService.

You will then have to create request dto classes that describe the request. The following is a complete service example:

` [Route("/Weather", "GET")] [Api(Description = "Gets weather information for a given location")] public class GetWeather : IReturn { [ApiMember(Name = "Location", Description = "Us zip / City, State, Country / City, Country", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] public string Location { get; set; } }

public class WeatherService : IRestfulService
{
    public object Get(GetWeather request)
    {
        var result = Kernel.Instance.WeatherProviders.First().GetWeatherInfoAsync(request.Location, CancellationToken.None).Result;

        return result;
    }
}

`

WeatherService is the service that processes the request. GetWeather is api method. The route attribute describes the url and http method.

The Api and ApiMember attributes, as well as IReturn are all optional. They only serve to improve documentation.

Advanced Options

Clone this wiki locally