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

Add an option to add a correlation ID to a request #323

Closed
wants to merge 12 commits into from
Closed
14 changes: 7 additions & 7 deletions packages/sdk/src/endpoints/AccountEndpoint.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ConnectorHttpResponse, ConnectorSyncInfo, IdentityInfo } from "../types";
import { Endpoint } from "./Endpoint";
import { CorrelationID, Endpoint } from "./Endpoint";

export class AccountEndpoint extends Endpoint {
public async getIdentityInfo(): Promise<ConnectorHttpResponse<IdentityInfo>> {
return await this.get("/api/v2/Account/IdentityInfo");
public async getIdentityInfo(correlationId?: CorrelationID): Promise<ConnectorHttpResponse<IdentityInfo>> {
return await this.get("/api/v2/Account/IdentityInfo", undefined, correlationId);
}

public async sync(): Promise<ConnectorHttpResponse<void>> {
return await this.post("/api/v2/Account/Sync", undefined, 204);
public async sync(correlationId?: CorrelationID): Promise<ConnectorHttpResponse<void>> {
return await this.post("/api/v2/Account/Sync", undefined, 204, undefined, correlationId);
}

public async getSyncInfo(): Promise<ConnectorHttpResponse<ConnectorSyncInfo>> {
return await this.get("/api/v2/Account/SyncInfo");
public async getSyncInfo(correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorSyncInfo>> {
return await this.get("/api/v2/Account/SyncInfo", undefined, correlationId);
}
}
110 changes: 69 additions & 41 deletions packages/sdk/src/endpoints/AttributesEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,91 +22,119 @@ import {
SucceedAttributeRequest,
SucceedAttributeResponse
} from "../types";
import { Endpoint } from "./Endpoint";
import { CorrelationID, Endpoint } from "./Endpoint";

export class AttributesEndpoint extends Endpoint {
public async createRepositoryAttribute(request: CreateRepositoryAttributeRequest): Promise<ConnectorHttpResponse<ConnectorAttribute>> {
return await this.post("/api/v2/Attributes", request);
public async createRepositoryAttribute(request: CreateRepositoryAttributeRequest, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttribute>> {
return await this.post("/api/v2/Attributes", request, undefined, undefined, correlationId);
}

public async succeedAttribute(predecessorId: string, request: SucceedAttributeRequest): Promise<ConnectorHttpResponse<SucceedAttributeResponse>> {
return await this.post(`/api/v2/Attributes/${predecessorId}/Succeed`, request);
public async succeedAttribute(
predecessorId: string,
request: SucceedAttributeRequest,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<SucceedAttributeResponse>> {
return await this.post(`/api/v2/Attributes/${predecessorId}/Succeed`, request, undefined, undefined, correlationId);
}

public async notifyPeerAboutRepositoryAttributeSuccession(
attributeId: string,
request: NotifyPeerAboutRepositoryAttributeSuccessionRequest
request: NotifyPeerAboutRepositoryAttributeSuccessionRequest,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<NotifyPeerAboutRepositoryAttributeSuccessionResponse>> {
return await this.post(`/api/v2/Attributes/${attributeId}/NotifyPeer`, request);
return await this.post(`/api/v2/Attributes/${attributeId}/NotifyPeer`, request, undefined, undefined, correlationId);
}

public async getAttributes(request: GetAttributesRequest): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes", request);
public async getAttributes(request: GetAttributesRequest, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes", request, correlationId);
}

public async getAttribute(attributeId: string): Promise<ConnectorHttpResponse<ConnectorAttribute>> {
return await this.get(`/api/v2/Attributes/${attributeId}`);
public async getAttribute(attributeId: string, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttribute>> {
return await this.get(`/api/v2/Attributes/${attributeId}`, undefined, correlationId);
}

public async getValidAttributes(request: GetValidAttributesRequest): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes/Valid", request);
public async getValidAttributes(request: GetValidAttributesRequest, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes/Valid", request, correlationId);
}

public async getAttributeTagCollection(): Promise<ConnectorHttpResponse<ConnectorAttributeTagCollection>> {
return await this.get("/api/v2/Attributes/TagCollection");
public async getAttributeTagCollection(correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttributeTagCollection>> {
return await this.get("/api/v2/Attributes/TagCollection", undefined, correlationId);
}

public async getOwnRepositoryAttributes(request?: GetOwnRepositoryAttributesRequest): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes/Own/Repository", request);
public async getOwnRepositoryAttributes(request?: GetOwnRepositoryAttributesRequest, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes/Own/Repository", request, correlationId);
}

public async getOwnSharedIdentityAttributes(request?: GetOwnSharedIdentityAttributesRequest): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes/Own/Shared/Identity", request);
public async getOwnSharedIdentityAttributes(
request?: GetOwnSharedIdentityAttributesRequest,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes/Own/Shared/Identity", request, correlationId);
}

public async getPeerSharedIdentityAttributes(request?: GetPeerSharedIdentityAttributesRequest): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes/Peer/Shared/Identity", request);
public async getPeerSharedIdentityAttributes(
request?: GetPeerSharedIdentityAttributesRequest,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get("/api/v2/Attributes/Peer/Shared/Identity", request, correlationId);
}

public async getVersionsOfAttribute(attributeId: string): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get(`/api/v2/Attributes/${attributeId}/Versions`);
public async getVersionsOfAttribute(attributeId: string, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get(`/api/v2/Attributes/${attributeId}/Versions`, correlationId);
}

public async getSharedVersionsOfAttribute(attributeId: string, request: GetSharedVersionsOfAttributeRequest): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get(`/api/v2/Attributes/${attributeId}/Versions/Shared`, request);
public async getSharedVersionsOfAttribute(
attributeId: string,
request: GetSharedVersionsOfAttributeRequest,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.get(`/api/v2/Attributes/${attributeId}/Versions/Shared`, request, correlationId);
}

public async deleteOwnSharedAttributeAndNotifyPeer(attributeId: string): Promise<ConnectorHttpResponse<DeleteOwnSharedAttributeAndNotifyPeerResponse>> {
return await this.delete(`/api/v2/Attributes/Own/Shared/${attributeId}`);
public async deleteOwnSharedAttributeAndNotifyPeer(
attributeId: string,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<DeleteOwnSharedAttributeAndNotifyPeerResponse>> {
return await this.delete(`/api/v2/Attributes/Own/Shared/${attributeId}`, correlationId);
}

public async deletePeerSharedAttributeAndNotifyOwner(attributeId: string): Promise<ConnectorHttpResponse<DeletePeerSharedAttributeAndNotifyOwnerResponse>> {
return await this.delete(`/api/v2/Attributes/Peer/Shared/${attributeId}`);
public async deletePeerSharedAttributeAndNotifyOwner(
attributeId: string,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<DeletePeerSharedAttributeAndNotifyOwnerResponse>> {
return await this.delete(`/api/v2/Attributes/Peer/Shared/${attributeId}`, correlationId);
}

public async deleteRepositoryAttribute(attributeId: string): Promise<ConnectorHttpResponse<void>> {
return await this.delete(`/api/v2/Attributes/${attributeId}`, undefined, 204);
public async deleteRepositoryAttribute(attributeId: string, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<void>> {
return await this.delete(`/api/v2/Attributes/${attributeId}`, undefined, 204, correlationId);
}

public async deleteThirdPartyRelationshipAttributeAndNotifyPeer(
attributeId: string
attributeId: string,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<DeleteThirdPartyRelationshipAttributeAndNotifyPeerResponse>> {
return await this.delete(`/api/v2/Attributes/ThirdParty/${attributeId}`);
return await this.delete(`/api/v2/Attributes/ThirdParty/${attributeId}`, correlationId);
}

public async executeIdentityAttributeQuery(request: ExecuteIdentityAttributeQueryRequest): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.post("/api/v2/Attributes/ExecuteIdentityAttributeQuery", request, 200);
public async executeIdentityAttributeQuery(request: ExecuteIdentityAttributeQueryRequest, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttributes>> {
return await this.post("/api/v2/Attributes/ExecuteIdentityAttributeQuery", request, 200, undefined, correlationId);
}

public async executeRelationshipAttributeQuery(request: ExecuteRelationshipAttributeQueryRequest): Promise<ConnectorHttpResponse<ConnectorAttribute>> {
return await this.post("/api/v2/Attributes/ExecuteRelationshipAttributeQuery", request, 200);
public async executeRelationshipAttributeQuery(
request: ExecuteRelationshipAttributeQueryRequest,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<ConnectorAttribute>> {
return await this.post("/api/v2/Attributes/ExecuteRelationshipAttributeQuery", request, 200, undefined, correlationId);
}

public async executeThirdPartyRelationshipAttributeQuery(request: ExecuteThirdPartyRelationshipAttributeQueryRequest): Promise<ConnectorHttpResponse<ConnectorAttribute[]>> {
return await this.post("/api/v2/Attributes/ExecuteThirdPartyRelationshipAttributeQuery", request, 200);
public async executeThirdPartyRelationshipAttributeQuery(
request: ExecuteThirdPartyRelationshipAttributeQueryRequest,
correlationId?: CorrelationID
): Promise<ConnectorHttpResponse<ConnectorAttribute[]>> {
return await this.post("/api/v2/Attributes/ExecuteThirdPartyRelationshipAttributeQuery", request, 200, undefined, correlationId);
}

public async executeIQLQuery(request: ExecuteIQLQueryRequest): Promise<ConnectorHttpResponse<ConnectorAttribute[]>> {
return await this.post("/api/v2/Attributes/ExecuteIQLQuery", request, 200);
public async executeIQLQuery(request: ExecuteIQLQueryRequest, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorAttribute[]>> {
return await this.post("/api/v2/Attributes/ExecuteIQLQuery", request, 200, undefined, correlationId);
}
}
10 changes: 5 additions & 5 deletions packages/sdk/src/endpoints/ChallengesEndpoint.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ConnectorChallenge, ConnectorChallengeValidationResult, ConnectorHttpResponse, CreateChallengeRequest, ValidateChallengeRequest } from "../types";
import { Endpoint } from "./Endpoint";
import { CorrelationID, Endpoint } from "./Endpoint";

export class ChallengesEndpoint extends Endpoint {
public async createChallenge(request: CreateChallengeRequest): Promise<ConnectorHttpResponse<ConnectorChallenge>> {
return await this.post("/api/v2/Challenges", request);
public async createChallenge(request: CreateChallengeRequest, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorChallenge>> {
return await this.post("/api/v2/Challenges", request, undefined, undefined, correlationId);
}

public async validateChallenge(request: ValidateChallengeRequest): Promise<ConnectorHttpResponse<ConnectorChallengeValidationResult>> {
return await this.post("/api/v2/Challenges/Validate", request, 200);
public async validateChallenge(request: ValidateChallengeRequest, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ConnectorChallengeValidationResult>> {
return await this.post("/api/v2/Challenges/Validate", request, 200, undefined, correlationId);
}
}
60 changes: 45 additions & 15 deletions packages/sdk/src/endpoints/Endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import formDataLib from "form-data";
import { ConnectorHttpResponse } from "../types/ConnectorHttpResponse";

export type CorrelationID = string;

export abstract class Endpoint {
public constructor(private readonly httpClient: AxiosInstance) {}

Expand All @@ -10,26 +12,43 @@ export abstract class Endpoint {
return reponse.data;
}

protected async get<T>(path: string, query?: unknown): Promise<ConnectorHttpResponse<T>> {
protected async get<T>(path: string, query?: unknown, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<T>> {
const response = await this.httpClient.get(path, {
params: query
params: query,
headers: {
"x-correlation-id": correlationId
}
});

return this.makeResult(response);
}

protected async post<T>(path: string, data?: unknown, expectedStatus?: number, params?: unknown): Promise<ConnectorHttpResponse<T>> {
const response = await this.httpClient.post(path, data, { params });
protected async post<T>(path: string, data?: unknown, expectedStatus?: number, params?: unknown, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<T>> {
const response = await this.httpClient.post(path, data, {
params,
headers: {
"x-correlation-id": correlationId
}
});
return this.makeResult(response, expectedStatus);
}

protected async put<T>(path: string, data?: unknown): Promise<ConnectorHttpResponse<T>> {
const response = await this.httpClient.put(path, data);
protected async put<T>(path: string, data?: unknown, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<T>> {
const response = await this.httpClient.put(path, data, {
headers: {
"x-correlation-id": correlationId
}
});
return this.makeResult(response);
}

protected async delete<T>(path: string, params?: unknown, expectedStatus?: number): Promise<ConnectorHttpResponse<T>> {
const response = await this.httpClient.delete(path, { params });
protected async delete<T>(path: string, params?: unknown, expectedStatus?: number, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<T>> {
const response = await this.httpClient.delete(path, {
params,
headers: {
"x-correlation-id": correlationId
}
});
return this.makeResult(response, expectedStatus);
}

Expand Down Expand Up @@ -68,9 +87,13 @@ export abstract class Endpoint {
return ConnectorHttpResponse.success(httpResponse.data.result);
}

protected async download(url: string): Promise<ConnectorHttpResponse<ArrayBuffer>> {
protected async download(url: string, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ArrayBuffer>> {
const httpResponse = await this.httpClient.get(url, {
responseType: "arraybuffer"
responseType: "arraybuffer",

headers: {
"x-correlation-id": correlationId
}
});

if (httpResponse.status !== 200) {
Expand All @@ -95,7 +118,7 @@ export abstract class Endpoint {
return ConnectorHttpResponse.success(httpResponse.data as ArrayBuffer);
}

protected async downloadQrCode(method: "GET" | "POST", url: string, request?: unknown): Promise<ConnectorHttpResponse<ArrayBuffer>> {
protected async downloadQrCode(method: "GET" | "POST", url: string, request?: unknown, correlationId?: CorrelationID): Promise<ConnectorHttpResponse<ArrayBuffer>> {
const config: AxiosRequestConfig = {
responseType: "arraybuffer",
headers: {
Expand All @@ -107,10 +130,17 @@ export abstract class Endpoint {

switch (method) {
case "GET":
httpResponse = await this.httpClient.get(url, { ...config, params: request });
httpResponse = await this.httpClient.get(url, {
responseType: config.responseType,
params: request,
headers: { "x-correlation-id": correlationId, ...config.headers }
});
break;
case "POST":
httpResponse = await this.httpClient.post(url, request, config);
httpResponse = await this.httpClient.post(url, request, {
responseType: config.responseType,
headers: { "x-correlation-id": correlationId, ...config.headers }
});
break;
}

Expand All @@ -136,7 +166,7 @@ export abstract class Endpoint {
return ConnectorHttpResponse.success(httpResponse.data as ArrayBuffer);
}

protected async postMultipart(url: string, data: Record<string, unknown>, filename: string): Promise<AxiosResponse<unknown>> {
protected async postMultipart(url: string, data: Record<string, unknown>, filename: string, correlationId?: CorrelationID): Promise<AxiosResponse<unknown>> {
const formData = new formDataLib();
for (const key in data) {
if (!data.hasOwnProperty(key)) {
Expand All @@ -155,7 +185,7 @@ export abstract class Endpoint {
}

const response = await this.httpClient.post(url, formData, {
headers: formData.getHeaders()
headers: { ...formData.getHeaders(), "x-correlation-id": correlationId }
});

return response;
Expand Down
Loading
Loading