Skip to content

Commit

Permalink
ULMS-3167 Updated api-clients, refactored api routes, removed deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkonst committed Aug 1, 2024
1 parent abebb19 commit 96b943c
Show file tree
Hide file tree
Showing 19 changed files with 467 additions and 605 deletions.
52 changes: 19 additions & 33 deletions src/basic-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ const parseParameter = (key, value) => {
return `${key}=${value}`
}

const responseTransformer = (_) => _.data
async function handleResponse() {
throw new Error('Method `handleResponse` not implemented.')
}

class BasicClient {
constructor(baseUrl, httpClient, tokenProvider) {
this.baseUrl = baseUrl
this.customHeaders = {}
this.httpClient = httpClient
this.tokenProvider = tokenProvider
this.labels = {}
this.trackEvent = undefined
this.handleResponse = handleResponse
}

url(endpoint, parameters) {
Expand All @@ -44,7 +46,7 @@ class BasicClient {

// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(parameters)) {
if (value !== undefined) {
if (value !== undefined && value !== null) {
urlParameters += urlParameters
? `&${parseParameter(key, value)}`
: parseParameter(key, value)
Expand All @@ -59,38 +61,22 @@ class BasicClient {
return `${this.baseUrl}${endpoint}`
}

static headers(token, labels = {}, headers = {}) {
static headers(token, headers = {}) {
return {
...headers,
authorization: `Bearer ${token}`,
'content-type': 'application/json',
...labels,
}
}

setHeaders(headers) {
this.customHeaders = headers
}

setLabels(labels) {
const { app_audience, app_label, app_version, scope } = labels // eslint-disable-line camelcase

this.labels = {
...(app_audience !== undefined && { 'ulms-app-audience': app_audience }), // eslint-disable-line camelcase
...(app_label !== undefined && { 'ulms-app-label': app_label }), // eslint-disable-line camelcase
...(app_version !== undefined && { 'ulms-app-version': app_version }), // eslint-disable-line camelcase
...(scope !== undefined && { 'ulms-scope': scope }),
}
}

setTrackEventFunction(trackEvent) {
this.trackEvent = trackEvent
}

clearLabels() {
this.labels = {}
}

async get(url, options = {}) {
const { retry: retryEnabled } = options

Expand All @@ -99,13 +85,13 @@ class BasicClient {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient
.get(url, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)
}

return retry(task, onRetry)
Expand All @@ -114,31 +100,31 @@ class BasicClient {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient.get(url, requestOptions).then(responseTransformer)
return this.httpClient.get(url, requestOptions).then(this.handleResponse)
}

async put(url, data, options = {}) {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient
.put(url, data, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)
}

async post(url, data, options = {}) {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

Expand All @@ -151,7 +137,7 @@ class BasicClient {
: undefined
const result = this.httpClient
.post(url, data, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)

result.catch((error) => {
const responseEnd = Date.now()
Expand All @@ -174,31 +160,31 @@ class BasicClient {

return this.httpClient
.post(url, data, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)
}

async patch(url, data, options = {}) {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient
.patch(url, data, requestOptions)
.then(responseTransformer)
.then(this.handleResponse)
}

async delete(url, options = {}) {
const token = await this.tokenProvider.getToken()
const headers = {
...options.headers,
...BasicClient.headers(token, this.labels, this.customHeaders),
...BasicClient.headers(token, this.customHeaders),
}
const requestOptions = { ...options, headers }

return this.httpClient.delete(url, requestOptions).then(responseTransformer)
return this.httpClient.delete(url, requestOptions).then(this.handleResponse)
}
}

Expand Down
55 changes: 0 additions & 55 deletions src/conference.js

This file was deleted.

Loading

0 comments on commit 96b943c

Please sign in to comment.