@@ -15,7 +15,12 @@ export class PrestoClient {
1515 /**
1616 * Creates an instance of PrestoClient.
1717 * @param {PrestoClientConfig } config - Configuration object for the PrestoClient.
18+ * @param {Object } config.basicAuthorization - Optional object for basic authorization.
19+ * @param {Object } config.basicAuthorization.user - The basic auth user name.
20+ * @param {Object } config.basicAuthorization.password - The basic auth password.
21+ * @param {string } config.authorizationToken - An optional token to be sent in the authorization header. Takes precedence over the basic auth.
1822 * @param {string } config.catalog - The default catalog to be used.
23+ * @param {Record<string, string> } config.extraHeaders - Any extra headers to include in the API requests. Optional.
1924 * @param {string } config.host - The host address of the Presto server.
2025 * @param {number } config.interval - The polling interval in milliseconds for query status checks.
2126 * @param {number } config.port - The port number on which the Presto server is listening.
@@ -24,7 +29,19 @@ export class PrestoClient {
2429 * @param {string } [config.timezone] - The timezone to be used for the session. Optional.
2530 * @param {string } config.user - The username to be used for the Presto session.
2631 */
27- constructor ( { catalog, host, interval, port, schema, source, timezone, user } : PrestoClientConfig ) {
32+ constructor ( {
33+ basicAuthentication,
34+ authorizationToken,
35+ catalog,
36+ extraHeaders,
37+ host,
38+ interval,
39+ port,
40+ schema,
41+ source,
42+ timezone,
43+ user,
44+ } : PrestoClientConfig ) {
2845 this . baseUrl = `${ host || 'http://localhost' } :${ port || 8080 } /v1/statement`
2946 this . catalog = catalog
3047 this . interval = interval
@@ -46,7 +63,19 @@ export class PrestoClient {
4663 this . headers [ 'X-Presto-Time-Zone' ] = this . timezone
4764 }
4865
49- // TODO: Set up auth
66+ if ( authorizationToken ) {
67+ this . headers [ 'Authorization' ] = `Bearer ${ authorizationToken } `
68+ } else if ( basicAuthentication ) {
69+ // Note this is only available for Node.js
70+ this . headers [ 'Authorization' ] = `Basic ${ Buffer . from (
71+ `${ basicAuthentication . user } :${ basicAuthentication . password } ` ,
72+ ) . toString ( 'base64' ) } `
73+ }
74+
75+ this . headers = {
76+ ...extraHeaders ,
77+ ...this . headers ,
78+ }
5079 }
5180
5281 /**
@@ -197,7 +226,7 @@ export class PrestoClient {
197226 const data = [ ]
198227
199228 do {
200- const response = await this . request ( { method : 'GET' , url : nextUri } )
229+ const response = await this . request ( { headers , method : 'GET' , url : nextUri } )
201230
202231 // Server is overloaded, wait a bit
203232 if ( response . status === 503 ) {
0 commit comments