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

Refactor Drive service into a class #489

Merged
merged 9 commits into from
Dec 24, 2024
Merged

Conversation

Apophenia
Copy link
Contributor

@Apophenia Apophenia commented Dec 20, 2024

  • Refactor drive service into a class
  • Refactor SSM service into a class
  • Add static method for parsing Google-specific URLs
  • SSM service returns parameter only as string instead of parameter object from boto3 for better parity with environment variable usage
  • Add test environment variable to test helpers

@@ -265,7 +274,7 @@ def test_parseEditionToPublication_ReaderFlagTrue(self, testPubEls, mocker):
mocker.call('rights', {'source': 'testSource', 'license': 'testLicense', 'rightsStatement': 'testStatement'})
])

pubMocks['addLink'].assert_called_with({'href': 'https://digital-research-books-beta.nypl.org/read/testID', 'type': 'testType', 'rel': 'http://opds-spec.org/acquisition/open-access', 'identifier': 'readable'})
pubMocks['addLink'].assert_called_with({'href': 'https://drb-qa.nypl.org/read/testID', 'type': 'testType', 'rel': 'http://opds-spec.org/acquisition/open-access', 'identifier': 'readable'})
Copy link
Contributor Author

@Apophenia Apophenia Dec 20, 2024

Choose a reason for hiding this comment

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

The environment variable used by this function seemed to be different when I ran the tests individually vs as a set, and then seemed to also behave different on my machine vs QA. I wondered if it was being set by TestFlaskAPI (which manually sets it to production) in some cases, but it was cleared to an empty string (perhaps by clearEnvVars) in others. In any case, adding the helper setup and teardown seemed to normalize it to a "test" environment.

I'm making a note to investigate the state sharing across unit tests as part of a test audit.

@Apophenia Apophenia marked this pull request as ready for review December 20, 2024 23:47
if os.environ['ENVIRONMENT'] == 'production':
SERVICE_ACCOUNT_FILE = ssm_service.get_parameter('arn:aws:ssm:us-east-1:946183545209:parameter/drb/production/google-drive-service-key')
else:
SERVICE_ACCOUNT_FILE = ssm_service.get_parameter('arn:aws:ssm:us-east-1:946183545209:parameter/drb/qa/google-drive-service-key')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be better to encapsulate the ARNs in e.g. a dictionary within the SSM service itself?

Copy link
Contributor

Choose a reason for hiding this comment

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

could you dossm_service.get_parameter(f'arn.../{os.environ.get('ENVIRONMENT', 'qa')}... to make this easier?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would definitely look better, but in some cases that variable would be 'test' or 'local' and wouldn't result in a valid ARN path - as it stands now, it falls back to using QA params locally.

As I'm testing the Airtable changes I've been using the local keys as a fallback anyway so that I can test against remote test data if I want to, but if I committed something like that it would be even more complex.

return file

def get_file_metadata(self, file_id: str) -> Optional[str]:
# supportsAllDrives=True required as of 12/24 despite deprecation
Copy link
Contributor

Choose a reason for hiding this comment

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

is this still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to leave this here in case anyone tried to refactor this code and referred to the Google documentation, which is in some places incorrect. It looks like they added warnings to the codebase that the supportsAllDrives would be considered true by default after 2020, and planned to support Team drives by default, but this isn't the case at least for get_media().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I lost a lot of time to this before I just tried adding it anyway - I didn't want the same to happen to anyone else.

def get_file_metadata(self, file_id: str) -> Optional[str]:
# supportsAllDrives=True required as of 12/24 despite deprecation
request = self.drive_service.files().get(fileId=file_id, supportsAllDrives=True)
meta = request.execute()
Copy link
Contributor

Choose a reason for hiding this comment

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

what is meta? the variable name isn't quite clear to me - sorry!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Copy link
Contributor

@kylevillegas93 kylevillegas93 left a comment

Choose a reason for hiding this comment

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

Overall, looks good - just a few minor comments!

@Apophenia Apophenia merged commit 624c229 into main Dec 24, 2024
1 check passed
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