Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(presto-client): allow basic authentication #19

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions presto-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ The Presto client can be configured with the following parameters:
- `schema`: The default schema to use for queries. (Default: `undefined`)
- `source`: The name of the source you want to use for reporting purposes (Default: `presto-js-client`)
- `timezone`: The timezone to use for queries. (Default: `undefined`)
- `authorizationToken`: The value to send as-is in the Authorization header. (Default: `undefined`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the same order that we have in the client's constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case I think I rather have params in order of relevance for anyone using the client 🤔

- `basicAuthentication`: An object with a user and password inside, to be used for basic authentication. (Default: `undefined`)
- `extraHeaders`: An dictionary of key-values to send as extra headers in all requests to the API. (Default: `undefined`)
- `interval`: (DEPRECATED) The interval in milliseconds between checks for the status of a running query. (Default: `100`)

## Querying
Expand Down
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({
basicAuthentication,
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 (basicAuthentication) {
// Note this is only available for Node.js
this.headers['Authorization'] = `Bearer ${Buffer.from(
`${basicAuthentication.user}:${basicAuthentication.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
basicAuthentication?: {
user: string
password: string
}
catalog?: string
extraHeaders?: Record<string, string>
host?: string
interval?: number
port?: number
Expand Down
Loading