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: update createUpload to accept environment id in its params [INTEG-1492] #2030

Merged
merged 1 commit into from
Nov 13, 2023

Conversation

ryunsong-contentful
Copy link
Contributor

@ryunsong-contentful ryunsong-contentful commented Nov 1, 2023

Somewhat recently there was a CEP for updating the upload-api that was approved and a subsequent PR that was merged. This effectively means the Upload API now takes in both the Environment Id and Space Id for a given request.

@ryunsong-contentful ryunsong-contentful requested a review from a team as a code owner November 1, 2023 21:26
@ryunsong-contentful ryunsong-contentful changed the title fix: update createUpload to accept environment id in its params [INTEG-1492 fix: update createUpload to accept environment id in its params [INTEG-1492] Nov 1, 2023
Copy link
Contributor

@ruderngespra ruderngespra left a comment

Choose a reason for hiding this comment

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

No expert on the changes that have been introduced to the upload endpoint, so happy to discuss this further async as well :)

@@ -1152,11 +1152,13 @@ export default function createEnvironmentApi(makeRequest: MakeRequest) {
*/
createUpload: function createUpload(data: { file: string | ArrayBuffer | Stream }) {
const raw = this.toPlainObject() as EnvironmentProps
const envId = raw.sys.aliasedEnvironment?.sys.id || ''
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is not targeting the public environment id. As far as I understand the code this either:

  1. in the case that we are looking at an alias route, using the underlying environment id

  2. in the case that no aliases are used, just providing an empty string.

Not sure about all the details of how the upload endpoint has been adjusted, but I am assuming it will always use either the alias-id or the environment-id, but never the environment-id that is being targeted by the alias, not empty string.

@ruderngespra
Copy link
Contributor

I think this is the file that the upload endpoint is properly handled and that would need to be able to handle the environment as part of the route.

) => {
const httpUpload = getUploadHttpClient(http)

return raw.get(httpUpload, `/spaces/${params.spaceId}/uploads/${params.uploadId}`)
const spacePath = `/spaces/${params.spaceId}/uploads/${params.uploadId}`
Copy link
Member

Choose a reason for hiding this comment

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

This is the third repetition of the same code and I think it would not be premature to abstract it into a helper function (eg in this file)

@ryunsong-contentful ryunsong-contentful force-pushed the INTEG-1492 branch 2 times, most recently from 15a9896 to c39d8a2 Compare November 8, 2023 16:10
lib/common-types.ts Outdated Show resolved Hide resolved
@ryunsong-contentful ryunsong-contentful force-pushed the INTEG-1492 branch 2 times, most recently from ce7488e to 2b28653 Compare November 8, 2023 16:36
lib/adapters/REST/endpoints/upload.ts Show resolved Hide resolved
const path = params.environmentId ? environmentPath : spacePath

// Return path as is if uploadId is not provided, otherwise append it to the path
if (!params.uploadId) return path
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand this correctly, this now changes the behavior to also on post requests use the asset id as part of the route, whereas before we would never use the uploadId on post requests. Is that in accordance with the CEP changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm... I don't think so. The path for the post route would look like /spaces/${params.spaceId}/uploads OR like /spaces/${params.spaceId}/environments/${params.environmentId}/uploads. So no asset ids, or upload ids in the route being added here. This is basically just a refactoring of the path logic since the post doesn't need an upload id but get and deletes do.

The asset itself is passed as a function parameter like this

  const path = getUploadUrlPath(params)
  return raw.post(httpUpload, path, file, {
    headers: {
      'Content-Type': 'application/octet-stream',
    },
  })
}

Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏻 But then we shouldn't add the uploadId to the path here, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, const path = getUploadUrlPath(params) will ensure that the path will contain the upload id for get/delete but not for the create operation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. 👍🏻 A bit of a nit but I think I would prefer to have this reflected in the types of the create method then, basically the create method then should not have GetSpaceEnvironmentUploadParams as type, because it will never accept the optional uploadId (or if it did, it would result in a 400 api call), but only GetSpaceEnvironmentParams right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, I agree, the create method should take in only the GetSpaceEnvironmentParams

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I have a few cents to add here as well... I think the comments and the suggestions on the approach make a lot of sense.

My opinion is that we should not overload the method here to handle both uses cases but treat them as different things. This also makes the type definition itself a bit cleaner.

So to give you an example picked at random from the same folder we're in here (REST/endpoints):

const getBaseUrl = (params: GetOrganizationParams) =>
`/organizations/${params.organizationId}/organization_memberships`
const getEntityUrl = (params: GetOrganizationMembershipParams) =>
`${getBaseUrl(params)}/${params.organizationMembershipId}`

In general this is true of every "restful" resource, there will always be two endpoints the "single" (for read/update/delete) and the "collection" (for create/query). So I suggest we embrace it, create two methods which have different input types, and then update GetSpaceEnvironmentUploadParams type to require the uploadid (so it can be reused throughout).

I think with this small change you might even end up with less lines of code, certainly one less conditional, and hopefully a cleaner solution.

Pretty small thing at the end of the day but i think worth getting to a nice clean state.

lib/common-types.ts Outdated Show resolved Hide resolved
@ryunsong-contentful ryunsong-contentful force-pushed the INTEG-1492 branch 3 times, most recently from 9bcac61 to 018840f Compare November 9, 2023 22:03
@@ -99,7 +99,8 @@ describe('Asset api', function () {
await unarchivedAsset.delete()
})

test('Create and process asset with multiple locales', async () => {
// Skip because this is a flakey test
Copy link
Contributor Author

@ryunsong-contentful ryunsong-contentful Nov 10, 2023

Choose a reason for hiding this comment

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

These are something the DX team will fix once they take ownership of this repo - ticket has been created in their backlog

Copy link
Contributor

@ruderngespra ruderngespra left a comment

Choose a reason for hiding this comment

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

lgtm 🎉
I would double check this with the owners of the upload api as well though!

const path = params.environmentId ? environmentPath : spacePath

// Return path as is if uploadId is not provided, otherwise append it to the path
if (!params.uploadId) return path
Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. 👍🏻 A bit of a nit but I think I would prefer to have this reflected in the types of the create method then, basically the create method then should not have GetSpaceEnvironmentUploadParams as type, because it will never accept the optional uploadId (or if it did, it would result in a 400 api call), but only GetSpaceEnvironmentParams right?

export const create: RestEndpoint<'Upload', 'create'> = (
http: AxiosInstance,
params: GetSpaceParams,
params: GetSpaceEnvironmentUploadParams,
Copy link
Contributor

Choose a reason for hiding this comment

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

Basically this should then be GetSpaceEnvironmentParams right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right!

lib/adapters/REST/endpoints/upload.ts Show resolved Hide resolved
Copy link
Member

@jsdalton jsdalton left a comment

Choose a reason for hiding this comment

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

I had some more thoughts in this thread of discussion around the path method and types, LMK if makes sense!

const path = params.environmentId ? environmentPath : spacePath

// Return path as is if uploadId is not provided, otherwise append it to the path
if (!params.uploadId) return path
Copy link
Member

Choose a reason for hiding this comment

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

Yeah I have a few cents to add here as well... I think the comments and the suggestions on the approach make a lot of sense.

My opinion is that we should not overload the method here to handle both uses cases but treat them as different things. This also makes the type definition itself a bit cleaner.

So to give you an example picked at random from the same folder we're in here (REST/endpoints):

const getBaseUrl = (params: GetOrganizationParams) =>
`/organizations/${params.organizationId}/organization_memberships`
const getEntityUrl = (params: GetOrganizationMembershipParams) =>
`${getBaseUrl(params)}/${params.organizationMembershipId}`

In general this is true of every "restful" resource, there will always be two endpoints the "single" (for read/update/delete) and the "collection" (for create/query). So I suggest we embrace it, create two methods which have different input types, and then update GetSpaceEnvironmentUploadParams type to require the uploadid (so it can be reused throughout).

I think with this small change you might even end up with less lines of code, certainly one less conditional, and hopefully a cleaner solution.

Pretty small thing at the end of the day but i think worth getting to a nice clean state.

lib/common-types.ts Outdated Show resolved Hide resolved
@ryunsong-contentful ryunsong-contentful force-pushed the INTEG-1492 branch 2 times, most recently from 82a542f to 155bac7 Compare November 10, 2023 17:29
Copy link
Member

@jsdalton jsdalton left a comment

Choose a reason for hiding this comment

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

Thanks for the changes, look great! 😎

@ryunsong-contentful ryunsong-contentful merged commit 4287e47 into master Nov 13, 2023
2 checks passed
@ryunsong-contentful ryunsong-contentful deleted the INTEG-1492 branch November 13, 2023 15:23
@contentful-automation
Copy link
Contributor

🎉 This PR is included in version 11.5.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@contentful-automation
Copy link
Contributor

🎉 This issue has been resolved in version 11.5.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

t-col added a commit that referenced this pull request Dec 5, 2023
t-col added a commit that referenced this pull request Dec 5, 2023
t-col added a commit that referenced this pull request Dec 5, 2023
t-col added a commit that referenced this pull request Dec 5, 2023
…ms [INTEG-1492]" (#2084)

* fix: revert "update createUpload to accept environment id in its params (#2030)"

This reverts commit 4287e47.

* fix: failing webhook integration test
@jsdalton jsdalton mentioned this pull request Dec 8, 2023
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants