12.7.0-alpha.0 • Published 12 hours ago

@jupiterone/integration-sdk-http-client v12.7.0-alpha.0

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
12 hours ago

@jupiterone/integration-sdk-http-client

The Base API Client is a foundational class designed to simplify interactions with RESTful APIs. It abstracts common tasks such as making HTTP requests, handling retries, and managing rate limits. Built with flexibility in mind, it allows for easy extension to accommodate specific API endpoints of different services.

Features

  • Automatic Retries: Implements an exponential backoff strategy for handling transient network and server errors.
  • Pagination Support: Includes utility methods for handling API pagination, making it easier to iterate over large data sets.
  • Rate Limit Handling: Monitors API usage against provided rate limit headers to prevent hitting API limits.
  • Token Bucket Rate Limiting: Optionally integrates a token bucket algorithm for fine-grained control over request rate limiting.
  • Extensible Authentication: Abstract method for setting authorization headers allows for flexible integration with different authentication mechanisms.
  • Error Handling: Provides a structured approach to handle and log API request errors, including support for custom error handling logic.

Installation

yarn add node-fetch @jupiterone/integration-sdk-http-client

Usage

To use the Base API Client, extend it to create a custom client for your specific API. Implement the getAuthorizationHeaders method to provide the necessary authentication headers for your API requests.

The getAuthorizationHeaders method doesn't need to be called manually, it's going to be called on the first request and saved and used for subsequent requests.

Below are the details of each option available in the BaseAPIClient constructor:

baseUrl (required)

  • Type: string
  • Description: The base URL for the API endpoints. All requests made through the client will use this URL as the base for constructing the full endpoint path. Exception: When a complete URL is sent to request or retryableRequest that will be used instead.

logger (required)

  • Type: IntegrationLogger
  • Description: An instance of IntegrationLogger used by the client to log messages. This is typically provided by the integration context and should support various log levels (e.g., info, warn, error).

retryOptions (optional)

  • Type: Partial<RetryOptions>
  • Description: Configuration options for controlling the behavior of request retries in case of failures. This is enabled by default.
    • maxAttempts: The maximum number of retry attempts.
    • delay: The initial delay between retries in milliseconds.
    • timeout: The maximum time to wait for a response before timing out.
    • factor: The factor by which the delay increases after each attempt (for exponential backoff).
    • handleError: A custom function invoked when an error occurs during a request attempt. This function allows for sophisticated error handling strategies, including conditional retries based on the type or severity of the error encountered.

logErrorBody (optional)

  • Type: boolean
  • Description: Indicates whether the body of a response should be logged when an error occurs. Defaults to false to prevent logging sensitive information.

rateLimitThrottling (optional)

  • Type: RateLimitThrottlingOptions
  • Description: Configuration options for handling API rate limits. A treshold needs to be specified to enable this feature.
    • threshold: A value between 0 and 1 indicating the percentage of rate limit utilization at which to start throttling requests.
    • resetMode: Specifies how to interpret the rate limit reset header. Options include 'remaining_epoch_s' and 'datetime_epoch_s'. Default is remaining_epoch_s.
    • rateLimitHeaders: Customizes the header names used to determine rate limits. Includes limit, remaining, and reset fields. Defaults are: ratelimit-limit, ratelimit-remaining and ratelimit-reset.

tokenBucket (optional)

  • Type: TokenBucketOptions
  • Description: Configuration options for a token bucket rate limiting mechanism.
    • maximumCapacity: The maximum number of tokens the bucket can hold.
    • refillRate: The rate at which tokens are added to the bucket in a second.

Example: Extending BaseAPIClient for a Custom API

import { BaseAPIClient } from '@jupiterone/integration-sdk-http-client';

class CustomAPIClient extends BaseAPIClient {
  private config: IntegrationConfig;

  constructor(config: IntegrationConfig, logger: IntegrationLogger) {
    super({
      baseUrl: `https://${config.accountId}.example.com/api`,
      logger,
    });
    this.config = config;
  }

  protected async getAuthorizationHeaders(): Record<string, string> {
    return {
      Authorization: `Bearer ${this.config.apiToken}`,
    };
  }

  // Add methods to interact with your specific API endpoints
  async fetchResource(): Promise<any> {
    const response = await this.retryableRequest('/your-api-endpoint');
    return await response.json();
  }
}

// Usage
const client = new CustomAPIClient(config, logger);
const data = await client.fetchResource();

There's some APIs that require to send a request first to get the Bearer token, that is also possible:

protected async getAuthorizationHeaders(): Promise<Record<string, string>> {
  const tokenResponse = await this.request('/oauth/token', {
    method: 'POST',
    body: {
      username: this.username,
      password: this.password,
    },
    authorize: false, // This is important to set when doing this request because otherwise `request()` is going to call this method going in a infinite loop.
  });
  const data = await tokenResponse.json();
  return {
    Authorization: `Bearer ${data.access_token}`,
  };
}

Pagination Support:

