Files
plexjs/src/sdk/plex.ts

254 lines
8.2 KiB
TypeScript

/*
* Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
*/
import { SDKHooks } from "../hooks";
import { SDK_METADATA, SDKOptions, serverURLFromOptions } from "../lib/config";
import * as enc$ from "../lib/encodings";
import { HTTPClient } from "../lib/http";
import * as schemas$ from "../lib/schemas";
import { ClientSDK, RequestOptions } from "../lib/sdks";
import * as errors from "../models/errors";
import * as operations from "../models/operations";
export class Plex extends ClientSDK {
private readonly options$: SDKOptions & { hooks?: SDKHooks };
constructor(options: SDKOptions = {}) {
const opt = options as unknown;
let hooks: SDKHooks;
if (
typeof opt === "object" &&
opt != null &&
"hooks" in opt &&
opt.hooks instanceof SDKHooks
) {
hooks = opt.hooks;
} else {
hooks = new SDKHooks();
}
super({
client: options.httpClient || new HTTPClient(),
baseURL: serverURLFromOptions(options),
hooks,
});
this.options$ = { ...options, hooks };
void this.options$;
}
/**
* Get a Pin
*
* @remarks
* Retrieve a Pin from Plex.tv for authentication flows
*/
async getPin(
strong?: boolean | undefined,
xPlexClientIdentifier?: string | undefined,
options?: RequestOptions & { serverURL?: string }
): Promise<operations.GetPinResponse> {
const input$: operations.GetPinRequest = {
strong: strong,
xPlexClientIdentifier: xPlexClientIdentifier,
};
const headers$ = new Headers();
headers$.set("user-agent", SDK_METADATA.userAgent);
headers$.set("Accept", "application/json");
const payload$ = schemas$.parse(
input$,
(value$) => operations.GetPinRequest$.outboundSchema.parse(value$),
"Input validation failed"
);
const body$ = null;
const baseURL$ =
options?.serverURL ||
this.templateURLComponent(operations.GetPinServerList[0], {
charEncoding: "percent",
})();
const path$ = this.templateURLComponent("/pins")();
const query$ = [
enc$.encodeForm("strong", payload$.strong, { explode: true, charEncoding: "percent" }),
]
.filter(Boolean)
.join("&");
headers$.set(
"X-Plex-Client-Identifier",
enc$.encodeSimple(
"X-Plex-Client-Identifier",
payload$["X-Plex-Client-Identifier"] ?? this.options$.xPlexClientIdentifier,
{ explode: false, charEncoding: "none" }
)
);
const context = { operationID: "getPin", oAuth2Scopes: [], securitySource: null };
const doOptions = { context, errorCodes: ["400", "4XX", "5XX"] };
const request = this.createRequest$(
context,
{
method: "POST",
baseURL: baseURL$,
path: path$,
headers: headers$,
query: query$,
body: body$,
},
options
);
const response = await this.do$(request, doOptions);
const responseFields$ = {
ContentType: response.headers.get("content-type") ?? "application/octet-stream",
StatusCode: response.status,
RawResponse: response,
Headers: {},
};
if (this.matchResponse(response, 200, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return operations.GetPinResponse$.inboundSchema.parse({
...responseFields$,
object: val$,
});
},
"Response validation failed"
);
return result;
} else if (this.matchResponse(response, 400, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.GetPinResponseBody$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else {
const responseBody = await response.text();
throw new errors.SDKError(
"Unexpected API response status or content-type",
response,
responseBody
);
}
}
/**
* Get Access Token
*
* @remarks
* Retrieve an Access Token from Plex.tv after the Pin has already been authenticated
*/
async getToken(
pinID: string,
xPlexClientIdentifier?: string | undefined,
options?: RequestOptions & { serverURL?: string }
): Promise<operations.GetTokenResponse> {
const input$: operations.GetTokenRequest = {
pinID: pinID,
xPlexClientIdentifier: xPlexClientIdentifier,
};
const headers$ = new Headers();
headers$.set("user-agent", SDK_METADATA.userAgent);
headers$.set("Accept", "application/json");
const payload$ = schemas$.parse(
input$,
(value$) => operations.GetTokenRequest$.outboundSchema.parse(value$),
"Input validation failed"
);
const body$ = null;
const baseURL$ =
options?.serverURL ||
this.templateURLComponent(operations.GetTokenServerList[0], {
charEncoding: "percent",
})();
const pathParams$ = {
pinID: enc$.encodeSimple("pinID", payload$.pinID, {
explode: false,
charEncoding: "percent",
}),
};
const path$ = this.templateURLComponent("/pins/{pinID}")(pathParams$);
const query$ = "";
headers$.set(
"X-Plex-Client-Identifier",
enc$.encodeSimple(
"X-Plex-Client-Identifier",
payload$["X-Plex-Client-Identifier"] ?? this.options$.xPlexClientIdentifier,
{ explode: false, charEncoding: "none" }
)
);
const context = { operationID: "getToken", oAuth2Scopes: [], securitySource: null };
const doOptions = { context, errorCodes: ["400", "4XX", "5XX"] };
const request = this.createRequest$(
context,
{
method: "GET",
baseURL: baseURL$,
path: path$,
headers: headers$,
query: query$,
body: body$,
},
options
);
const response = await this.do$(request, doOptions);
const responseFields$ = {
ContentType: response.headers.get("content-type") ?? "application/octet-stream",
StatusCode: response.status,
RawResponse: response,
Headers: {},
};
if (this.matchStatusCode(response, 200)) {
// fallthrough
} else if (this.matchResponse(response, 400, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.GetTokenResponseBody$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else {
const responseBody = await response.text();
throw new errors.SDKError(
"Unexpected API response status or content-type",
response,
responseBody
);
}
return schemas$.parse(
undefined,
() => operations.GetTokenResponse$.inboundSchema.parse(responseFields$),
"Response validation failed"
);
}
}