Skip to content

Commit

Permalink
Merge pull request #221 from textileio/carson/context-interface
Browse files Browse the repository at this point in the history
Pulls out context interface + provider
  • Loading branch information
carsonfarmer committed Jan 7, 2021
1 parent 4c6efd6 commit 542ebaa
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 92 deletions.
5 changes: 3 additions & 2 deletions packages/js-threads/packages/client/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Identity, Libp2pCryptoIdentity } from '@textile/threads-core'

import { expect } from 'chai'
import { Provider } from '@textile/context'
import { Where, ReadTransaction, WriteTransaction } from './models'
import { Client, ThreadID } from './index'

Expand Down Expand Up @@ -55,7 +56,7 @@ describe('Client', function () {
let dbKey: string
let dbAddr: string
let identity: Identity
const client = new Client()
const client = new Client(new Provider('http://127.0.0.1:6007'))

before(async () => {
identity = await Libp2pCryptoIdentity.fromRandom()
Expand Down Expand Up @@ -308,7 +309,7 @@ describe('Client', function () {

describe('Restart', () => {
it('Should handle a whole new "restart" of the client', async () => {
const newClient = new Client()
const newClient = new Client(new Provider('http://127.0.0.1:6007'))
const person = createPerson()
await newClient.getToken(identity)
const created = await newClient.create(dbID, 'Person', [person])
Expand Down
4 changes: 2 additions & 2 deletions packages/js-threads/packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import nextTick from 'next-tick'
import { Identity, Libp2pCryptoIdentity } from '@textile/threads-core'
import { Multiaddr } from '@textile/multiaddr'
import { ThreadID } from '@textile/threads-id'
import { Context } from '@textile/context'
import { Context, Provider } from '@textile/context'
import { encode, decode } from 'bs58'
import {
QueryJSON,
Expand All @@ -37,7 +37,7 @@ export class Client {
* Creates a new gRPC client instance for accessing the Textile Threads API.
* @param context The context to use for interacting with the APIs. Can be modified later.
*/
constructor(public context: Context = new Context('http://127.0.0.1:6007')) {
constructor(public context: Context = new Provider()) {
this.serviceHost = context.host
this.rpcOptions = {
transport: context.transport,
Expand Down
4 changes: 2 additions & 2 deletions packages/js-threads/packages/context/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect } from 'chai'
import { Context, expirationError } from './index'
import { Provider, Context, expirationError } from './index'

describe('Context', () => {
it('should throw an exception when working with an expired msg', async () => {
const context = new Context()
const context: Context = new Provider()
try {
context.withAPISig({
sig: 'fake',
Expand Down
25 changes: 22 additions & 3 deletions packages/js-threads/packages/context/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,29 @@ export interface ContextKeys {
[key: string]: any
}

export interface Context {
host: HostString
debug: boolean
transport: grpc.TransportFactory
withSession(value?: string): Context
withThread(value?: ThreadID): Context
withThreadName(value?: string): Context
withOrg(value?: string): Context
withToken(value?: string): Context
withAPIKey(value?: string): Context
withAPISig(value?: { sig: string; msg: string }): Context
withContext(value?: Context): Context
toJSON(): any
toMetadata(): grpc.Metadata
withUserKey(key?: KeyInfo, date?: Date): Promise<Context>
set(key: keyof ContextKeys, value?: any): Context
get(key: keyof ContextKeys): any
}

/**
* Context provides context management for gRPC credentials and config settings.
* Provider provides context management for gRPC credentials and config settings.
*/
export class Context {
export class Provider implements Context {
// Internal context variables
private _context: Partial<Record<keyof ContextKeys, any>> = {}

Expand Down Expand Up @@ -208,7 +227,7 @@ export class Context {
newContext['host'] = host
newContext['transport'] = transport
newContext['debug'] = debug
const ctx = new Context()
const ctx = new Provider()
ctx._context = newContext
return ctx
}
Expand Down
4 changes: 2 additions & 2 deletions packages/js-threads/packages/database/src/db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import delay from 'delay'
import { isBrowser } from 'browser-or-node'
import { Key } from 'interface-datastore'
import { DomainDatastore, Dispatcher, Update, Op } from '@textile/threads-store'
import { Network, Client } from '@textile/threads-network'
import { Network, Client, Provider } from '@textile/threads-network'
import { MemoryDatastore } from 'interface-datastore'
import { Database, mismatchError } from './db'
import { EventBus } from './eventbus'
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Database', () => {
// have the same state of dummy.
const info = await d1.network.getThread(id1)
const datastore = new MemoryDatastore()
const client = new Client({ host: 'http://127.0.0.1:6207' })
const client = new Client(new Provider('http://127.0.0.1:6207'))
const network = new Network(new DomainDatastore(datastore, new Key('network')), client)
const d2 = new Database(datastore, { network })
const ident2 = await Database.randomIdentity()
Expand Down
63 changes: 0 additions & 63 deletions packages/js-threads/packages/network-client/src/config.ts

This file was deleted.

10 changes: 5 additions & 5 deletions packages/js-threads/packages/network-client/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Identity,
Libp2pCryptoIdentity,
} from '@textile/threads-core'
import { Context } from '@textile/context'
import { Provider } from '@textile/context'
import { createEvent, createRecord } from '@textile/threads-encoding'
import { Client } from '.'

Expand All @@ -40,7 +40,7 @@ describe('Network Client...', () => {
let token: string
let identity: Identity
before(async () => {
client = new Client(new Context(proxyAddr1))
client = new Client(new Provider(proxyAddr1))
identity = await Libp2pCryptoIdentity.fromRandom()
token = await client.getToken(identity)
})
Expand All @@ -64,7 +64,7 @@ describe('Network Client...', () => {
const info1 = await createThread(client)
const hostAddr = new Multiaddr('/dns4/threads1/tcp/4006')
const addr = threadAddr(hostAddr, hostID, info1)
const client2 = new Client(new Context(proxyAddr2))
const client2 = new Client(new Provider(proxyAddr2))
// Create temporary identity
const identity = await Libp2pCryptoIdentity.fromRandom()
const token2 = await client2.getToken(identity)
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('Network Client...', () => {
})

it('should add a replicator to a thread', async () => {
const client2 = new Client(new Context(proxyAddr2))
const client2 = new Client(new Provider(proxyAddr2))
const hostID2 = await client2.getHostID()
const hostAddr2 = new Multiaddr(`/dns4/threads2/tcp/4006`)

Expand Down Expand Up @@ -176,7 +176,7 @@ describe('Network Client...', () => {

before(async function () {
this.timeout(5000)
client2 = new Client(new Context(proxyAddr2))
client2 = new Client(new Provider(proxyAddr2))
const hostID2 = await client2.getHostID()
const hostAddr2 = new Multiaddr(`/dns4/threads2/tcp/4006`)
const peerAddr = hostAddr2.encapsulate(new Multiaddr(`/p2p/${hostID2}`))
Expand Down
10 changes: 3 additions & 7 deletions packages/js-threads/packages/network-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Identity,
Libp2pCryptoIdentity,
} from '@textile/threads-core'
import { Context, ContextKeys } from '@textile/context'
import { Context, ContextKeys, Provider } from '@textile/context'
import * as pb from '@textile/threads-net-grpc/threadsnet_pb'
import { API, APIGetToken } from '@textile/threads-net-grpc/threadsnet_pb_service'
import { recordFromProto, recordToProto } from '@textile/threads-encoding'
Expand Down Expand Up @@ -85,15 +85,11 @@ export class Client implements Network {
*/
public serviceHost: string
public rpcOptions: grpc.RpcOptions

/**
* Client creates a new gRPC client instance.
* Creates a new gRPC client instance for accessing the Textile Threads API.
* @param context The context to use for interacting with the APIs. Can be modified later.
*/
constructor(public context: Context | ContextKeys = new Context('http://127.0.0.1:6007')) {
if (!(context instanceof Context)) {
this.context = Context.fromJSON(context)
}
constructor(public context: Context = new Provider('http://127.0.0.1:6007')) {
this.serviceHost = context.host
this.rpcOptions = {
transport: context.transport,
Expand Down
2 changes: 1 addition & 1 deletion packages/js-threads/packages/network/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './store'
export * from './network'
export { Client } from '@textile/threads-network-client'
export { Context } from '@textile/context'
export { Context, Provider } from '@textile/context'
10 changes: 5 additions & 5 deletions packages/js-threads/packages/network/src/network.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
Libp2pCryptoIdentity,
} from '@textile/threads-core'
import { createEvent, createRecord } from '@textile/threads-encoding'
import { Context } from '@textile/context'
import { Provider } from '@textile/context'
import { Client } from '@textile/threads-network-client'
import { MemoryDatastore } from 'interface-datastore'
import { Network } from '.'
Expand All @@ -42,7 +42,7 @@ describe('Network...', () => {
let client: Network
before(async () => {
const identity = await Libp2pCryptoIdentity.fromRandom()
client = new Network(new MemoryDatastore(), new Client(new Context(proxyAddr1)))
client = new Network(new MemoryDatastore(), new Client(new Provider(proxyAddr1)))
const token = await client.getToken(identity)
expect(token).to.not.be.undefined
})
Expand All @@ -66,7 +66,7 @@ describe('Network...', () => {
const info1 = await createThread(client)
const hostAddr = new Multiaddr('/dns4/threads1/tcp/4006')
const addr = threadAddr(hostAddr, hostID, info1)
const client2 = new Client({ host: proxyAddr2 })
const client2 = new Client(new Provider(proxyAddr2))
// Create temporary identity
const identity = await Libp2pCryptoIdentity.fromRandom()
await client2.getToken(identity)
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('Network...', () => {
})

it('should add a replicator to a thread', async () => {
const client2 = new Client({ host: proxyAddr2 })
const client2 = new Client(new Provider(proxyAddr2))
const hostID2 = await client2.getHostID()
const hostAddr2 = new Multiaddr(`/dns4/threads2/tcp/4006`)

Expand Down Expand Up @@ -182,7 +182,7 @@ describe('Network...', () => {
let token2: string

before(async () => {
client2 = new Network(new MemoryDatastore(), new Client({ host: proxyAddr2 }))
client2 = new Network(new MemoryDatastore(), new Client(new Provider(proxyAddr2)))
const hostID2 = await client2.getHostID()
const hostAddr2 = new Multiaddr(`/dns4/threads2/tcp/4006`)
const peerAddr = hostAddr2.encapsulate(new Multiaddr(`/p2p/${hostID2}`))
Expand Down

0 comments on commit 542ebaa

Please sign in to comment.