To effectively use pagination with the BaseAPIClient class, you'll generally follow a pattern where you make an initial request to an endpoint that supports pagination and then continue to fetch subsequent pages of data based on the information provided in the response of each request. This process is often necessary for APIs that return large sets of data in chunks (pages) to reduce load on their servers and improve response times.

The BaseAPIClient class provides an abstracted method, paginate, to facilitate the implementation of pagination in your API requests. Here's how you can use it:

You'll need to send 3 parameters to the paginate method:

  • An initial request configuration (endpoint and options).
  • A path to the items in the API response.
  • A callback function that determines the parameters for fetching the next page, based on the current response.
Example: Cursor-based pagination
async iterateResources(iteratee: (resource: any) => Promise<void>): Promise<void> {
  const baseEndpoint = '/resources?limit=100';
  const iterator = this.paginate(
    { endpoint: baseEndpoint },
    'data.items', // This is the path to access the array items, in this example we would iterate over the items in a response like { "data": { "items": [{ "id": 1, "name": "test-1" }, { "id": 2, "name": "test-2" }] } }
    (data) => {
      const { body } = data;
      // Assuming the nextPageData.body includes a `nextCursor` property
      const nextCursor = body.nextCursor;
      if (!nextCursor) {
        return; // No more pages
      }
      return {
        nextUrl: `${baseEndpoint}&cursor=${nextCursor}`,
      };
    }
  );

  for await (const resource of iterator) {
    await iteratee(resource);
  }
}
12.7.0-alpha.0

12 hours ago

12.6.0

9 days ago

12.5.1

10 days ago

12.4.1

22 days ago

12.4.0

24 days ago

12.3.0

1 month ago

12.3.1

1 month ago

12.2.7

1 month ago

12.2.5

2 months ago

12.2.3

2 months ago

12.2.4

2 months ago

12.2.2

2 months ago

12.2.1

2 months ago

12.2.0

2 months ago

12.1.0

2 months ago

12.0.1-alpha.2

3 months ago

12.0.0

3 months ago

11.8.0

4 months ago

11.7.0

4 months ago

11.7.1

4 months ago

11.4.0-alpha.0

4 months ago

11.5.2

4 months ago

11.4.0

5 months ago

9.8.1

10 months ago

9.8.0

10 months ago

10.0.0

9 months ago

10.4.0

8 months ago

11.2.0

6 months ago

9.9.0

10 months ago

10.7.0

8 months ago

10.7.1

7 months ago

10.3.0

8 months ago

9.9.2

9 months ago

9.9.3

9 months ago

9.11.1

9 months ago

11.1.0

7 months ago

10.6.0

8 months ago

9.6.2

10 months ago

9.6.0

10 months ago

10.2.0

8 months ago

9.10.0

9 months ago

11.0.2

7 months ago

11.0.3

7 months ago

11.0.0

7 months ago

10.5.0

8 months ago

10.5.1

8 months ago

10.5.3

8 months ago

9.7.0

10 months ago

10.1.0

8 months ago

11.3.1

5 months ago

9.4.1

11 months ago

9.4.0

12 months ago

8.38.0

1 year ago

8.41.0

12 months ago

9.1.0

1 year ago

9.5.0

11 months ago

9.3.1

12 months ago

9.3.0

12 months ago

8.39.0

1 year ago

8.40.0

12 months ago

9.2.0

1 year ago

9.0.0

1 year ago

8.32.0

1 year ago

8.34.0

1 year ago

8.30.5

1 year ago

8.36.0

1 year ago

8.31.0

1 year ago

8.31.1

1 year ago

8.33.0

1 year ago

8.33.1

1 year ago

8.35.0

1 year ago

8.37.0

1 year ago

9.0.0-beta.1

1 year ago

8.30.2

1 year ago

8.30.3

1 year ago

8.30.4

1 year ago

8.30.0

1 year ago

8.30.1

1 year ago

8.29.2

1 year ago

8.29.3

1 year ago

8.29.0

1 year ago

8.28.1

1 year ago

8.26.0

2 years ago

8.27.0

2 years ago

8.28.0

2 years ago

8.27.1

2 years ago

8.23.0

2 years ago

8.24.0

2 years ago

8.23.1

2 years ago

8.25.0

2 years ago

8.24.1

2 years ago

8.22.3

2 years ago

8.25.1

2 years ago

8.22.4

2 years ago

8.22.5

2 years ago

8.22.6

2 years ago

8.22.7

2 years ago

8.14.0

2 years ago

8.16.0

2 years ago

8.15.0

2 years ago

8.14.1

2 years ago

8.18.0

2 years ago

8.17.0

2 years ago

8.20.0

2 years ago

8.21.0

2 years ago

8.22.0

2 years ago

8.22.2

2 years ago

8.19.0

2 years ago

8.18.1

2 years ago

8.13.14

2 years ago

8.13.10

2 years ago

8.13.2

2 years ago

8.13.11

2 years ago

8.13.4

2 years ago

8.13.7

2 years ago

8.13.6

2 years ago

8.13.9

2 years ago

8.13.8

2 years ago

8.13.1

2 years ago

8.13.0

2 years ago