-
Notifications
You must be signed in to change notification settings - Fork 25
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
[#174877080] Upgrade to italia-utils 5.x #711
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0027732
upgraded io-utils and solve breaking changes
balanza 7f8a935
generate client for app api
balanza b801cf8
fix test
balanza d97ea72
fixes
balanza bd570ea
implement bonus client
balanza 614692c
implement pagopa proxy client
balanza 17de3fe
fix lint
balanza 5e081c3
Merge branch 'master' into 174877080-upgrade-io-utils
balanza 704b5e3
Merge branch 'master' into 174877080-upgrade-io-utils
balanza 260e6b4
Merge branch 'master' into 174877080-upgrade-io-utils
balanza 1465d94
Merge branch '174877080-upgrade-io-utils' of https://github.com/pagop…
balanza d06ce61
Merge branch 'master' into 174877080-upgrade-io-utils
balanza b2e7987
comments
balanza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,239 +1,22 @@ | ||
import { | ||
ApiHeaderJson, | ||
composeHeaderProducers, | ||
composeResponseDecoders, | ||
constantResponseDecoder, | ||
createFetchRequestForApi, | ||
ioResponseDecoder, | ||
ReplaceRequestParams, | ||
RequestHeaderProducer, | ||
RequestParams, | ||
TypeofApiCall | ||
} from "italia-ts-commons/lib/requests"; | ||
import { ProblemJson } from "italia-ts-commons/lib/responses"; | ||
import { Omit } from "italia-ts-commons/lib/types"; | ||
import nodeFetch from "node-fetch"; | ||
|
||
import { | ||
createProfileDefaultDecoder, | ||
CreateProfileT, | ||
getMessageDefaultDecoder, | ||
getMessagesByUserDefaultDecoder, | ||
GetMessagesByUserT, | ||
GetMessageT, | ||
getProfileDefaultDecoder, | ||
GetProfileT, | ||
getServiceDefaultDecoder, | ||
GetServiceT, | ||
getUserDataProcessingDefaultDecoder, | ||
GetUserDataProcessingT, | ||
getVisibleServicesDefaultDecoder, | ||
GetVisibleServicesT, | ||
StartEmailValidationProcessT, | ||
updateProfileDefaultDecoder, | ||
UpdateProfileT, | ||
upsertUserDataProcessingDefaultDecoder, | ||
UpsertUserDataProcessingT | ||
} from "../../generated/io-api/requestTypes"; | ||
|
||
// we want to authenticate against the platform APIs with the APIM header key or | ||
// the Azure Functions header key, so we send both headers | ||
function SubscriptionKeyHeaderProducer<P>( | ||
token: string | ||
): RequestHeaderProducer<P, "X-Functions-Key" | "Ocp-Apim-Subscription-Key"> { | ||
return () => ({ | ||
"Ocp-Apim-Subscription-Key": token, | ||
"X-Functions-Key": token | ||
}); | ||
} | ||
import { Client, createClient } from "../../generated/io-api/client"; | ||
|
||
export function APIClient( | ||
baseUrl: string, | ||
token: string, | ||
// tslint:disable-next-line:no-any | ||
fetchApi: typeof fetch = (nodeFetch as any) as typeof fetch // TODO: customize fetch with timeout | ||
): { | ||
readonly updateProfile: TypeofApiCall<typeof updateProfileT>; | ||
readonly getMessage: TypeofApiCall<typeof getMessageT>; | ||
readonly getMessages: TypeofApiCall<typeof getMessagesT>; | ||
readonly getProfile: TypeofApiCall<typeof getProfileT>; | ||
readonly createProfile: TypeofApiCall<typeof createProfileT>; | ||
readonly upsertUserDataProcessing: TypeofApiCall< | ||
typeof upsertUserDataProcessingT | ||
>; | ||
readonly emailValidationProcess: TypeofApiCall< | ||
typeof emailValidationProcessT | ||
>; | ||
readonly getService: TypeofApiCall<typeof getServiceT>; | ||
readonly getVisibleServices: TypeofApiCall<typeof getVisibleServicesT>; | ||
readonly getUserDataProcessing: TypeofApiCall<typeof getUserDataProcessingT>; | ||
} { | ||
const options = { | ||
): Client<"SubscriptionKey"> { | ||
return createClient<"SubscriptionKey">({ | ||
basePath: "", | ||
baseUrl, | ||
fetchApi | ||
}; | ||
|
||
const tokenHeaderProducer = SubscriptionKeyHeaderProducer(token); | ||
|
||
// Custom decoder until we fix the problem in the io-utils generator | ||
// https://www.pivotaltracker.com/story/show/169915207 | ||
// tslint:disable-next-line:typedef | ||
function startEmailValidationProcessCustomDecoder() { | ||
return composeResponseDecoders( | ||
composeResponseDecoders( | ||
composeResponseDecoders( | ||
composeResponseDecoders( | ||
constantResponseDecoder<undefined, 202>(202, undefined), | ||
ioResponseDecoder< | ||
400, | ||
typeof ProblemJson["_A"], | ||
typeof ProblemJson["_O"] | ||
>(400, ProblemJson) | ||
), | ||
constantResponseDecoder<undefined, 401>(401, undefined) | ||
), | ||
constantResponseDecoder<undefined, 404>(404, undefined) | ||
), | ||
constantResponseDecoder<undefined, 429>(429, undefined) | ||
); | ||
} | ||
|
||
const getProfileT: ReplaceRequestParams< | ||
GetProfileT, | ||
Omit<RequestParams<GetProfileT>, "SubscriptionKey"> | ||
> = { | ||
headers: tokenHeaderProducer, | ||
method: "get", | ||
query: _ => ({}), | ||
response_decoder: getProfileDefaultDecoder(), | ||
url: params => `/profiles/${params.fiscalCode}` | ||
}; | ||
|
||
const createProfileT: ReplaceRequestParams< | ||
CreateProfileT, | ||
Omit<RequestParams<CreateProfileT>, "SubscriptionKey"> | ||
> = { | ||
body: params => JSON.stringify(params.newProfile), | ||
headers: composeHeaderProducers(tokenHeaderProducer, ApiHeaderJson), | ||
method: "post", | ||
query: _ => ({}), | ||
response_decoder: createProfileDefaultDecoder(), | ||
url: params => `/profiles/${params.fiscalCode}` | ||
}; | ||
|
||
const updateProfileT: ReplaceRequestParams< | ||
UpdateProfileT, | ||
Omit<RequestParams<UpdateProfileT>, "SubscriptionKey"> | ||
> = { | ||
body: params => JSON.stringify(params.profile), | ||
headers: composeHeaderProducers(tokenHeaderProducer, ApiHeaderJson), | ||
method: "put", | ||
query: _ => ({}), | ||
response_decoder: updateProfileDefaultDecoder(), | ||
url: params => `/profiles/${params.fiscalCode}` | ||
}; | ||
|
||
const emailValidationProcessT: ReplaceRequestParams< | ||
StartEmailValidationProcessT, | ||
Omit<RequestParams<StartEmailValidationProcessT>, "SubscriptionKey"> | ||
> = { | ||
body: _ => "{}", | ||
headers: composeHeaderProducers(tokenHeaderProducer, ApiHeaderJson), | ||
method: "post", | ||
query: _ => ({}), | ||
response_decoder: startEmailValidationProcessCustomDecoder(), | ||
url: params => `/email-validation-process/${params.fiscalCode}` | ||
}; | ||
|
||
const getUserDataProcessingT: ReplaceRequestParams< | ||
GetUserDataProcessingT, | ||
Omit<RequestParams<GetUserDataProcessingT>, "SubscriptionKey"> | ||
> = { | ||
headers: tokenHeaderProducer, | ||
method: "get", | ||
query: _ => ({}), | ||
response_decoder: getUserDataProcessingDefaultDecoder(), | ||
url: params => | ||
`/user-data-processing/${params.fiscalCode}/${params.userDataProcessingChoiceParam}` | ||
}; | ||
|
||
const getMessagesT: ReplaceRequestParams< | ||
GetMessagesByUserT, | ||
Omit<RequestParams<GetMessagesByUserT>, "SubscriptionKey"> | ||
> = { | ||
headers: tokenHeaderProducer, | ||
method: "get", | ||
query: _ => ({}), | ||
response_decoder: getMessagesByUserDefaultDecoder(), | ||
url: params => `/messages/${params.fiscalCode}` | ||
}; | ||
|
||
const getMessageT: ReplaceRequestParams< | ||
GetMessageT, | ||
Omit<RequestParams<GetMessageT>, "SubscriptionKey"> | ||
> = { | ||
headers: tokenHeaderProducer, | ||
method: "get", | ||
query: _ => ({}), | ||
response_decoder: getMessageDefaultDecoder(), | ||
url: params => `/messages/${params.fiscalCode}/${params.id}` | ||
}; | ||
|
||
const getVisibleServicesT: ReplaceRequestParams< | ||
GetVisibleServicesT, | ||
Omit<RequestParams<GetVisibleServicesT>, "SubscriptionKey"> | ||
> = { | ||
headers: tokenHeaderProducer, | ||
method: "get", | ||
query: _ => ({}), | ||
response_decoder: getVisibleServicesDefaultDecoder(), | ||
url: () => `/services` | ||
}; | ||
|
||
const getServiceT: ReplaceRequestParams< | ||
GetServiceT, | ||
Omit<RequestParams<GetServiceT>, "SubscriptionKey"> | ||
> = { | ||
headers: tokenHeaderProducer, | ||
method: "get", | ||
query: _ => ({}), | ||
response_decoder: getServiceDefaultDecoder(), | ||
url: params => `/services/${params.service_id}` | ||
}; | ||
|
||
const upsertUserDataProcessingT: ReplaceRequestParams< | ||
UpsertUserDataProcessingT, | ||
Omit<RequestParams<UpsertUserDataProcessingT>, "SubscriptionKey"> | ||
> = { | ||
body: params => JSON.stringify(params.userDataProcessingChoiceRequest), | ||
headers: composeHeaderProducers(tokenHeaderProducer, ApiHeaderJson), | ||
method: "post", | ||
query: _ => ({}), | ||
response_decoder: upsertUserDataProcessingDefaultDecoder(), | ||
url: params => `/user-data-processing/${params.fiscalCode}` | ||
}; | ||
|
||
return { | ||
createProfile: createFetchRequestForApi(createProfileT, options), | ||
emailValidationProcess: createFetchRequestForApi( | ||
emailValidationProcessT, | ||
options | ||
), | ||
getMessage: createFetchRequestForApi(getMessageT, options), | ||
getMessages: createFetchRequestForApi(getMessagesT, options), | ||
getProfile: createFetchRequestForApi(getProfileT, options), | ||
getService: createFetchRequestForApi(getServiceT, options), | ||
getUserDataProcessing: createFetchRequestForApi( | ||
getUserDataProcessingT, | ||
options | ||
), | ||
getVisibleServices: createFetchRequestForApi(getVisibleServicesT, options), | ||
updateProfile: createFetchRequestForApi(updateProfileT, options), | ||
upsertUserDataProcessing: createFetchRequestForApi( | ||
upsertUserDataProcessingT, | ||
options | ||
) | ||
}; | ||
fetchApi, | ||
withDefaults: op => params => | ||
op({ | ||
...params, | ||
SubscriptionKey: token | ||
}) | ||
}); | ||
} | ||
|
||
export type APIClient = typeof APIClient; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this must be x-functions-key (Ocp-Apim-Subscription-Key is unused)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually is. The referenced api spec is
Under the hood, the generated client does the following:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's right, not easy to grasp from the code here :-) (maybe we could add a comment?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done here