Add OpenAPI spec for new API, and wire up a new API handler

Based on Aura and JSONAPI
This commit is contained in:
Deluan 2023-03-18 20:09:41 -04:00 • committed by Deluan
commit dcb5725642
16 changed files with 2660 additions and 46 deletions

537
api/spec.yml Normal file
View file

@ -0,0 +1,537 @@
openapi: 3.0.0
info:
version: 0.2.0
title: Navidrome API
description: >
This spec describes the Navidrome API, which allows users to browse and manage their music library via a JSON:API
based interface. The API provides endpoints for albums, tracks, artists, playlists and images, along with their
relationships. Clients can retrieve information about the items in the library, filter and sort results, and
perform actions such as creating and deleting playlists. With this API, developers can build music apps and
services that integrate with Navidrome music server, providing a seamless experience for users to access and
manage their music collection.
contact:
name: Navidrome
url: https://navidrome.org
servers:
- url: /api/v2
paths:
/server:
get:
summary: Get server's global info
operationId: getServerInfo
responses:
'200':
description: The response’s data key maps to a resource object dictionary representing the server.
content:
application/vnd.api+json:
schema:
type: object
required: [data]
properties:
data:
$ref: '#/components/schemas/ServerInfo'
'403':
$ref: '#/components/responses/NotAuthorized'
'500':
$ref: '#/components/responses/InternalServerError'
/tracks:
get:
summary: Get a list of tracks
operationId: getTracks
parameters:
- $ref: '#/components/parameters/pageLimit'
- $ref: '#/components/parameters/pageOffset'
- $ref: '#/components/parameters/filterEquals'
- $ref: '#/components/parameters/filterContains'
- $ref: '#/components/parameters/filterLessThan'
- $ref: '#/components/parameters/filterLessOrEqual'
- $ref: '#/components/parameters/filterGreaterThan'
- $ref: '#/components/parameters/filterGreaterOrEqual'
- $ref: '#/components/parameters/filterStartsWith'
- $ref: '#/components/parameters/filterEndsWith'
- $ref: '#/components/parameters/sort'
- $ref: '#/components/parameters/include'
responses:
'200':
description: A list of tracks
content:
application/vnd.api+json:
schema:
type: object
required: [data, links]
properties:
data:
type: array
items:
$ref: '#/components/schemas/Track'
links:
$ref: '#/components/schemas/PaginationLinks'
meta:
$ref: '#/components/schemas/PaginationMeta'
'400':
$ref: '#/components/responses/BadRequest'
'403':
$ref: '#/components/responses/NotAuthorized'
'500':
$ref: '#/components/responses/InternalServerError'
/tracks/{trackId}:
get:
summary: Retrieve an individual track
operationId: getTrack
parameters:
- $ref: '#/components/parameters/include'
- name: trackId
in: path
description: The unique identifier of the track
required: true
schema:
type: string
responses:
'200':
description: A track object
content:
application/vnd.api+json:
schema:
type: object
required: [data]
properties:
data:
$ref: '#/components/schemas/Track'
'403':
$ref: '#/components/responses/NotAuthorized'
'404':
$ref: '#/components/responses/NotFound'
'500':
$ref: '#/components/responses/InternalServerError'
/artists:
get:
summary: Retrieve a list of artists
operationId: getArtists
parameters:
- $ref: '#/components/parameters/pageLimit'
- $ref: '#/components/parameters/pageOffset'
- $ref: '#/components/parameters/filterEquals'
- $ref: '#/components/parameters/filterContains'
- $ref: '#/components/parameters/filterLessThan'
- $ref: '#/components/parameters/filterLessOrEqual'
- $ref: '#/components/parameters/filterGreaterThan'
- $ref: '#/components/parameters/filterGreaterOrEqual'
- $ref: '#/components/parameters/filterStartsWith'
- $ref: '#/components/parameters/filterEndsWith'
- $ref: '#/components/parameters/sort'
- $ref: '#/components/parameters/include'
responses:
'200':
description: A list of artists
content:
application/vnd.api+json:
schema:
type: object
required: [data, links]
properties:
data:
type: array
items:
$ref: '#/components/schemas/Artist'
links:
$ref: '#/components/schemas/PaginationLinks'
meta:
$ref: '#/components/schemas/PaginationMeta'
'400':
$ref: '#/components/responses/BadRequest'
'403':
$ref: '#/components/responses/NotAuthorized'
'500':
$ref: '#/components/responses/InternalServerError'
/artists/{artistId}:
get:
summary: Retrieve an individual artist
operationId: getArtist
parameters:
- $ref: '#/components/parameters/include'
- name: artistId
in: path
description: The unique identifier of the artist
required: true
schema:
type: string
responses:
'200':
description: An artist object
content:
application/vnd.api+json:
schema:
type: object
required: [data]
properties:
data:
$ref: '#/components/schemas/Artist'
'403':
$ref: '#/components/responses/NotAuthorized'
'404':
$ref: '#/components/responses/NotFound'
'500':
$ref: '#/components/responses/InternalServerError'
components:
parameters:
pageOffset:
name: page[offset]
in: query
description: The offset for pagination
required: false
schema:
type: integer
format: int32
minimum: 0
default: 0
pageLimit:
name: page[limit]
in: query
description: The number of items per page
required: false
schema:
type: integer
format: int32
minimum: 0
default: 10
filterEquals:
name: filter[equals]
in: query
description: 'Filter by any property with an exact match. Usage: filter[equals]=property:value'
required: false
schema:
type: array
items:
type: string
pattern: '^\w+:\w+'
filterLessThan:
name: filter[lessThan]
in: query
description: 'Filter by any numeric property less than a value. Usage: filter[lessThan]=property:value'
required: false
schema:
type: array
items:
type: string
pattern: '^\w+:\d+'
filterLessOrEqual:
name: filter[lessOrEqual]
in: query
description: 'Filter by any numeric property less than or equal to a value. Usage: filter[lessOrEqual]=property:value'
required: false
schema:
type: array
items:
type: string
pattern: '^\w+:\d+'
filterGreaterThan:
name: filter[greaterThan]
in: query
description: 'Filter by any numeric property greater than a value. Usage: filter[greaterThan]=property:value'
required: false
schema:
type: array
items:
type: string
pattern: '^\w+:\d+'
filterGreaterOrEqual:
name: filter[greaterOrEqual]
in: query
description: 'Filter by any numeric property greater than or equal to a value. Usage: filter[greaterOrEqual]=property:value'
required: false
schema:
type: array
items:
type: string
pattern: '^\w+:\d+'
filterContains:
name: filter[contains]
in: query
description: 'Filter by any property containing text. Usage: filter[contains]=property:value'
required: false
schema:
type: array
items:
type: string
pattern: '^\w+:\w+'
filterStartsWith:
name: filter[startsWith]
in: query
description: 'Filter by any property that starts with text. Usage: filter[startsWith]=property:value'
required: false
schema:
type: array
items:
type: string
pattern: '^\w+:\w+'
filterEndsWith:
name: filter[endsWith]
in: query
description: 'Filter by any property that ends with text. Usage: filter[endsWith]=property:value'
required: false
schema:
type: array
items:
type: string
pattern: '^\w+:\w+'
sort:
name: sort
in: query
description: Sort the results by one or more properties, separated by commas. Prefix the property with '-' for descending order.
required: false
schema:
type: string
include:
name: include
in: query
description: Related resources to include in the response, separated by commas
required: false
schema:
type: string
schemas:
ServerInfo:
type: object
required: [server, serverVersion, authRequired, features]
properties:
server:
type: string
description: The name of the server software.
example: "navidrome"
serverVersion:
type: string
description: The version number of the server.
example: "0.60.0"
authRequired:
type: boolean
description: Whether the user has access to the server.
example: true
features:
type: array
description: A list of optional features the server supports.
items:
type: string
enum:
- albums
- artists
- images
ResourceObject:
type: object
required: [id, type]
properties:
id:
type: string
description: The unique identifier for the resource
type:
type: string
description: The type of the resource
ResourceList:
type: object
properties:
data:
oneOf:
- $ref: '#/components/schemas/Track'
- $ref: '#/components/schemas/Artist'
- type: array
items:
$ref: '#/components/schemas/ResourceObject'
included:
type: array
items:
$ref: '#/components/schemas/IncludedResource'
links:
$ref: '#/components/schemas/PaginationLinks'
meta:
$ref: '#/components/schemas/PaginationMeta'
IncludedResource:
oneOf:
- $ref: '#/components/schemas/Track'
- $ref: '#/components/schemas/Artist'
discriminator:
propertyName: type
mapping:
track: '#/components/schemas/Track'
artist: '#/components/schemas/Artist'
Track:
allOf:
- $ref: '#/components/schemas/ResourceObject'
- type: object
properties:
attributes:
$ref: '#/components/schemas/TrackAttributes'
relationships:
type: object
properties:
artists:
type: array
items:
$ref: '#/components/schemas/TrackArtistRelationship'
required:
- artist
TrackAttributes:
type: object
properties:
title:
type: string
description: The title of the track
artist:
type: string
description: The name of the artist who performed the track
album:
type: string
description: The name of the album the track belongs to
composer:
type: string
description: The name of the composer who created the track
duration:
type: number
format: float
description: The duration of the track in seconds
required:
- title
- artist
- album
- duration
TrackArtistRelationship:
type: object
properties:
meta:
type: object
properties:
role:
$ref: '#/components/schemas/ArtistRole'
required:
- role
data:
$ref: '#/components/schemas/ResourceObject'
required:
- meta
- data
ArtistRole:
type: string
enum:
- albumArtist
- composer
- vocalist
description: The role of an artist in a track or album
Artist:
allOf:
- $ref: '#/components/schemas/ResourceObject'
- type: object
properties:
attributes:
$ref: '#/components/schemas/ArtistAttributes'
relationships:
type: object
properties:
tracks:
type: object
properties:
data:
type: array
items:
$ref: '#/components/schemas/ArtistTrackRelationship'
required:
- data
required:
- tracks
ArtistAttributes:
type: object
properties:
name:
type: string
description: The name of the artist
bio:
type: string
description: A short biography of the artist
ArtistTrackRelationship:
type: object
properties:
meta:
type: object
properties:
role:
$ref: '#/components/schemas/ArtistRole'
required:
- role
data:
$ref: '#/components/schemas/ResourceObject'
required:
- meta
- data
PaginationLinks:
type: object
properties:
first:
type: string
format: uri
prev:
type: string
format: uri
next:
type: string
format: uri
last:
type: string
format: uri
PaginationMeta:
type: object
properties:
currentPage:
type: integer
format: int32
description: The current page in the collection
totalPages:
type: integer
format: int32
description: The total numeber of pages in the collection
totalItems:
type: integer
format: int32
description: The total number of items in the collection
ErrorList:
type: object
required: [errors]
properties:
errors:
type: array
items:
$ref: '#/components/schemas/ErrorObject'
ErrorObject:
type: object
properties:
id:
type: string
status:
type: string
title:
type: string
detail:
type: string
required:
- errors
responses:
NotFound:
description: Not Found
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/ErrorList'
NotAuthorized:
description: Not Authorized
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/ErrorList'
BadRequest:
description: Bad Request
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/ErrorList'
InternalServerError:
description: Internal Server Error
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/ErrorList'

