Skip to content

Commit

Permalink
feat: allow changing backoff and limiter per request (#208)
Browse files Browse the repository at this point in the history
* feat: allow changing backoff and limiter per request

* fix: use custom RateLimiter is limiterOpts are overridden

(cherry picked from commit 4368844)
  • Loading branch information
shoom3301 committed May 16, 2024
1 parent ca5a581 commit 90cc982
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/common/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export interface ApiContext {
chainId: SupportedChainId
env: CowEnv
baseUrls?: ApiBaseUrls
limiterOpts?: RateLimiterOpts
backoffOpts?: BackoffOptions
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/order-book/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
DEFAULT_COW_API_CONTEXT,
ENVS_LIST,
PartialApiContext,
RequestOptions,
} from '../common/configs'
import { transformOrder } from './transformOrder'
import { EnrichedOrder } from './types'
Expand Down Expand Up @@ -126,15 +125,15 @@ export type GetOrdersRequest = {
* @see {@link OrderBook API https://github.com/cowprotocol/services}
*/
export class OrderBookApi {
public context: ApiContext & RequestOptions
public context: ApiContext

private rateLimiter: RateLimiter

/**
* Creates a new instance of the CoW Protocol OrderBook API client.
* @param context - The API context to use. If not provided, the default context will be used.
*/
constructor(context: PartialApiContext & RequestOptions = {}) {
constructor(context: PartialApiContext = {}) {
this.context = { ...DEFAULT_COW_API_CONTEXT, ...context }
this.rateLimiter = new RateLimiter(context.limiterOpts || DEFAULT_LIMITER_OPTIONS)
}
Expand Down Expand Up @@ -386,7 +385,7 @@ export class OrderBookApi {
* @param contextOverride Optional context override for this request.
* @returns New context with the override applied.
*/
private getContextWithOverride(contextOverride: PartialApiContext = {}): ApiContext & RequestOptions {
private getContextWithOverride(contextOverride: PartialApiContext = {}): ApiContext {
return { ...this.context, ...contextOverride }
}

Expand All @@ -408,10 +407,11 @@ export class OrderBookApi {
* @returns The response from the API.
*/
private fetch<T>(params: FetchParams, contextOverride: PartialApiContext = {}): Promise<T> {
const { chainId, env } = this.getContextWithOverride(contextOverride)
const { chainId, env, backoffOpts: _backoffOpts } = this.getContextWithOverride(contextOverride)
const baseUrl = this.getApiBaseUrls(env)[chainId]
const backoffOpts = this.context.backoffOpts || DEFAULT_BACKOFF_OPTIONS
const backoffOpts = _backoffOpts || DEFAULT_BACKOFF_OPTIONS
const rateLimiter = contextOverride.limiterOpts ? new RateLimiter(contextOverride.limiterOpts) : this.rateLimiter

return request(baseUrl, params, this.rateLimiter, backoffOpts)
return request(baseUrl, params, rateLimiter, backoffOpts)
}
}

0 comments on commit 90cc982

Please sign in to comment.