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: allow changing backoff and limiter per request #208

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -8,7 +8,6 @@ import {
DEFAULT_COW_API_CONTEXT,
ENVS_LIST,
PartialApiContext,
RequestOptions,
} from '../common/configs'
import { CowError } from '../common/cow-error'
import {
Expand Down Expand Up @@ -128,15 +127,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 @@ -388,7 +387,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 @@ -410,10 +409,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)
}
}
Loading