Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Moved examples to their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultNocchi committed Sep 16, 2021
1 parent 7c72c87 commit b6ec7c6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 81 deletions.
83 changes: 83 additions & 0 deletions EXAMPLES.md
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);
```
82 changes: 1 addition & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b6ec7c6

Please sign in to comment.