This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c72c87
commit b6ec7c6
Showing
2 changed files
with
84 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Usage examples | ||
|
||
Install it in your project: | ||
|
||
```bash | ||
npm i @jellyfin/client-axios | ||
``` | ||
|
||
This library follows the API pattern you can get on https://api.jellyfin.org/ or on your own server at `/api-docs/swagger/index.html` or `/api-docs/redoc/index.html`. | ||
|
||
## Basic example | ||
|
||
```typescript | ||
// Get server infos | ||
import { SystemApi } from "@jellyfin/client-axios"; | ||
|
||
const systemApi = new SystemApi( | ||
undefined, // OpenAPI configuration object, found in `configuration.ts` | ||
"https://demo.jellyfin.org/stable", // Base URL | ||
undefined // Axios base object to use for queries, can be used to set base URL or headers | ||
); | ||
const infos = (await systemApi.getPublicSystemInfo()).data; | ||
console.log(infos); | ||
``` | ||
|
||
## Authentication | ||
|
||
For authentication, Jellyfin needs an `Authorization` header with a a few informations filled in, and that isn't part of the SDK spec. So the authentication method doesn't pass the `Configuration` API key field that we will use later, and we need to specify it manually. It'll be then used to append the fetched authentication token and use it as the classic authorization header. | ||
|
||
```typescript | ||
import { v4 } from "uuid"; // Used to generate random string | ||
import { UserApi } from "@jellyfin/client-axios"; | ||
|
||
const base_token = `MediaBrowser Client="My New Client", Device="${ | ||
window.navigator.userAgent | ||
}", DeviceId="${v4()}", Version="1.0.0"`; // Base string for the full header | ||
|
||
const vanilla_token = `${base_token}, Token=""`; // We need a vanilla token to authenticate | ||
|
||
const ax = axios.create({ | ||
headers: { | ||
Authorization: vanilla_token | ||
} | ||
}); // Prepare the authentification header | ||
|
||
const userApi = new UserApi(undefined, "https://demo.jellyfin.org/stable", ax); | ||
const res = ( | ||
await userApi.authenticateUserByName({ | ||
authenticateUserByName: { | ||
Username: "demo", | ||
Pw: "" | ||
} | ||
}) | ||
).data; | ||
|
||
const logged_token = `${base_token}, Token="${res.AccessToken}"`; // Re-use our base token to append the newly fetched one | ||
const userId = res.User?.Id; | ||
console.log(logged_token); | ||
console.log(userId); | ||
``` | ||
|
||
## API calls | ||
|
||
For other calls, we don't need to use a specific Axios object and just a `Configuration` one with our server URL and the full size token. | ||
|
||
```typescript | ||
const conf = new Configuration({ | ||
basePath: url, | ||
apiKey: token | ||
}); // Use the OpenAPI configuration object to pass the JF header and server URL | ||
|
||
const libraryApi = new LibraryApi(conf); | ||
const libs = (await libraryApi.getMediaFolders()).data; | ||
console.log(libs); | ||
|
||
const artistsApi = new ArtistsApi(conf); | ||
const artist = ( | ||
await artistsApi.getArtistByName({ | ||
name: "Flume" | ||
}) | ||
).data; // Each API parameter is typed and available as method's parameters | ||
console.log(artist); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters