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: using headers for body #202

Merged
merged 1 commit into from
Sep 13, 2023
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
10 changes: 6 additions & 4 deletions ui/summit-2023/src/common/api/loginService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ const buildCanonicalLoginJson = ({ stakeAddress, slotNumber }: LoginInput): Retu

const submitLogin = async (jsonRequest: SignedWeb3Request) => {
return await doRequest<{ accessToken: string; expiresAt: string }>(
HttpMethods.POST,
LOGIN_URL,
DEFAULT_CONTENT_TYPE_HEADERS,
JSON.stringify(jsonRequest)
HttpMethods.GET,
LOGIN_URL,
DEFAULT_CONTENT_TYPE_HEADERS,
JSON.stringify(jsonRequest),
undefined,
true
);
};

Expand Down
44 changes: 23 additions & 21 deletions ui/summit-2023/src/common/api/voteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type voteInput = {
};

export const buildCanonicalVoteInputJson = ({
voteId,
categoryId,
proposalId,
stakeAddress,
slotNumber,
}: voteInput): ReturnType<typeof canonicalize> => {
voteId,
categoryId,
proposalId,
stakeAddress,
slotNumber,
}: voteInput): ReturnType<typeof canonicalize> => {
const startOfCurrentDay = new Date();
startOfCurrentDay.setUTCMinutes(0, 0, 0);
return canonicalize({
Expand All @@ -43,26 +43,28 @@ export const buildCanonicalVoteInputJson = ({
};

const castAVoteWithDigitalSignature = async (jsonRequest: SignedWeb3Request) =>
await doRequest<Problem | Vote>(
HttpMethods.POST,
CAST_VOTE_URL,
DEFAULT_CONTENT_TYPE_HEADERS,
JSON.stringify(jsonRequest)
);
await doRequest<Problem | Vote>(
HttpMethods.POST,
CAST_VOTE_URL,
DEFAULT_CONTENT_TYPE_HEADERS,
JSON.stringify(jsonRequest),
undefined,
true
);

const getSlotNumber = async () => {
return await doRequest<ChainTip>(HttpMethods.GET, BLOCKCHAIN_TIP_URL, DEFAULT_CONTENT_TYPE_HEADERS);
};

const getVoteReceipt = async (categoryId: string, token: string) =>
await doRequest<VoteReceipt>(
HttpMethods.GET,
`${VOTE_RECEIPT_URL}/${env.EVENT_ID}/${categoryId}`,
{
...DEFAULT_CONTENT_TYPE_HEADERS,
},
null,
token
);
await doRequest<VoteReceipt>(
HttpMethods.GET,
`${VOTE_RECEIPT_URL}/${env.EVENT_ID}/${categoryId}`,
{
...DEFAULT_CONTENT_TYPE_HEADERS,
},
null,
token
);

export { castAVoteWithDigitalSignature, getSlotNumber, getVoteReceipt };
43 changes: 25 additions & 18 deletions ui/summit-2023/src/common/handlers/httpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ export function responseErrorsHandler() {
return {
parse(errors: Errors['errors']) {
return (errors || [])
.map((error) => {
if (error.errorCode) {
return error.errorCode;
}
})
.toString();
.map((error) => {
if (error.errorCode) {
return error.errorCode;
}
})
.toString();
},
};
}
Expand All @@ -120,9 +120,9 @@ export function responseHandlerDelegate<T>() {
parsedResponse = await response[isJson ? 'json' : 'text']();
if (response.status !== 200) {
throw new HttpError(
response.status,
response.url,
response.status === 500 ? 'Oops, something went wrong...' : getErrorMessage(parsedResponse)
response.status,
response.url,
response.status === 500 ? 'Oops, something went wrong...' : getErrorMessage(parsedResponse)
);
}
} catch (error: any) {
Expand All @@ -145,10 +145,10 @@ type RequestInit = {
};

async function executeRequest<T>(
requestUri: string,
method: HttpMethods,
headers: Partial<contentTypeHeaders>,
body?: string
requestUri: string,
method: HttpMethods,
headers: Partial<contentTypeHeaders>,
body?: string
) {
const request: RequestInit = {
method: method || HttpMethods.GET,
Expand Down Expand Up @@ -191,14 +191,21 @@ export function patch<T>(url: string, headers: Partial<contentTypeHeaders>, body
}

export const doRequest = async <T>(
method: HttpMethods,
url: string,
headers: Partial<contentTypeHeaders>,
body?: string,
token?: string
method: HttpMethods,
url: string,
headers: Partial<contentTypeHeaders>,
body?: string,
token?: string,
bodyInHeader?: boolean,
) => {
const allHeaders = { ...headers, ...DEFAULT_CONTENT_TYPE_HEADERS };

if (body && bodyInHeader){
allHeaders['X-CIP93-Signature'] = JSON.parse(body).coseSignature;
allHeaders['X-CIP93-Public-Key'] = JSON.parse(body).cosePublicKey;
body = undefined;
}

if (token) {
allHeaders['Authorization'] = <MediaTypes>`Bearer ${token}`;
}
Expand Down
Loading