Classify Http & Ws providers (#120)

* Class-ify HttpProvider

* Class-ify WsProvider (tests still WIP)

* Rework tests

* rx-api usage
This commit is contained in:
Jaco Greeff
2018-08-07 16:59:40 +02:00
committed by GitHub
parent f74fd9e01d
commit fe4c34defe
38 changed files with 342 additions and 531 deletions
+14 -2
View File
@@ -4,13 +4,25 @@
import { TEST_HTTP_URL } from '../../test/mockHttp';
import createHttp from './index';
import Http from './index';
describe('Http', () => {
let http;
beforeEach(() => {
http = createHttp(TEST_HTTP_URL);
http = new Http(TEST_HTTP_URL);
});
it('requires an http:// prefixed endpoint', () => {
expect(
() => new Http('ws://')
).toThrow(/with 'http/);
});
it('allows https:// endpoints', () => {
expect(
() => new Http('https://')
).not.toThrow();
});
it('always returns isConnected true', () => {
+52 -19
View File
@@ -2,29 +2,62 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Logger } from '@polkadot/util/types';
import { RpcCoder } from '../coder/json/types';
import { ProviderInterface, ProviderInterface$Callback, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
import './polyfill';
import on from './on';
import send from './send';
import subscribe from './subscribe';
import state from './state';
import unsubscribe from './unsubscribe';
import assert from '@polkadot/util/assert';
import logger from '@polkadot/util/logger';
export default function httpProvider (endpoint: string): ProviderInterface {
const self = state(endpoint);
import coder from '../coder/json';
return {
isConnected: (): boolean =>
true,
on: (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void =>
on(self, type, sub),
send: (method: string, params: Array<any>): Promise<any> =>
send(self, method, params),
subscribe: (method: string, params: Array<any>, cb: ProviderInterface$Callback): Promise<number> =>
subscribe(self, method, params, cb),
unsubscribe: (method: string, id: number): Promise<boolean> =>
unsubscribe(self, method, id)
};
export default class HttpProvider implements ProviderInterface {
private coder: RpcCoder;
private endpoint: string;
private l: Logger;
constructor (endpoint: string) {
assert(/^(https|http):\/\//.test(endpoint), `Endpoint should start with 'http://', received '${endpoint}'`);
this.coder = coder();
this.endpoint = endpoint;
this.l = logger('api-http');
}
isConnected (): boolean {
return true;
}
on (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
this.l.error(`Provider does not have 'on' emitters for type '${type}'`);
}
async send (method: string, params: Array<any>): Promise<any> {
const body = this.coder.encodeJson(method, params);
const response = await fetch(this.endpoint, {
body,
headers: {
'Accept': 'application/json',
'Content-Length': `${body.length}`,
'Content-Type': 'application/json'
},
method: 'POST'
});
assert(response.ok, `[${response.status}]: ${response.statusText}`);
const result = await response.json();
return this.coder.decodeResponse(result);
}
async subscribe (method: string, params: Array<any>, cb: ProviderInterface$Callback): Promise<number> {
throw new Error('Subscriptions has not been implemented');
}
async unsubscribe (method: string, id: number): Promise<boolean> {
throw new Error('Subscriptions has not been implemented');
}
}
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
import { HttpState } from './types';
export default function on (self: HttpState, type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
self.l.error(`Provider does not have 'on' emitters for type '${type}'`);
}
+2 -2
View File
@@ -4,14 +4,14 @@
import { mockHttp, TEST_HTTP_URL } from '../../test/mockHttp';
import createHttp from './index';
import Http from './index';
describe('send', () => {
let http;
let mock;
beforeEach(() => {
http = createHttp(TEST_HTTP_URL);
http = new Http(TEST_HTTP_URL);
});
afterEach(() => {
-26
View File
@@ -1,26 +0,0 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { HttpState } from './types';
import assert from '@polkadot/util/assert';
export default async function send ({ coder, endpoint }: HttpState, method: string, params: Array<any>): Promise<any> {
const body = coder.encodeJson(method, params);
const response = await fetch(endpoint, {
body,
headers: {
'Accept': 'application/json',
'Content-Length': `${body.length}`,
'Content-Type': 'application/json'
},
method: 'POST'
});
assert(response.ok, `[${response.status}]: ${response.statusText}`);
const result = await response.json();
return coder.decodeResponse(result);
}
@@ -1,19 +0,0 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import state from './state';
describe('state', () => {
it('requires an http:// prefixed endpoint', () => {
expect(
() => state('ws://')
).toThrow(/with 'http/);
});
it('allows https:// endpoints', () => {
expect(
state('https://')
).toBeDefined();
});
});
-22
View File
@@ -1,22 +0,0 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { HttpState } from './types';
import assert from '@polkadot/util/assert';
import logger from '@polkadot/util/logger';
import coder from '../coder/json';
const l = logger('api-http');
export default function state (endpoint: string): HttpState {
assert(/^(https|http):\/\//.test(endpoint), `Endpoint should start with 'http://', received '${endpoint}'`);
return {
coder: coder(),
endpoint,
l
};
}
@@ -1,10 +0,0 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { ProviderInterface$Callback } from '../types';
import { HttpState } from './types';
export default async function subscribe (self: HttpState, method: string, params: Array<any>, cb: ProviderInterface$Callback): Promise<number> {
throw new Error('Subscriptions has not been implemented');
}
@@ -1,9 +0,0 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { HttpState } from './types';
export default async function unsubscribe (self: HttpState, method: string, id: number): Promise<boolean> {
throw new Error('Subscriptions has not been implemented');
}