Browse Source

WebhooksService

pull/130/head
Sebastian Stehle 9 years ago
parent
commit
afb87c6c18
  1. 10
      src/Squidex/app/features/dashboard/pages/dashboard-page.component.ts
  2. 33
      src/Squidex/app/shared/services/plans.service.spec.ts
  3. 99
      src/Squidex/app/shared/services/webhooks.service.1.ts
  4. 117
      src/Squidex/app/shared/services/webhooks.service.spec.ts

10
src/Squidex/app/features/dashboard/pages/dashboard-page.component.ts

@ -47,11 +47,13 @@ export class DashboardPageComponent extends AppComponentBase implements OnInit,
display: true display: true
} }
], ],
yAxes: [{ yAxes: [
ticks: { {
beginAtZero: true ticks: {
beginAtZero: true
}
} }
}] ]
}, },
maintainAspectRatio: false maintainAspectRatio: false
}; };

33
src/Squidex/app/shared/services/plans.service.spec.ts

@ -55,21 +55,24 @@ describe('PlansService', () => {
hasConfigured: true, hasConfigured: true,
hasPortal: true, hasPortal: true,
planOwner: '456', planOwner: '456',
plans: [{ plans: [
id: 'free', {
name: 'Free', id: 'free',
costs: '14 €', name: 'Free',
maxApiCalls: 1000, costs: '14 €',
maxAssetSize: 1500, maxApiCalls: 1000,
maxContributors: 2500 maxAssetSize: 1500,
}, { maxContributors: 2500
id: 'prof', },
name: 'Prof', {
costs: '18 €', id: 'prof',
maxApiCalls: 4000, name: 'Prof',
maxAssetSize: 5500, costs: '18 €',
maxContributors: 6500 maxApiCalls: 4000,
}] maxAssetSize: 5500,
maxContributors: 6500
}
]
}); });
expect(plans).toEqual( expect(plans).toEqual(

99
src/Squidex/app/shared/services/webhooks.service.1.ts

@ -1,99 +0,0 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Sebastian Stehle. All rights reserved
*/
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import 'framework/angular/http-extensions';
import {
ApiUrlConfig,
HTTP,
Version
} from 'framework';
export class WebhookDto {
constructor(
public readonly id: string,
public readonly schemaId: string,
public readonly sharedSecret: string,
public readonly url: string,
public readonly totalSucceeded: number,
public readonly totalFailed: number,
public readonly totalTimedout: number,
public readonly averageRequestTimeMs: number,
public readonly lastDumps: string[]
) {
}
}
export class WebhookCreatedDto {
constructor(
public readonly id: string,
public readonly sharedSecret: string
) {
}
}
export class CreateWebhookDto {
constructor(
public readonly url: string
) {
}
}
@Injectable()
export class WebhooksService {
constructor(
private readonly http: HttpClient,
private readonly apiUrl: ApiUrlConfig
) {
}
public getWebhooks(appName: string, version?: Version): Observable<WebhookDto[]> {
const url = this.apiUrl.buildUrl(`api/apps/${appName}/webhooks`);
return HTTP.getVersioned(this.http, url, version)
.map(response => {
const items: any[] = response;
return items.map(item => {
return new WebhookDto(
item.id,
item.schemaId,
item.sharedSecret,
item.url,
item.totalSucceeded,
item.totalFailed,
item.totalTimedout,
item.averageRequestTimeMs,
item.lastDumps);
});
})
.pretifyError('Failed to load webhooks. Please reload.');
}
public postWebhook(appName: string, schemaName: string, dto: CreateWebhookDto, version?: Version): Observable<WebhookCreatedDto> {
const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/webhooks`);
return HTTP.postVersioned(this.http, url, dto, version)
.map(response => {
return new WebhookCreatedDto(
response.id,
response.sharedSecret);
})
.pretifyError('Failed to create webhook. Please reload.');
}
public deleteWebhook(appName: string, schemaName: string, id: string, version?: Version): Observable<any> {
const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/webhooks/${id}`);
return HTTP.deleteVersioned(this.http, url, version)
.pretifyError('Failed to delete webhook. Please reload.');
}
}

117
src/Squidex/app/shared/services/webhooks.service.spec.ts

@ -0,0 +1,117 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Sebastian Stehle. All rights reserved
*/
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { inject, TestBed } from '@angular/core/testing';
import {
ApiUrlConfig,
CreateWebhookDto,
Version,
WebhookCreatedDto,
WebhookDto,
WebhooksService
} from './../';
describe('WebhooksService', () => {
let version = new Version('1');
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
WebhooksService,
{ provide: ApiUrlConfig, useValue: new ApiUrlConfig('http://service/p/') }
]
});
});
afterEach(inject([HttpTestingController], (httpMock: HttpTestingController) => {
httpMock.verify();
}));
it('should make get request to get app webhooks',
inject([WebhooksService, HttpTestingController], (webhooksService: WebhooksService, httpMock: HttpTestingController) => {
let webhooks: WebhookDto[] | null = null;
webhooksService.getWebhooks('my-app', version).subscribe(result => {
webhooks = result;
});
const req = httpMock.expectOne('http://service/p/api/apps/my-app/webhooks');
expect(req.request.method).toEqual('GET');
expect(req.request.headers.get('If-Match')).toBe(version.value);
req.flush([
{
id: 'id1',
schemaId: 'schemaId1',
sharedSecret: 'token1',
url: 'http://squidex.io/1',
totalSucceeded: 1,
totalFailed: 2,
totalTimedout: 3,
averageRequestTimeMs: 4,
lastDumps: ['dump1']
},
{
id: 'id2',
schemaId: 'schemaId2',
sharedSecret: 'token2',
url: 'http://squidex.io/2',
totalSucceeded: 5,
totalFailed: 6,
totalTimedout: 7,
averageRequestTimeMs: 8,
lastDumps: ['dump2']
}
]);
expect(webhooks).toEqual([
new WebhookDto('id1', 'schemaId1', 'token1', 'http://squidex.io/1', 1, 2, 3, 4, ['dump1']),
new WebhookDto('id2', 'schemaId2', 'token2', 'http://squidex.io/2', 5, 6, 7, 8, ['dump2'])
]);
}));
it('should make post request to create webhook',
inject([WebhooksService, HttpTestingController], (webhooksService: WebhooksService, httpMock: HttpTestingController) => {
const dto = new CreateWebhookDto('http://squidex.io/hook');
let webhook: WebhookCreatedDto | null = null;
webhooksService.postWebhook('my-app', 'my-schema', dto, version).subscribe(result => {
webhook = result;
});
const req = httpMock.expectOne('http://service/p/api/apps/my-app/schemas/my-schema/webhooks');
expect(req.request.method).toEqual('POST');
expect(req.request.headers.get('If-Match')).toBe(version.value);
req.flush({ id: 'id1', sharedSecret: 'token1' });
expect(webhook).toEqual(new WebhookCreatedDto('id1', 'token1'));
}));
it('should make delete request to delete webhook',
inject([WebhooksService, HttpTestingController], (webhooksService: WebhooksService, httpMock: HttpTestingController) => {
webhooksService.deleteWebhook('my-app', 'my-schema', '123', version).subscribe();
const req = httpMock.expectOne('http://service/p/api/apps/my-app/schemas/my-schema/webhooks/123');
expect(req.request.method).toEqual('DELETE');
expect(req.request.headers.get('If-Match')).toBe(version.value);
req.flush({ id: 'id1', sharedSecret: 'token1' });
}));
});
Loading…
Cancel
Save