2023-01-26 13:58:25 -06:00
|
|
|
import axios from 'axios';
|
2023-02-01 23:09:46 -06:00
|
|
|
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
2023-01-26 13:58:25 -06:00
|
|
|
import { ArgumentValidator } from './models/ArgumentValidator';
|
2023-01-27 11:28:45 -06:00
|
|
|
import { EndpointFactory } from './models/EndpointFactory';
|
2023-01-27 15:00:34 -06:00
|
|
|
import { ApiResponseFactory } from './models/ApiResponseFactory';
|
|
|
|
|
import { ApiResponse } from './models/ApiResponse';
|
2023-01-27 22:53:14 -06:00
|
|
|
import { PagingRequest } from './models/PagingRequest';
|
|
|
|
|
import { GetPagedAppsResponse } from './models/GetPagedAppsResponse';
|
2023-02-01 21:21:39 -06:00
|
|
|
import { App } from './models/App';
|
2023-01-25 17:08:19 -06:00
|
|
|
|
2023-01-26 13:58:25 -06:00
|
|
|
/**
|
|
|
|
|
* @class OnspringClient - A client that can communicate with the Onspring API.
|
|
|
|
|
*/
|
2023-01-25 22:28:45 -06:00
|
|
|
export class OnspringClient {
|
2023-01-26 13:58:25 -06:00
|
|
|
/**
|
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 13:58:25 -06:00
|
|
|
*/
|
2023-01-26 22:40:13 -06:00
|
|
|
private readonly _client: AxiosInstance;
|
2023-01-25 22:28:45 -06:00
|
|
|
|
2023-01-26 13:58:25 -06:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
) {
|
2023-01-25 22:28:45 -06:00
|
|
|
if (ArgumentValidator.isValidUrl(baseUrl) === false) {
|
2023-01-26 13:58:25 -06:00
|
|
|
throw new Error('baseUrl must be an absolute and well-formed URI.');
|
2023-01-25 22:28:45 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 13:58:25 -06:00
|
|
|
if (ArgumentValidator.isNullOrWhiteSpace(apiKey)) {
|
|
|
|
|
throw new Error('apiKey cannot be null/empty/whitespace.');
|
2023-01-25 22:28:45 -06:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2023-01-27 15:00:34 -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> {
|
2023-01-27 15:00:34 -06:00
|
|
|
const endpoint = EndpointFactory.getPingEndpoint(
|
|
|
|
|
this._client.defaults.baseURL
|
|
|
|
|
);
|
2023-01-27 22:53:14 -06:00
|
|
|
|
2023-02-01 21:21:39 -06:00
|
|
|
const response = await this.get<any>(endpoint);
|
2023-01-27 22:53:14 -06:00
|
|
|
return response.isSuccessful;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-02-01 21:21:39 -06:00
|
|
|
*
|
2023-01-27 22:53:14 -06:00
|
|
|
* @param pagingRequest - The paging request that will be used to get the apps.
|
2023-02-01 21:21:39 -06:00
|
|
|
* @returns
|
2023-01-27 22:53:14 -06:00
|
|
|
*/
|
|
|
|
|
public async getApps(
|
|
|
|
|
pagingRequest: PagingRequest = new PagingRequest(1, 50)
|
|
|
|
|
): Promise<ApiResponse<GetPagedAppsResponse>> {
|
|
|
|
|
const endpoint = EndpointFactory.getAppsEndpoint(
|
|
|
|
|
this._client.defaults.baseURL,
|
|
|
|
|
pagingRequest
|
|
|
|
|
);
|
|
|
|
|
|
2023-02-01 21:21:39 -06:00
|
|
|
var apiResponse = await this.get<any>(endpoint);
|
2023-02-01 22:44:30 -06:00
|
|
|
|
|
|
|
|
if (apiResponse.isSuccessful === false) {
|
|
|
|
|
return apiResponse;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 21:21:39 -06:00
|
|
|
return apiResponse.AsGetPagedAppsResponseType();
|
2023-01-27 11:28:45 -06:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 22:44:30 -06:00
|
|
|
public async getAppById(appId: number): Promise<ApiResponse<App>> {
|
|
|
|
|
const endpoint = EndpointFactory.getAppByIdEndpoint(
|
|
|
|
|
this._client.defaults.baseURL,
|
|
|
|
|
appId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var apiResponse = await this.get<any>(endpoint);
|
|
|
|
|
|
|
|
|
|
if (apiResponse.isSuccessful === false) {
|
|
|
|
|
return apiResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apiResponse.AsAppType();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 15:00:34 -06:00
|
|
|
/**
|
|
|
|
|
* @method get - Makes a GET request to the specified endpoint.
|
|
|
|
|
* @param {string} endpoint - The endpoint 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:48:36 -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-01-26 13:58:25 -06:00
|
|
|
}
|