Skip to content

Commit

Permalink
Implement processing v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Nov 27, 2023
1 parent c8fae71 commit 662f48d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.
8 changes: 4 additions & 4 deletions packages/e2e/src/pusher/pusher.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@
{ "name": "_type", "fixed": "int256" },
{ "name": "_times", "fixed": "1000000000000000000" }
],
"preProcessingSpecifications": [
"preProcessingSpecificationV2": [
{
"environment": "Node",
"value": "const output = {};",
"value": "() => { return { endpointParameters: {} }; }",
"timeoutMs": 5000
}
],
"postProcessingSpecifications": [
"postProcessingSpecificationV2": [
{
"environment": "Node",
"value": "const output = input[endpointParameters.name];",
"value": "({ endpointParameters, response }) => { return { response: response[endpointParameters.name].value }; }",
"timeoutMs": 5000
}
]
Expand Down
8 changes: 4 additions & 4 deletions packages/pusher/config/pusher.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@
{ "name": "_type", "fixed": "int256" },
{ "name": "_times", "fixed": "1000000000000000000" }
],
"preProcessingSpecifications": [
"preProcessingSpecificationV2": [
{
"environment": "Node",
"value": "const output = {};",
"value": "() => { return { endpointParameters: {} }; }",
"timeoutMs": 5000
}
],
"postProcessingSpecifications": [
"postProcessingSpecificationV2": [
{
"environment": "Node",
"value": "const output = input[endpointParameters.name].value;",
"value": "({ endpointParameters, response }) => { return { response: response[endpointParameters.name].value }; }",
"timeoutMs": 5000
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/pusher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@api3/airnode-adapter": "^0.12.0",
"@api3/airnode-node": "^0.12.0",
"@api3/airnode-validator": "^0.12.0",
"@api3/commons": "^0.3.0",
"@api3/commons": "file:../../../commons",
"@api3/ois": "^2.2.1",
"@api3/promise-utils": "^0.4.0",
"axios": "^1.6.0",
Expand Down
21 changes: 11 additions & 10 deletions packages/pusher/src/api-requests/data-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type BuildRequestOptions, buildAndExecuteRequest, extractAndEncodeResponse } from '@api3/airnode-adapter';
import type * as node from '@api3/airnode-node';
import { getReservedParameters } from '@api3/airnode-node/dist/src/adapters/http/parameters';
import { preProcessApiCallParameters, type ApiCallParameters, postProcessApiCallResponse } from '@api3/commons';
import { preProcessEndpointParameters, type EndpointParameters, postProcessResponse } from '@api3/commons';
import type { OIS, Endpoint as OisEndpoint } from '@api3/ois';
import { go, goSync } from '@api3/promise-utils';
import { isNil } from 'lodash';
Expand All @@ -15,14 +15,17 @@ import type { SignedApiUpdate } from '../validation/schema';
export const callApi = async (
ois: OIS,
endpoint: OisEndpoint,
apiCallParameters: ApiCallParameters,
endpointParameters: EndpointParameters,
apiCredentials: node.ApiCredentials[]
) => {
return go(async () => {
const logContext = { endpointName: endpoint.name, oisTitle: ois.title };
logger.debug('Preprocessing API call payload', logContext);
const processedApiCallParameters = await preProcessApiCallParameters(endpoint, apiCallParameters);
logger.debug('Performing API call', { ...logContext, processedApiCallParameters });
const { endpointParameters: processedEndpointParameters } = await preProcessEndpointParameters(
endpoint,
endpointParameters
);
logger.debug('Performing API call', { ...logContext, processedEndpointParameters });

const response = await buildAndExecuteRequest(
{
Expand Down Expand Up @@ -53,14 +56,14 @@ export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Pr
const operationEndpoint = endpoints[operationTemplate.endpointId]!;
const ois = oises.find((o) => o.title === operationEndpoint.oisTitle)!;
const operationOisEndpoint = ois.endpoints.find((e) => e.name === operationEndpoint.endpointName)!;
const operationApiCallParameters = operationTemplate.parameters.reduce((acc, parameter) => {
const endpointParameters = operationTemplate.parameters.reduce((acc, parameter) => {
return {
...acc,
[parameter.name]: parameter.value,
};
}, {});

const goCallApi = await callApi(ois, operationOisEndpoint, operationApiCallParameters, apiCredentials);
const goCallApi = await callApi(ois, operationOisEndpoint, endpointParameters, apiCredentials);

if (!goCallApi.success) {
logger.warn(`Failed to make API call`, {
Expand All @@ -77,17 +80,15 @@ export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Pr
const endpoint = endpoints[template.endpointId]!;
const oisEndpoint = ois.endpoints.find((e) => e.name === endpoint.endpointName)!;

const apiCallParameters = template.parameters.reduce((acc, parameter) => {
const endpointParameters = template.parameters.reduce((acc, parameter) => {
return {
...acc,
[parameter.name]: parameter.value,
};
}, {});

logger.debug('Processing successful API call', { templateId, operationTemplateId });
const goPostProcess = await go(async () =>
postProcessApiCallResponse(apiCallResponse, oisEndpoint, apiCallParameters)
);
const goPostProcess = await go(async () => postProcessResponse(apiCallResponse, oisEndpoint, endpointParameters));
if (!goPostProcess.success) {
const message = `Failed to post process successful API call`;
logger.warn(message, { templateId, operationTemplateId, errorMessage: goPostProcess.error.message });
Expand Down
6 changes: 3 additions & 3 deletions packages/pusher/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type LogLevel,
logFormatOptions,
logLevelOptions,
preProcessApiCallParameters,
preProcessEndpointParameters,
} from '@api3/commons';
import { oisSchema, type OIS, type Endpoint as oisEndpoint } from '@api3/ois';
import { goSync } from '@api3/promise-utils';
Expand Down Expand Up @@ -174,14 +174,14 @@ const validateTriggerReferences: SuperRefinement<{

const ois = oises.find((o) => o.title === endpoint.oisTitle)!;
const oisEndpoint = ois.endpoints.find((e) => e.name === endpoint.endpointName)!;
const apiCallParameters = template.parameters.reduce((acc, parameter) => {
const endpointParameters = template.parameters.reduce((acc, parameter) => {
return {
...acc,
[parameter.name]: parameter.value,
};
}, {});

return preProcessApiCallParameters(oisEndpoint, apiCallParameters);
return preProcessEndpointParameters(oisEndpoint, endpointParameters);
});

const operationsPayloads = await Promise.all(operationPayloadPromises);
Expand Down
54 changes: 52 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 662f48d

Please sign in to comment.