Repository navigation
Introduce ApiAccess for representing access scopes - #482
Conversation
| hooks: | ||
| - id: end-of-file-fixer | ||
| - id: trailing-whitespace | ||
| - repo: https://github.com/pre-commit/mirrors-autopep8 |
| def __eq__(self, other): | ||
| return type(self) == type(other) and self._compressed_scopes == other._compressed_scopes | ||
|
|
||
| def __store_scopes(self, scopes): |
There was a problem hiding this comment.
Just out of curiosity what do double leading underscores do? 😅 Is this for name mangling?
There was a problem hiding this comment.
This is the Python convention of defining "private" methods. Although, methods in Python cannot be truly private. This is just a way to indicate that this method should not be consumed as a public interface as it is more likely to change.
Paulinakhew
left a comment
There was a problem hiding this comment.
Love how detailed the tests are!
| return type(self) == type(other) and self._compressed_scopes == other._compressed_scopes | ||
|
|
||
| def __store_scopes(self, scopes): | ||
| sanitized_scopes = set(list(filter(None, [scope.strip() for scope in scopes]))) |
There was a problem hiding this comment.
If we don't want/need the contents of the set to change after we make it, we could make this a frozenset.
Also, I don't think you need to convert to list before converting to set/frozenset.
|
|
||
| def __store_scopes(self, scopes): | ||
| sanitized_scopes = set(list(filter(None, [scope.strip() for scope in scopes]))) | ||
| implied_scopes = set([self.__implied_scope(scope) for scope in sanitized_scopes]) |
There was a problem hiding this comment.
again, could be frozenset if you don't want this to mutate. Also, I think you can skip the square brackets. It turns it from a list comprehension into a generator, which the set initializer should be happy to consume
| match = self.IMPLIED_SCOPE_RE.match(scope) | ||
| if match: | ||
| return "{unauthenticated}read_{resource}".format( | ||
| unauthenticated="" if match.group("unauthenticated") is None else match.group("unauthenticated"), |
There was a problem hiding this comment.
We could use the boolean magic here:
unauthenticated=match.group('unauthenticated') or '',
greatestape
left a comment
There was a problem hiding this comment.
Looks good! I left a bunch of style comments. Nothing profound. I think this PR should include docs in the README.
bd11e2b to
1719e98
Compare
|
Pre-commit doesn't actually reformat your code in CI so if you want to reformat stuff locally you can do this |
| #### Constructing an ApiAccess | ||
|
|
||
| ```python | ||
| api_access = ApiAccess(["read_products", "write_orders"]) # List of access scopes | ||
| another_api_access = ApiAccess("read_products, write_products") # String of comma-delimited access scopes | ||
| ``` | ||
|
|
||
| #### Serializing ApiAccess | ||
|
|
||
| ```python | ||
| api_access = ApiAccess(["read_products", "write_orders"]) | ||
|
|
||
| access_scopes_list = list(api_access) # ["read_products", "write_orders"] | ||
| comma_delmited_access_scopes = str(api_access) # "read_products,write_orders" | ||
| ``` | ||
|
|
||
| #### Comparing ApiAccess objects | ||
|
|
||
| ##### Checking for API access equality | ||
|
|
||
| ```python | ||
| expected_api_access = ApiAccess(["read_products", "write_orders"]) | ||
|
|
||
| actual_api_access = ApiAccess(["read_products", "read_orders", "write_orders"]) | ||
| non_equal_api_access = ApiAccess(["read_products", "write_orders", "read_themes"]) | ||
|
|
||
| actual_api_access == expected_api_access # True | ||
| non_equal_api_access == expected_api_access # False | ||
| ``` | ||
|
|
||
| ##### Checking if ApiAccess covers the access of another | ||
|
|
||
| ```python | ||
| superset_access = ApiAccess(["write_products", "write_orders", "read_themes"]) | ||
| subset_access = ApiAccess(["read_products", "write_orders"]) | ||
|
|
||
| superset_access.covers(subset_access) # True | ||
| ``` |
There was a problem hiding this comment.
I think we might want to have something more targeted that illustrates how a developer could leverage this code. Is there code in our POC django app that we could simplify and excerpt here instead?
There was a problem hiding this comment.
Just moved this into a separate doc as discussed.
9afe1ff to
3914837
Compare
WHY are these changes introduced?
There are common operations that are used for managing access scopes in apps. Such operations include serializing, deserializing and normalizing scopes.
WHAT is this pull request doing?
This PR introduces the
ApiAccessvalue object that will be used to perform such operations. This helps ensure that apps do not potentially write duplicate code for scopes and delegating this responsibility to theshopify_python_api.For further reference, we made a similar move in the Shopify API ruby gem
Usage examples
Constructing
ApiAccessDelegating check for whether scopes are mismatched or subset of another
Checklist