202
api/spec1.yml Normal file
View file

@ -0,0 +1,202 @@
openapi: "3.0.0"
info:
version: 0.3.0
title: Navidrome API
description: 'This is heavily based on Aura API: https://auraspec.readthedocs.io/en/latest/api.html'
contact:
name: Navidrome
url: https://navidrome.org
servers:
- url: /api/v2
security:
- bearerAuth: []
paths:
/server:
get:
operationId: getServerInfo
responses:
'200':
description: The response’s data key maps to a resource object dictionary representing the server.
content:
application/vnd.api+json:
schema:
type: object
required: [data]
properties:
data:
$ref: '#/components/schemas/ServerInfo'
default:
description: unexpected error
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/JSONAPIErrorResponse'
/tracks:
get:
operationId: getTracks
parameters:
- in: query
name: limit
schema:
type: integer
minimum: 0
default: 0
responses:
'200':
description: TODO
content:
application/vnd.api+json:
schema:
type: object
required: [data]
properties:
data:
type: array
items:
$ref: '#/components/schemas/Track'
default:
description: unexpected error
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/JSONAPIErrorResponse'
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
schemas:
ServerInfo:
type: object
required: [aura-version, server, server-version, auth-required, features]
properties:
aura-version:
type: string
description: The version of the AURA spec implemented.
example: "1.0.0"
server:
type: string
description: The name of the server software.
example: "navidrome"
server-version:
type: string
description: The version number of the server.
example: "0.60.0"
auth-required:
type: boolean
description: Whether the user has access to the server.
example: true
features:
type: array
description: A list of optional features the server supports.
items:
type: string
enum:
- albums
- artists
- images
JSONAPIResourceObject:
title: JSON:API Resource Object
description: |
A JSON:API-compliant resource object. An inheritable object schema that contains the basic framework of a Resource Object from the JSON:API spec.
The `attributes`, `relationship`, and `meta` properties are added, as necessary, by the inheriting schema.
type: object
required: [type, id]
properties:
type:
$ref: '#/components/schemas/JSONAPIType'
id:
$ref: '#/components/schemas/JSONAPIId'
JSONAPIType:
title: JSON:API type Member
description: |
The `type` member is used to describe resource objects that share common attributes and relationships.
Note: The type member is required in every resource object throughout requests and responses in JSON:API. There are some cases, such as when POSTing to an endpoint representing heterogeneous data, when the type could not be inferred from the endpoint. However, picking and choosing when it is required would be confusing; it would be hard to remember when it was required and when it was not. Therefore, to improve consistency and minimize confusion, type is always required.
type: string
JSONAPIId:
title: JSON:API id Member
description: Every resource object MUST contain an id member and a type member (exception-the id member is not required when the resource object originates at the client and represents a new resource to be created on the server). Within a given API, each resource object’s type and id pair MUST identify a single, unique resource.
type: string
Track:
allOf:
- $ref: '#/components/schemas/JSONAPIResourceObject'
- type: object
required: [attributes]
properties:
attributes:
$ref: '#/components/schemas/TrackAttributes'
TrackAttributes:
type: object
required: [title, artist, album, albumartist, track, mimetype, duration, channels, bitrate, size]
properties:
title:
type: string
artist:
type: string
album:
type: string
albumartist:
type: string
track:
type: integer
disc:
type: integer
year:
type: integer
bpm:
type: integer
genre:
type: string
recording-mbid:
type: string
track-mbid:
type: string
comments:
type: string
mimetype:
type: string
duration:
type: number
channels:
type: integer
bitrate:
type: integer
size:
type: integer
JSONAPIErrorResponse:
title: JSON:API Resource Object
type: object
required: [errors]
properties:
errors:
type: array
items:
$ref: '#/components/schemas/JSONAPIErrorObject'
JSONAPIErrorObject:
type: object
description: 'A JSON-API error object. see: https://jsonapi.org/format/#errors'
properties:
id:
type: string
description: a unique identifier for this particular occurrence of the problem.
example: 1
status:
type: integer
description: the HTTP status code applicable to this problem, expressed as a string value.
example: 400
code:
type: string
description: an application-specific error code, expressed as a string value.
example: invalid_request
detail:
type: string
description: a human-readable explanation specific to this occurrence of the problem.
example: First name must contain at least two characters.