Skip to content

Commit

Permalink
feat(client): allow basic and custom auth, pass extra headers
Browse files Browse the repository at this point in the history
  • Loading branch information
alonsovb committed Jan 18, 2024
1 parent a35bf9d commit f9efb3b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
35 changes: 32 additions & 3 deletions presto-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export class PrestoClient {
/**
* Creates an instance of PrestoClient.
* @param {PrestoClientConfig} config - Configuration object for the PrestoClient.
* @param {Object} config.basicAuthorization - Optional object for basic authorization.
* @param {Object} config.basicAuthorization.user - The basic auth user name.
* @param {Object} config.basicAuthorization.password - The basic auth password.
* @param {string} config.authorizationToken - An optional token to be sent in the authorization header. Takes precedence over the basic auth.
* @param {string} config.catalog - The default catalog to be used.
* @param {Record<string, string>} config.extraHeaders - Any extra headers to include in the API requests. Optional.
* @param {string} config.host - The host address of the Presto server.
* @param {number} config.interval - The polling interval in milliseconds for query status checks.
* @param {number} config.port - The port number on which the Presto server is listening.
Expand All @@ -24,7 +29,19 @@ export class PrestoClient {
* @param {string} [config.timezone] - The timezone to be used for the session. Optional.
* @param {string} config.user - The username to be used for the Presto session.
*/
constructor({ catalog, host, interval, port, schema, source, timezone, user }: PrestoClientConfig) {
constructor({
basicAuthorization,
authorizationToken,
catalog,
extraHeaders,
host,
interval,
port,
schema,
source,
timezone,
user,
}: PrestoClientConfig) {
this.baseUrl = `${host || 'http://localhost'}:${port || 8080}/v1/statement`
this.catalog = catalog
this.interval = interval
Expand All @@ -46,7 +63,19 @@ export class PrestoClient {
this.headers['X-Presto-Time-Zone'] = this.timezone
}

// TODO: Set up auth
if (authorizationToken) {
this.headers['Authorization'] = authorizationToken
} else if (basicAuthorization) {
// Note this is only available for Node.js
this.headers['Authorization'] = `Bearer ${Buffer.from(
`${basicAuthorization.user}:${basicAuthorization.password}`,
).toString('base64')}`
}

this.headers = {
...extraHeaders,
...this.headers,
}
}

/**
Expand Down Expand Up @@ -197,7 +226,7 @@ export class PrestoClient {
const data = []

do {
const response = await this.request({ method: 'GET', url: nextUri })
const response = await this.request({ headers, method: 'GET', url: nextUri })

// Server is overloaded, wait a bit
if (response.status === 503) {
Expand Down
6 changes: 6 additions & 0 deletions presto-client/src/client.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export interface PrestoClientConfig {
authorizationToken?: string
basicAuthorization?: {
user: string
password: string
}
catalog?: string
extraHeaders?: Record<string, string>
host?: string
interval?: number
port?: number
Expand Down

0 comments on commit f9efb3b

Please sign in to comment.