From b6ec7c660d186e197fe999642cf6f24e35bb3f30 Mon Sep 17 00:00:00 2001 From: Thibault Nocchi <1619359+ThibaultNocchi@users.noreply.github.com> Date: Thu, 16 Sep 2021 12:01:13 +0200 Subject: [PATCH] Moved examples to their own file --- EXAMPLES.md | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 82 +--------------------------------------------------- 2 files changed, 84 insertions(+), 81 deletions(-) create mode 100644 EXAMPLES.md diff --git a/EXAMPLES.md b/EXAMPLES.md new file mode 100644 index 00000000..4bf9d4f0 --- /dev/null +++ b/EXAMPLES.md @@ -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); +``` diff --git a/README.md b/README.md index 3577a2d8..d30df62d 100644 --- a/README.md +++ b/README.md @@ -51,87 +51,7 @@ import type TYPE_NAME from "@jellyfin/client-axios/models"; ## Usage -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); -``` +Usage examples can be found [here](EXAMPLES.md). ## Build Process