-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
addd custom subscription transport to ea generator (#241)
- Loading branch information
1 parent
0975737
commit e2b09c3
Showing
6 changed files
with
156 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
scripts/generator-adapter/generators/app/templates/src/transport/custombg.ts.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { TransportDependencies } from '@chainlink/external-adapter-framework/transports' | ||
import { ResponseCache } from '@chainlink/external-adapter-framework/cache/response' | ||
import { Requester } from '@chainlink/external-adapter-framework/util/requester' | ||
import { | ||
AdapterResponse, sleep | ||
} from '@chainlink/external-adapter-framework/util' | ||
import { SubscriptionTransport } from '@chainlink/external-adapter-framework/transports/abstract/subscription' | ||
import { EndpointContext } from '@chainlink/external-adapter-framework/adapter' | ||
import { BaseEndpointTypes, inputParameters } from '../endpoint/<%= inputEndpointName %>' | ||
|
||
type RequestParams = typeof inputParameters.validated | ||
|
||
<% if (includeComments) { -%> | ||
// CustomTransport extends base types from endpoint and adds additional, Provider-specific types (if needed). | ||
<% } -%> | ||
export type CustomTransportTypes = BaseEndpointTypes & { | ||
Provider: { | ||
RequestBody: never | ||
ResponseBody: any | ||
} | ||
} | ||
<% if (includeComments) { -%> | ||
// CustomTransport is used to perform custom data fetching and processing from a Provider. The framework provides built-in transports to | ||
// fetch data from a Provider using several protocols, including `http`, `websocket`, and `sse`. Use CustomTransport when the Provider uses | ||
// different protocol, or you need custom functionality that built-in transports don't support. For example, custom, multistep authentication | ||
// for requests, paginated requests, on-chain data retrieval using third party libraries, and so on. | ||
<% } -%> | ||
export class CustomTransport extends SubscriptionTransport<CustomTransportTypes> { | ||
<% if (includeComments) { -%> | ||
// name of the transport, used for logging | ||
<% } -%> | ||
name!: string | ||
<% if (includeComments) { -%> | ||
// cache instance for caching responses from provider | ||
<% } -%> | ||
responseCache!: ResponseCache<CustomTransportTypes> | ||
<% if (includeComments) { -%> | ||
// instance of Requester to be used for data fetching. Use this instance to perform http calls | ||
<% } -%> | ||
requester!: Requester | ||
|
||
<% if (includeComments) { -%> | ||
// REQUIRED. Transport will be automatically initialized by the framework using this method. It will be called with transport | ||
// dependencies, adapter settings, endpoint name, and transport name as arguments. Use this method to initialize transport state | ||
<% } -%> | ||
async initialize(dependencies: TransportDependencies<CustomTransportTypes>, adapterSettings: CustomTransportTypes['Settings'], endpointName: string, transportName: string): Promise<void> { | ||
await super.initialize(dependencies, adapterSettings, endpointName, transportName) | ||
this.requester = dependencies.requester | ||
} | ||
<% if (includeComments) { -%> | ||
// 'backgroundHandler' is called on each background execution iteration. It receives endpoint context as first argument | ||
// and an array of all the entries in the subscription set as second argument. Use this method to handle the incoming | ||
// request, process it and save it in the cache. | ||
<% } -%> | ||
async backgroundHandler(context: EndpointContext<CustomTransportTypes>, entries: RequestParams[]) { | ||
await Promise.all(entries.map(async (param) => this.handleRequest(param))) | ||
await sleep(context.adapterSettings.BACKGROUND_EXECUTE_MS) | ||
} | ||
|
||
async handleRequest(param: RequestParams) { | ||
let response: AdapterResponse<CustomTransportTypes['Response']> | ||
try { | ||
response = await this._handleRequest(param) | ||
} catch (e) { | ||
const errorMessage = e instanceof Error ? e.message : 'Unknown error occurred' | ||
response = { | ||
statusCode: 502, | ||
errorMessage, | ||
timestamps: { | ||
providerDataRequestedUnixMs: 0, | ||
providerDataReceivedUnixMs: 0, | ||
providerIndicatedTimeUnixMs: undefined, | ||
}, | ||
} | ||
} | ||
await this.responseCache.write(this.name, [{ params: param, response }]) | ||
} | ||
|
||
async _handleRequest( | ||
_: RequestParams, | ||
): Promise<AdapterResponse<CustomTransportTypes['Response']>> { | ||
|
||
const providerDataRequestedUnixMs = Date.now() | ||
|
||
// custom transport logic | ||
|
||
return { | ||
data: { | ||
result: 2000, | ||
}, | ||
statusCode: 200, | ||
result: 2000, | ||
timestamps: { | ||
providerDataRequestedUnixMs, | ||
providerDataReceivedUnixMs: Date.now(), | ||
providerIndicatedTimeUnixMs: undefined, | ||
}, | ||
} | ||
} | ||
|
||
getSubscriptionTtlFromConfig(adapterSettings: CustomTransportTypes['Settings']): number { | ||
return adapterSettings.WARMUP_SUBSCRIPTION_TTL | ||
} | ||
} | ||
|
||
export const customSubscriptionTransport = new CustomTransport() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters