Skip to content

Commit 754a9a6

Browse files
committed
first implementation of a concrete client and its factory
1 parent bf1ef20 commit 754a9a6

File tree

13 files changed

+134
-36
lines changed

13 files changed

+134
-36
lines changed

src/Client.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { account } from './actions/account'
2+
import { ClientAccountBalance } from './client/ClientAccountBalance'
3+
import { Address } from './entities/Address'
24
import { ApiKey } from './entities/Apikey'
35

46
export class Client {
@@ -7,10 +9,20 @@ export class Client {
79
this.apiKey = new ApiKey(apiKey)
810
this.apiKey.validate()
911
}
10-
account(action: string): void {
12+
13+
account(action: string): any {
1114
if (!account.get(action)) {
1215
throw new Error('unknown action')
1316
}
14-
return
17+
18+
var me = this;
19+
const actions:{ [key: string]: any } = {
20+
'balance': (address: string, tag: string): ClientAccountBalance => {
21+
const oAddress = new Address(address)
22+
return new ClientAccountBalance(this.apiKey, 'account', action, oAddress, tag)
23+
},
24+
}
25+
26+
return actions[action]
1527
}
1628
}

src/client/ClientAccountBalance.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Address } from '../entities/Address'
2+
import { ApiKey } from '../entities/Apikey'
3+
import { IClientAccountBalanceRequest } from '../interfaces/Account'
4+
5+
import { requestBuilder } from '../requestBuilder'
6+
7+
export class ClientAccountBalance implements IClientAccountBalanceRequest {
8+
apiKey: ApiKey
9+
address: Address
10+
module: string
11+
action: string
12+
tag: string
13+
14+
constructor(apiKey: ApiKey, module: string, action: string, address: Address, tag: string) {
15+
this.apiKey = apiKey
16+
this.module = module
17+
this.action = action
18+
this.address = address
19+
this.tag = tag
20+
}
21+
22+
toUrl(): string {
23+
return requestBuilder(this.module, this.action, {
24+
address: this.address.toString(),
25+
tag: this.tag.toString(),
26+
apiKey: this.apiKey.toString()
27+
})
28+
}
29+
30+
}

src/entities/Address.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import { EntityBase } from './EntityBase'
44

55
// a ethereum address
66
export class Address extends EntityBase implements IEntity {
7-
private address: string
7+
88
private regex = /0x[a-fA-F0-9]{40}/
99

1010
constructor(address: string) {
11-
super()
11+
super(address)
1212
this.errorMessage = 'Invalid Address'
13-
this.address = address
1413
}
1514

1615
valid(): boolean {
17-
return this.address.match(this.regex) !== null
16+
return this.value.match(this.regex) !== null
1817
}
1918
}

src/entities/Apikey.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import { EntityBase } from './EntityBase'
55
// api key for etherscan.io
66
export class ApiKey extends EntityBase implements IEntity {
77
errorMessage: string = 'invalid'
8-
private apiKey: string
98
private keyLength: number = 34
109
constructor(apiKey: string) {
11-
super()
12-
this.apiKey = apiKey
10+
super(apiKey)
1311
}
1412

1513
valid(): boolean {
16-
return this.apiKey.length === this.keyLength
14+
return this.value.length === this.keyLength
1715
}
1816
}

src/entities/EntityBase.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
// base class for entities
33
export abstract class EntityBase {
4+
protected value: any
45
// basic error message for exceptions on validations
56
errorMessage: string
67
// checks valid() and throws errorMessage
7-
constructor() {
8+
constructor(value: any) {
9+
this.value = value
810
this.errorMessage = 'invalid value'
911
}
1012
validate(): void {
@@ -13,6 +15,10 @@ export abstract class EntityBase {
1315
}
1416
}
1517

18+
toString(): string {
19+
return this.value.toString()
20+
}
21+
1622
valid(): boolean {
1723
return true
1824
}

src/entities/HexQuantity.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ import { EntityBase } from './EntityBase'
66
*/
77
export class HexQuantity extends EntityBase implements IEntity {
88
errorMessage: string = 'invalid'
9-
private hexString: string
10-
119
constructor(hexString: string) {
12-
super()
13-
this.hexString = hexString
10+
super(hexString)
1411
}
1512

1613
valid(): boolean {
17-
const a = parseInt(this.hexString, 16)
18-
return (a.toString(16) === this.hexString)
14+
const a = parseInt(this.value, 16)
15+
return (a.toString(16) === this.value)
1916
}
2017
}

src/entities/PositiveNumber.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import { EntityBase } from './EntityBase'
55
// a positive number
66
export class PositiveNumber extends EntityBase implements IEntity {
77
errorMessage: string
8-
private positiveNumber: any
98

109
constructor(positiveNumber: any) {
11-
super()
10+
super(positiveNumber)
1211
this.errorMessage = 'invalid number'
13-
this.positiveNumber = positiveNumber
12+
this.value = positiveNumber
1413
}
1514

1615
valid(): boolean {
1716

18-
const numerical = parseInt(this.positiveNumber, 3)
17+
const numerical = parseInt(this.value, 3)
1918

2019
if (numerical < 0) {
2120
return false

src/entities/Sort.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import { EntityBase } from './EntityBase'
66
// Sort Parameter asc or desc
77
export class Sort extends EntityBase implements IEntity {
88
errorMessage: string = 'invalid'
9-
private sortParam: string
109

1110
constructor(sortParam: string) {
12-
super()
13-
this.sortParam = sortParam
11+
super(sortParam)
12+
1413
}
1514

1615
valid(): boolean {
17-
return sort.asc === this.sortParam || sort.desc === this.sortParam
16+
return sort.asc === this.value || sort.desc === this.value
1817
}
1918
}

src/interfaces/Entity.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ApiKey } from '../entities/Apikey'
22

3-
export interface IEntity {
4-
validate(): void
5-
valid(): boolean
6-
}
7-
83
export interface IClientRequest {
94
module: string
105
action: string
116
apiKey: ApiKey
127
}
8+
9+
export interface IEntity {
10+
validate(): void
11+
valid(): boolean
12+
}

src/requestBuilder.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ actions.set('stats', stats)
2323
actions.set('tokens', tokens)
2424
actions.set('transaction', transaction)
2525

26-
export const requestBuilder = (module: string, action: string): string => {
26+
export const requestBuilder = (module: string, action: string, params?: object): string => {
2727

2828
if (!modules.get(module)) {
2929
throw Error('unknown module')
@@ -33,10 +33,13 @@ export const requestBuilder = (module: string, action: string): string => {
3333
throw Error('unknown action')
3434
}
3535

36-
const query: string = encode({
36+
const baseParams = {
3737
action,
3838
module,
39-
})
39+
}
4040

41+
const toEncodeParams = Object.assign(baseParams, params)
42+
const query: string = encode(toEncodeParams)
43+
4144
return `${base}?${query}`
4245
}

0 commit comments

Comments
 (0)