feat: add get apps method
This commit is contained in:
+22
-6
@@ -4,6 +4,9 @@ import { ArgumentValidator } from './models/ArgumentValidator';
|
||||
import { EndpointFactory } from './models/EndpointFactory';
|
||||
import { ApiResponseFactory } from './models/ApiResponseFactory';
|
||||
import { ApiResponse } from './models/ApiResponse';
|
||||
import { App } from './models/App';
|
||||
import { PagingRequest } from './models/PagingRequest';
|
||||
import { GetPagedAppsResponse } from './models/GetPagedAppsResponse';
|
||||
|
||||
/**
|
||||
* @class OnspringClient - A client that can communicate with the Onspring API.
|
||||
@@ -51,12 +54,25 @@ export class OnspringClient {
|
||||
const endpoint = EndpointFactory.getPingEndpoint(
|
||||
this._client.defaults.baseURL
|
||||
);
|
||||
try {
|
||||
const response = await this.get<ApiResponse<boolean>>(endpoint);
|
||||
return response.isSuccessful;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const response = await this.get<ApiResponse<boolean>>(endpoint);
|
||||
return response.isSuccessful;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param pagingRequest - The paging request that will be used to get the apps.
|
||||
* @returns
|
||||
*/
|
||||
public async getApps(
|
||||
pagingRequest: PagingRequest = new PagingRequest(1, 50)
|
||||
): Promise<ApiResponse<GetPagedAppsResponse>> {
|
||||
const endpoint = EndpointFactory.getAppsEndpoint(
|
||||
this._client.defaults.baseURL,
|
||||
pagingRequest
|
||||
);
|
||||
|
||||
return await this.get<GetPagedAppsResponse>(endpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { PagingRequest } from "./PagingRequest";
|
||||
|
||||
/**
|
||||
* @class EndpointFactory - A factory class for creating endpoints.
|
||||
*/
|
||||
@@ -14,8 +16,8 @@ export class EndpointFactory {
|
||||
* @param {string} baseUrl - The base url that will be used to create the apps endpoint.
|
||||
* @returns {string} - The apps endpoint.
|
||||
*/
|
||||
public static getAppsEndpoint(baseUrl: string): string {
|
||||
return `${baseUrl}/Apps`;
|
||||
public static getAppsEndpoint(baseUrl: string, pagingRequest: PagingRequest): string {
|
||||
return `${baseUrl}/Apps?pageSize=${pagingRequest.pageSize}&pageNumber=${pagingRequest.pageNumber}`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { PagedResponse } from "./PagedResponse";
|
||||
import { App } from "./App";
|
||||
|
||||
export class GetPagedAppsResponse extends PagedResponse<App> {
|
||||
constructor(items: App[], pageNumber: number, pageSize: number, totalCount: number) {
|
||||
super(items, pageNumber, pageSize, totalCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
export class PagedResponse<T> {
|
||||
public items: T[];
|
||||
public pageNumber: number;
|
||||
public pageSize: number;
|
||||
public totalCount: number;
|
||||
|
||||
constructor(items: T[], pageNumber: number, pageSize: number, totalCount: number) {
|
||||
this.items = items;
|
||||
this.pageNumber = pageNumber;
|
||||
this.pageSize = pageSize;
|
||||
this.totalCount = totalCount;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { EndpointFactory } from '../src/models/EndpointFactory';
|
||||
import { expect } from 'chai';
|
||||
import { PagingRequest } from '../src/models/PagingRequest';
|
||||
|
||||
describe('EndpointFactory', function () {
|
||||
const baseUrl = 'https://api.onspring.com';
|
||||
@@ -12,9 +13,9 @@ describe('EndpointFactory', function () {
|
||||
});
|
||||
|
||||
describe('getAppsEndpoint', function () {
|
||||
it('should return the correct apps endpoint', function () {
|
||||
const result = EndpointFactory.getAppsEndpoint(baseUrl);
|
||||
expect(result).to.equal(`${baseUrl}/Apps`);
|
||||
it('should return the correct apps endpoint with paging params based on paging request parameter passed', function () {
|
||||
const result = EndpointFactory.getAppsEndpoint(baseUrl, new PagingRequest(2, 1000));
|
||||
expect(result).to.equal(`${baseUrl}/Apps?page=2&pageSize=1000`);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
|
||||
import { expect } from 'chai';
|
||||
import { App } from '../src/models/App';
|
||||
|
||||
describe('GetPagedAppsResponse', function () {
|
||||
it('should be defined', function () {
|
||||
expect(GetPagedAppsResponse).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should have a constructor', function () {
|
||||
expect(GetPagedAppsResponse).to.have.property('constructor');
|
||||
});
|
||||
|
||||
it('should have 4 parameters', function () {
|
||||
expect(GetPagedAppsResponse).to.have.lengthOf(4);
|
||||
});
|
||||
|
||||
it('should construct a new instance of GetPagedAppsResponse', function () {
|
||||
const getPagedAppsResponse = new GetPagedAppsResponse([new App('test', 1, 'test'), new App('test', 1, 'test')], 1, 10, 100);
|
||||
|
||||
expect(getPagedAppsResponse).to.not.be.undefined;
|
||||
expect(getPagedAppsResponse).to.be.instanceOf(GetPagedAppsResponse);
|
||||
expect(getPagedAppsResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(2);
|
||||
expect(getPagedAppsResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1);
|
||||
expect(getPagedAppsResponse).to.have.property('pageSize').to.be.a('number').to.equal(10);
|
||||
expect(getPagedAppsResponse).to.have.property('totalCount').to.be.a('number').to.equal(100);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { PagedResponse } from '../src/models/PagedResponse';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('PagedResponse', function () {
|
||||
it('should be defined', function () {
|
||||
expect(PagedResponse).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should have a constructor', function () {
|
||||
expect(PagedResponse).to.have.property('constructor');
|
||||
});
|
||||
|
||||
it('should have 4 parameters', function () {
|
||||
expect(PagedResponse).to.have.lengthOf(4);
|
||||
});
|
||||
|
||||
it('should construct a new instance of PagedResponse', function () {
|
||||
const pagedResponse = new PagedResponse<number>([1, 2, 3, 4], 1, 10, 100);
|
||||
|
||||
expect(pagedResponse).to.not.be.undefined;
|
||||
expect(pagedResponse).to.be.instanceOf(PagedResponse);
|
||||
expect(pagedResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(4);
|
||||
expect(pagedResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1);
|
||||
expect(pagedResponse).to.have.property('pageSize').to.be.a('number').to.equal(10);
|
||||
expect(pagedResponse).to.have.property('totalCount').to.be.a('number').to.equal(100);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user