Skip to content

Commit 438e76b

Browse files
committed
[wip] update brand list act
1 parent 2c0de52 commit 438e76b

File tree

3 files changed

+49
-33
lines changed

3 files changed

+49
-33
lines changed

src/tillo-provider.ts

+41-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* Copyright © 2022-2023 Seneca Project Contributors, MIT License. */
22

3+
const crypto = require('crypto');
34

45
const Pkg = require('../package.json')
56

@@ -11,6 +12,20 @@ type TilloProviderOptions = {
1112
debug: boolean
1213
}
1314

15+
type TilloSignatureOptions = {
16+
apiKey: string
17+
apiSecret: string
18+
method: string
19+
path?: string
20+
timestamp: string
21+
}
22+
23+
24+
function getAuthSignature(signData: TilloSignatureOptions) {
25+
const authSign = `${signData.apiKey}-${signData.method}-${signData.path}-${signData.timestamp}`
26+
const hashedSign = crypto.createHmac('sha256', signData.apiSecret).update(authSign).digest('hex')
27+
return hashedSign
28+
}
1429

1530
function TilloProvider(this: any, options: TilloProviderOptions) {
1631
const seneca: any = this
@@ -71,10 +86,24 @@ function TilloProvider(this: any, options: TilloProviderOptions) {
7186
cmd: {
7287
list: {
7388
action: async function(this: any, entize: any, msg: any) {
89+
const path = "brands"
90+
const timestamp = new Date().getTime().toString()
91+
const options: TilloSignatureOptions = {
92+
apiKey: this.shared.headers["API-Key"],
93+
method: "GET",
94+
path,
95+
timestamp,
96+
apiSecret: this.shared.secret
97+
}
98+
99+
this.shared.headers.Signature = getAuthSignature(options)
100+
this.shared.headers.Timestamp = timestamp
101+
this.shared.headers.Accept = "application/json"
102+
74103
let json: any =
75-
await getJSON(makeUrl('catalogs', msg.q), makeConfig())
76-
let brands = json.brands
77-
let list = brands.map((data: any) => entize(data))
104+
await getJSON(makeUrl(path, msg.q), makeConfig())
105+
let brands = json.data.brands
106+
let list = Object.entries(brands).map(([name, value]: any) => entize({ name, value }))
78107
return list
79108
},
80109
}
@@ -124,22 +153,15 @@ function TilloProvider(this: any, options: TilloProviderOptions) {
124153
let res =
125154
await this.post('sys:provider,get:keymap,provider:tillo')
126155

127-
// if (!res.ok) {
128-
// throw this.fail('keymap')
129-
// }
130-
//
131-
// let src = res.keymap.name.value + ':' + res.keymap.key.value
132-
// let auth = Buffer.from(src).toString('base64')
133-
//
134-
// this.shared.headers = {
135-
// Authorization: 'Basic ' + auth
136-
// }
137-
//
138-
// this.shared.primary = {
139-
// customerIdentifier: res.keymap.cust.value,
140-
// accountIdentifier: res.keymap.acc.value,
141-
// }
156+
if (!res.ok) {
157+
throw this.fail('keymap')
158+
}
159+
160+
this.shared.headers = {
161+
'API-Key': res.keymap.key.value,
162+
}
142163

164+
this.shared.secret = res.keymap.secret.value
143165
})
144166

145167

@@ -154,7 +176,7 @@ function TilloProvider(this: any, options: TilloProviderOptions) {
154176
const defaults: TilloProviderOptions = {
155177

156178
// NOTE: include trailing /
157-
url: 'https://integration-api.tillo.com/raas/v2/',
179+
url: 'https://sandbox.tillo.dev/api/v2/',
158180

159181
// Use global fetch by default - if exists
160182
fetch: ('undefined' === typeof fetch ? undefined : fetch),

test/local-env-template.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
module.exports = {
2-
TANGOCARD_NAME: '<NAME>',
3-
TANGOCARD_KEY: '<KEY>',
4-
TANGOCARD_CUSTID: '<customerIdentifier>',
5-
TANGOCARD_ACCID: '<accountIdentifier>',
2+
TILLO_KEY: '<KEY>',
3+
TILLO_SECRET: '<SECRET>',
64
}

test/tillo-provider.test.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('tillo-provider', () => {
4848
// const seneca = await makeSeneca()
4949
//
5050
// const list = await seneca.entity("provider/tillo/brand").list$()
51-
// // console.log('BRANDS', list)
51+
// console.log('BRANDS', list)
5252
//
5353
// expect(list.length > 0).toBeTruthy()
5454
// })
@@ -63,22 +63,18 @@ async function makeSeneca() {
6363
.use('entity')
6464
.use('env', {
6565
// debug: true,
66-
file: [__dirname + '/local-env.js;?'],
66+
file: [__dirname + '/local-config.js;?'],
6767
var: {
68-
// $TANGOCARD_KEY: String,
69-
// $TANGOCARD_NAME: String,
70-
// $TANGOCARD_CUSTID: String,
71-
// $TANGOCARD_ACCID: String,
68+
$TILLO_KEY: String,
69+
$TILLO_SECRET: String,
7270
}
7371
})
7472
.use('provider', {
7573
provider: {
7674
tillo: {
7775
keys: {
78-
// key: { value: '$TANGOCARD_KEY' },
79-
// name: { value: '$TANGOCARD_NAME' },
80-
// cust: { value: '$TANGOCARD_CUSTID' },
81-
// acc: { value: '$TANGOCARD_ACCID' },
76+
key: { value: '$TILLO_KEY' },
77+
secret: { value: '$TILLO_SECRET' },
8278
}
8379
}
8480
}

0 commit comments

Comments
 (0)