Orneholm.SverigesRadio is an unofficial wrapper of Sveriges Radio Open API (v2) for .NET. Built in C# targeting .NET Standard 2.0. Based on the public documentation.
- đ» Typed wrappers for the Swedish Radio Open API
- đ§ Cross platform: Targets .NET Standard 2.0 and .NET Standard 2.1 (Use from .NET Framework and .NET Core)
âïž Supports sorting, filtering, searching etc.
The API-wrapper supports all these methods:
- Programs
GetProgramAsync(...)
ListProgramsAsync(...)
ListProgramNewsAsync(...)
ListAllProgramsAsync(...)
(See Pagination with IAsyncEnumerable)
- Program Categories
GetProgramCategoryAsync(...)
ListProgramCategoriesAsync(...)
ListAllProgramCategoriesAsync(...)
(See Pagination with IAsyncEnumerable)
- Channels
GetChannelAsync(...)
ListChannelsAsync(...)
ListAllChannelsAsync(...)
(See Pagination with IAsyncEnumerable)
- Episodes
GetEpisodeAsync(...)
GetEpisodesAsync(...)
GetLatestEpisodeAsync(...)
ListEpisodesAsync(...)
ListAllEpisodesAsync(...)
(See Pagination with IAsyncEnumerable)SearchEpisodesAsync(...)
SearchAllEpisodesAsync(...)
(See Pagination with IAsyncEnumerable)ListEpisodeNewsAsync(...)
- Episode Groups
ListEpisodeGroupsAsync(...)
ListAllEpisodeGroupsAsync(...)
(See Pagination with IAsyncEnumerable)
- Broadcasts
ListBroadcastsAsync(...)
ListAllBroadcastsAsync(...)
(See Pagination with IAsyncEnumerable)
- Extra Broadcasts
ListExtraBroadcastsAsync(...)
ListAllExtraBroadcastsAsync(...)
(See Pagination with IAsyncEnumerable)
- Podfiles
GetPodfileAsync(...)
ListPodfilesAsync(...)
ListAllPodfilesAsync(...)
(See Pagination with IAsyncEnumerable)
- Audio Url Templates
ListOnDemandAudioTypesAsync(...)
ListLiveAudioTypesAsync(...)
Please start by reading the official documentation to get a basic understanding of the Swedish Radio open API: https://sverigesradio.se/api/documentation/v2/
Orneholm.SverigesRadio is distributed as packages on NuGet, install using the tool of your choice, for example dotnet cli.
dotnet add package Orneholm.SverigesRadio.Api
When used in a place where .NET can handle the lifecycle of HttpClient (like ASP.NET), let .NET inject the api client to cache the http client.
services.AddHttpClient(nameof(SverigesRadioApiClient), httpClient =>
{
httpClient.BaseAddress = SverigesRadioApiDefaults.ProductionApiBaseUrl;
});
services.AddTransient<ISverigesRadioApiClient>(s =>
{
var httpClient = s.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(SverigesRadioApiClient));
return new SverigesRadioApiClient(httpClient, new AudioSettings
{
AudioQuality = AudioQuality.High,
LiveAudioTemplateId = SverigesRadioApiIds.LiveAudioTemplates.MP3,
OnDemandAudioTemplateId = SverigesRadioApiIds.OnDemandAudioTemplates.Html5_Desktop
});
});
Create api client without DI:
var apiClient = SverigesRadioApiClient.CreateClient();
Override default audio settings:
var apiClient = SverigesRadioApiClient.CreateClient(new AudioSettings
{
OnDemandAudioTemplateId = SverigesRadioApiIds.OnDemandAudioTemplates.Html5_Desktop,
LiveAudioTemplateId = SverigesRadioApiIds.LiveAudioTemplates.MP3,
AudioQuality = AudioQuality.High
});
var programsResult = await apiClient.ListProgramsAsync();
foreach (var program in programsResult.Programs)
{
Console.WriteLine($"{program.Name} ({program.Id}): {program.Description}");
}
var episodeResult = await apiClient.GetLatestEpisodeAsync(SverigesRadioApiIds.Programs.P3_Dokumentar);
Console.WriteLine($"{episodeResult.Episode.Title} ({episodeResult.Episode.Id}): {episodeResult.Episode.Description}");
var podfilesResult = await apiClient.ListPodfilesAsync(SverigesRadioApiIds.Programs.Sa_Funkar_Det, ListPagination.TakeFirst(5));
foreach (var podfile in podfilesResult.Podfiles)
{
Console.WriteLine($"{podfile.Title} ({podfile.Id}): {podfile.Url}");
}
var episodeSearchResult = await apiClient.SearchEpisodesAsync("Microsoft");
foreach (var episode in episodeSearchResult.Episodes)
{
Console.WriteLine($"{episode.Title} ({episode.Id}) - {episode.Description}");
}
These were just brief samples. Explore the API to find the rest of the possibilities :)
If used from a runtime that supports .NET Standard 2.1, extensions are provided for the list calls to automatically do pagination using IAsyncEnumerable
, for example: ListAllProgramCategoriesAsync()
.
Under the hood the method will fetch 100 items at a time but wllow you to seemlesly enumrate over it.
Note: This will enumerate over all items, which could be millions. Use with care.
All methods take a *Request
object with the parameters for that specific endpoint. There are overloads on all of them for common scenarios. For example:
Shorthand for search episode:
var episodeSearchResult = await apiClient.SearchEpisodesAsync("Microsoft");
Full request object for search episode:
var episodeSearchResult = await apiClient.SearchEpisodesAsync(new EpisodeSearchRequest("Microsoft")
{
ChannelId = SverigesRadioApiIds.Channels.P3_Rikskanal
});
You can set the default audio settings when creating the API client, but you can also override these settings per method call whemeber audio settings are relevant, like this.
var podfilesResult = await apiClient.ListPodfilesAsync(new PodfileListRequest(SverigesRadioApiIds.Programs.Sa_Funkar_Det)
{
AudioSettings = new AudioSettings()
{
AudioQuality = AudioQuality.High,
OnDemandAudioTemplateId = SverigesRadioApiIds.OnDemandAudioTemplates.M4A_M3U
}
});
Some identifiers in the API are quite constant and there are classes that lists the most common items.
SverigesRadioApiIds.Programs
SverigesRadioApiIds.Channels
SverigesRadioApiIds.ChannelTypes
SverigesRadioApiIds.ProgramCategories
SverigesRadioApiIds.OnDemandAudioTemplates
SverigesRadioApiIds.LiveAudioTemplates
`Examples:
SverigesRadioApiIds.Programs.Ekot
returns4540
SverigesRadioApiIds.Channels.P4_Ostergötland_Lokalkanal
returns222
SverigesRadioApiIds.ChannelTypes.LokalKanal
returnsLokal kanal
SverigesRadioApiIds.ProgramCategories.Ekonomi
returns135
SverigesRadioApiIds.OnDemandAudioTemplates.Html5_Desktop
returns9
SverigesRadioApiIds.LiveAudioTemplates.IOS
returns12
For more use cases, samples and inspiration; feel free to browse our samples and test.
- Orneholm.SverigesRadio.ConsoleSample
- Orneholm.SverigesRadio.BlazorSample
- Orneholm.SverigesRadio.Api.Test
We are very open to community contributions. Please see our contribution guidelines before getting started.
Orneholm.SverigesRadio is licensed under the very permissive MIT license for you to be able to use it in commercial or non-commercial applications without many restrictions.
The brand Sveriges Radio belongs to Sveriges Radio.