Files
onspring-api-sdk-javascript/src/models/OnspringClient.ts
T

161 lines
5.9 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
2023-02-01 23:09:46 -06:00
import { AxiosInstance, AxiosRequestConfig } from 'axios';
import { ArgumentValidator } from './ArgumentValidator';
import { EndpointFactory } from './EndpointFactory';
import { ApiResponseFactory } from './ApiResponseFactory';
import { ApiResponse } from './ApiResponse';
import { PagingRequest } from './PagingRequest';
import { GetPagedAppsResponse } from './GetPagedAppsResponse';
import { App } from './App';
2023-02-02 20:24:20 -06:00
import { CollectionResponse } from './CollectionResponse';
2023-02-02 21:37:40 -06:00
import { Field } from './Field';
2023-01-25 17:08:19 -06:00
/**
* @class OnspringClient - A client that can communicate with the Onspring API.
*/
export class OnspringClient {
/**
2023-01-26 14:20:30 -06:00
* @readonly {AxiosInstance} client - The axios instance that will be used to make requests to the Onspring API.
*/
2023-01-26 22:40:13 -06:00
private readonly _client: AxiosInstance;
/**
* @constructor - Creates a new instance of the OnspringClient class.
* @param {string} baseUrl - The base url that will be used to make requests to the Onspring API.
* @param {string} apiKey - The api key that will be used to authorize requests made by this client.
* @throws {Error} - Thrown when the baseUrl is not a valid url.
* @throws {Error} - Thrown when the apiKey is null/undefined/empty/whitespace.
* @returns {OnspringClient} - A new instance of the OnspringClient class.
*/
constructor(
baseUrl: string | undefined | null,
apiKey: string | undefined | null
) {
if (ArgumentValidator.isValidUrl(baseUrl) === false || baseUrl === null) {
throw new Error('baseUrl must be an absolute and well-formed URI.');
}
if (ArgumentValidator.isNullOrWhiteSpace(apiKey)) {
throw new Error('apiKey cannot be null/empty/whitespace.');
}
2023-02-01 23:45:14 -06:00
this._client = axios.create({
baseURL: baseUrl,
headers: { 'x-apikey': apiKey, 'x-api-version': '2' },
});
2023-01-25 17:08:19 -06:00
}
2023-01-27 11:28:45 -06:00
/**
* @method canConnect - Determines if the client can connect to the Onspring API.
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the client can connect to the Onspring API.
*/
2023-01-27 11:28:45 -06:00
public async canConnect(): Promise<boolean> {
const endpoint = EndpointFactory.getPingEndpoint();
2023-01-27 22:53:14 -06:00
const response = await this.get<any>(endpoint);
2023-01-27 22:53:14 -06:00
return response.isSuccessful;
}
/**
2023-02-02 20:24:20 -06:00
* @method getApps - Gets a paged list of apps.
2023-02-02 22:52:14 -06:00
* @param {PagingRequest} pagingRequest - The paging request that will be used to get the apps.
* @returns {Promise<ApiResponse<GetPagedAppsResponse>>} - A promise that resolves to an ApiResponse of type GetPagedAppsResponse.
2023-01-27 22:53:14 -06:00
*/
public async getApps(
pagingRequest: PagingRequest = new PagingRequest(1, 50)
): Promise<ApiResponse<GetPagedAppsResponse>> {
const endpoint = EndpointFactory.getAppsEndpoint(pagingRequest);
2023-01-27 22:53:14 -06:00
var apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.AsGetPagedAppsResponseType();
2023-01-27 11:28:45 -06:00
}
2023-02-02 20:24:20 -06:00
/**
* @method getAppById - Gets an app by its id.
2023-02-02 22:52:14 -06:00
* @param {number} appId - The id of the app to get.
* @returns {Promise<ApiResponse<App>>} - A promise that resolves to an ApiResponse of type App.
2023-02-02 20:24:20 -06:00
*/
public async getAppById(appId: number): Promise<ApiResponse<App>> {
const endpoint = EndpointFactory.getAppByIdEndpoint(appId);
var apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.AsAppType();
}
2023-02-02 20:24:20 -06:00
/**
* @method getAppsByIds - Gets a list of apps by their ids.
2023-02-02 22:52:14 -06:00
* @param {number[]} appIds - The ids of the apps to get.
* @returns {Promise<ApiResponse<CollectionResponse<App>>>} - A promise that resolves to an ApiResponse of type CollectionResponse<App>.
2023-02-02 20:24:20 -06:00
*/
public async getAppsByIds(
2023-02-02 22:52:14 -06:00
appIds: number[]
2023-02-02 20:24:20 -06:00
): Promise<ApiResponse<CollectionResponse<App>>> {
const endpoint = EndpointFactory.getAppsByIdsEndpoint();
2023-02-02 22:52:14 -06:00
const apiResponse = await this.post<any>(endpoint, appIds);
2023-02-02 20:24:20 -06:00
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.AsAppCollectionType();
}
2023-02-02 22:52:14 -06:00
/**
* @method getFields - Gets a paged list of fields.
* @param {PagingRequest} pagingRequest - The paging request that will be used to get the fields.
* @returns {Promise<ApiResponse<Field>>} - A promise that resolves to an ApiResponse of type Field.
*/
2023-02-02 21:37:40 -06:00
public async getFieldById(fieldId: number): Promise<ApiResponse<Field>> {
const endpoint = EndpointFactory.getFieldByIdEndpoint(fieldId);
const apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.AsFieldType();
}
/**
* @method get - Makes a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request.
2023-02-02 20:24:20 -06:00
* @param {AxiosRequestConfig} config - The configuration that will be used to make the request.
* @returns {Promise<ApiResponse<T>>} - A promise that resolves to an ApiResponse of type T.
*/
2023-02-01 23:45:14 -06:00
private async get<T>(
endpoint: string,
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
2023-02-01 23:50:20 -06:00
const response = await this._client.get(endpoint, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
2023-02-01 23:45:14 -06:00
return apiResponse;
2023-01-27 11:28:45 -06:00
}
2023-02-02 20:24:20 -06:00
/**
2023-02-02 22:52:14 -06:00
* @method post - Makes a POST request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request.
* @param {any} data - The data that will be sent with the request.
* @param {AxiosRequestConfig} config - The configuration that will be used to make the request.
* @returns {Promise<ApiResponse<T>>} - A promise that resolves to an ApiResponse of type T.
2023-02-02 20:24:20 -06:00
*/
private async post<T>(
endpoint: string,
data: any,
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.post(endpoint, data, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
}