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

fix: override auth header while retrying #896

Merged
merged 2 commits into from
Sep 26, 2022
Merged

Conversation

cemreyavuz
Copy link
Contributor

@cemreyavuz cemreyavuz commented Sep 26, 2022

Latest change to this line caused a regression:

return this.postRequest(rawResponse, mutatedRequest);

Updated tokens are not used on retries for 401s now.

Because now we are passing mutatedRequest to postRequest, not all the default headers are applied properly. For us, the important one is the authorization header. Here we keep using the old authorization header on retries of because of this ordering:

protected populateDefaultHeaders(headers: HttpHeaders = {}) {
return {
...this.defaultHeaders,
...headers,
};
}

I'm not sure if we should change this to other way - then we wouldn't be able to override default headers. So I went with adding another parameter to postRequest for keeping both the original request and the mutated version. Open to suggestions to clean it up.

@cemreyavuz cemreyavuz requested a review from a team September 26, 2022 12:32
@cemreyavuz cemreyavuz marked this pull request as draft September 26, 2022 12:34
@cemreyavuz cemreyavuz marked this pull request as ready for review September 26, 2022 12:50
@@ -215,7 +216,7 @@ export class BasicHttpClient {
protected async request<ResponseType>(request: HttpRequest) {
const mutatedRequest = await this.preRequest(request);
const rawResponse = await this.rawRequest<ResponseType>(mutatedRequest);
return this.postRequest(rawResponse, mutatedRequest);
return this.postRequest(rawResponse, request, mutatedRequest);
Copy link
Contributor Author

@cemreyavuz cemreyavuz Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a way to differentiate between default headers and original headers in the request. Filtering out the default headers will not work because they can be overridden in the original request.

We need the mutated request to compare the authorization header used in the request, so maybe we can pass that instead of the whole request.

@codecov
Copy link

codecov bot commented Sep 26, 2022

Codecov Report

Merging #896 (dd8d8fc) into master (de3557f) will decrease coverage by 4.54%.
The diff coverage is 69.26%.

@@            Coverage Diff             @@
##           master     #896      +/-   ##
==========================================
- Coverage   84.06%   79.52%   -4.55%     
==========================================
  Files          70      105      +35     
  Lines        1952     3389    +1437     
  Branches      249      437     +188     
==========================================
+ Hits         1641     2695    +1054     
- Misses        301      692     +391     
+ Partials       10        2       -8     
Impacted Files Coverage Δ
packages/beta/src/cogniteClient.ts 100.00% <ø> (ø)
packages/core/src/httpClient/httpError.ts 90.00% <ø> (ø)
packages/core/src/testUtils.ts 35.59% <0.00%> (-19.58%) ⬇️
packages/stable/src/__tests__/testUtils.ts 96.42% <ø> (-3.58%) ⬇️
packages/stable/src/api/3d/assetMappings3DApi.ts 79.16% <ø> (-20.84%) ⬇️
packages/stable/src/api/3d/nodes3DApi.ts 81.81% <ø> (-18.19%) ⬇️
packages/stable/src/api/3d/revealNodes3DApi.ts 100.00% <ø> (ø)
packages/stable/src/api/3d/revealSectors3DApi.ts 100.00% <ø> (ø)
packages/stable/src/api/3d/revisions3DApi.ts 95.65% <ø> (-4.35%) ⬇️
...kages/stable/src/api/annotations/annotationsApi.ts 100.00% <ø> (ø)
... and 124 more

Copy link
Contributor

@BugGambit BugGambit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. As far as I can see, the SDK interface for the user would not change due to this PR.

@cemreyavuz cemreyavuz merged commit 25592fe into master Sep 26, 2022
@cemreyavuz cemreyavuz deleted the fix-401-second-try branch September 26, 2022 18:58
@BugGambit
Copy link
Contributor

@cemreyavuz Did the PR fix the problem?

@cemreyavuz
Copy link
Contributor Author

@BugGambit yes the problem is fixed with this version - also I have used it for the whole day and i didn't see any problem: 401s, retrys, etc. Everything looked fine 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants