-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): add
connect
function to use remote azot API instance
- Loading branch information
1 parent
74c7953
commit 0765f89
Showing
4 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
type ConnectParams = { | ||
baseUrl: string; | ||
secret: string; | ||
}; | ||
|
||
export const connect = ({ baseUrl, secret }: ConnectParams) => { | ||
const headers = { | ||
'x-secret-key': secret, | ||
'content-type': 'application/json', | ||
}; | ||
|
||
const json = (data: any) => JSON.stringify(data); | ||
|
||
const post = async (route: string, body: object) => { | ||
const response = await fetch(`${baseUrl}${route}`, { | ||
method: 'POST', | ||
headers, | ||
body: json(body), | ||
}); | ||
const contentLength = response.headers.get('content-length'); | ||
if (contentLength === '0') return; | ||
return response.json(); | ||
}; | ||
|
||
const get = async (route: string) => { | ||
const response = await fetch(`${baseUrl}${route}`, { | ||
method: 'GET', | ||
headers, | ||
}); | ||
return response.json(); | ||
}; | ||
|
||
const createSession = async (client: string) => { | ||
const data = await post(`/session`, { client }); | ||
const sessionId = data.id; | ||
|
||
const generateRequest = async (initData: string, initDataType: string) => { | ||
const data = await post(`/session/${sessionId}/generate-request`, { | ||
initDataType, | ||
initData, | ||
}); | ||
return data.licenseRequest; | ||
}; | ||
|
||
const update = async (response: string) => | ||
post(`/session/${sessionId}/update`, { response }); | ||
|
||
const keys = async () => get(`/session/${sessionId}/keys`); | ||
|
||
return { sessionId, generateRequest, update, keys }; | ||
}; | ||
|
||
return { createSession }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { expect, test } from 'vitest'; | ||
import { connect } from '../src/lib'; | ||
|
||
test('connection to Azot API instance and generating license request', async () => { | ||
const baseUrl = 'https://azot.pw'; // Set your API base URL here | ||
const secret = 'db44ec40-3e02-47bd-8fc6-373935e30eae'; // Set your API secret here | ||
const client = 'pixel6'; // Set client name related with your API key | ||
if (secret === 'db44ec40-3e02-47bd-8fc6-373935e30eae') | ||
return console.warn('Add your API endpoint & secret to test connection'); | ||
const { createSession } = connect({ baseUrl, secret }); | ||
const session = await createSession(client); | ||
const licenseRequest = await session.generateRequest( | ||
'AAAAW3Bzc2gAAAAA7e+LqXnWSs6jyCfc1R0h7QAAADsIARIQ62dqu8s0Xpa7z2FmMPGj2hoNd2lkZXZpbmVfdGVzdCIQZmtqM2xqYVNkZmFsa3IzaioCSEQyAA==', | ||
'cenc', | ||
); | ||
const licenseUrl = 'https://cwip-shaka-proxy.appspot.com/no_auth'; | ||
const license = await fetch(licenseUrl, { | ||
body: Buffer.from(licenseRequest, 'base64'), | ||
method: 'POST', | ||
}) | ||
.then((response) => response.arrayBuffer()) | ||
.then((buffer) => Buffer.from(buffer)); | ||
await session.update(license.toString('base64')); | ||
const keys = await session.keys(); | ||
expect(keys.length).toBe(5); | ||
}); |