-
Notifications
You must be signed in to change notification settings - Fork 116
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
Users and Spaces V3 implementation #136
Open
seba-aln
wants to merge
1
commit into
master
Choose a base branch
from
fix/change-user-endpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
from pubnub.endpoints.entities.endpoint import EntitiesEndpoint | ||
from pubnub.enums import PNOperationType | ||
from pubnub.enums import HttpMethod | ||
from pubnub.errors import PNERR_SPACE_MISSING | ||
from pubnub.exceptions import PubNubException | ||
from pubnub.models.consumer.entities.page import Next, Previous | ||
from pubnub.models.consumer.entities.space import PNCreateSpaceResult, PNRemoveSpaceResult, PNUpdateSpaceResult, \ | ||
PNUpsertSpaceResult, PNFetchSpaceResult, PNFetchSpacesResult | ||
from pubnub.models.consumer.objects_v2.page import PNPage | ||
from pubnub.utils import write_value_as_string | ||
|
||
|
||
class EntitiesSpaceEndpoint(EntitiesEndpoint): | ||
SPACE_PATH = "/v3/objects/%s/spaces/%s" | ||
_space_id = None | ||
_name = None | ||
_description = None | ||
_space_type = None | ||
_space_status = None | ||
_custom = None | ||
_operation_type = None | ||
_operation_name = None | ||
_operation_http_method = None | ||
|
||
def __init__(self, pubnub, space_id, name: str = None, description: str = None, space_type: str = None, | ||
space_status: str = None, custom: dict = None): | ||
EntitiesEndpoint.__init__(self, pubnub) | ||
|
||
self._space_id = str(space_id) | ||
if name is not None: | ||
self._name = str(name) | ||
|
||
if description is not None: | ||
self._description = str(description) | ||
|
||
if custom is not None: | ||
self._custom = dict(custom) if custom else None | ||
self._include_custom = True | ||
|
||
if space_type is not None: | ||
self._space_type = str(space_type) | ||
self._include_type = True | ||
|
||
if space_status is not None: | ||
self._space_status = str(space_status) | ||
self._include_status = True | ||
|
||
def _validate_space_id(self): | ||
if self._space_id is None or len(self._space_id) == 0: | ||
raise PubNubException(pn_error=PNERR_SPACE_MISSING) | ||
|
||
def build_path(self): | ||
return self.SPACE_PATH % (self.pubnub.config.subscribe_key, self._space_id) | ||
|
||
def build_data(self): | ||
payload = {} | ||
|
||
if self._name: | ||
payload['name'] = self._name | ||
if self._description: | ||
payload['description'] = self._description | ||
if self._custom: | ||
payload['custom'] = self._custom | ||
if self._space_status: | ||
payload['status'] = self._space_status | ||
if self._space_type: | ||
payload['type'] = self._space_type | ||
|
||
return write_value_as_string(payload) | ||
|
||
def validate_specific_params(self): | ||
self._validate_space_id() | ||
|
||
def operation_type(self): | ||
return self._operation_type | ||
|
||
def name(self): | ||
return self._operation_name | ||
|
||
def http_method(self): | ||
return self._operation_http_method | ||
|
||
|
||
class CreateSpace(EntitiesSpaceEndpoint): | ||
def __init__(self, pubnub, space_id, name: str = None, description: str = None, space_type: str = None, | ||
space_status: str = None, custom: dict = None): | ||
super().__init__(pubnub, space_id, name, description, space_type, space_status, custom) | ||
|
||
self._operation_type = PNOperationType.PNCreateSpaceOperation | ||
self._operation_name = "Create Space V3" | ||
self._operation_http_method = HttpMethod.POST | ||
|
||
def create_response(self, envelope) -> PNCreateSpaceResult: | ||
return PNCreateSpaceResult(envelope) | ||
|
||
|
||
class UpdateSpace(EntitiesSpaceEndpoint): | ||
def __init__(self, pubnub, space_id, name: str = None, description: str = None, space_type: str = None, | ||
space_status: str = None, custom: dict = None): | ||
super().__init__(pubnub, space_id, name, description, space_type, space_status, custom) | ||
|
||
self._operation_type = PNOperationType.PNUpdateSpaceOperation | ||
self._operation_name = "Update Space V3" | ||
self._operation_http_method = HttpMethod.PATCH | ||
self._custom_headers = {"Content-type": "application/json"} | ||
|
||
def create_response(self, envelope) -> PNUpdateSpaceResult: | ||
return PNUpdateSpaceResult(envelope) | ||
|
||
|
||
class UpsertSpace(EntitiesSpaceEndpoint): | ||
def __init__(self, pubnub, space_id, name: str = None, description: str = None, space_type: str = None, | ||
space_status: str = None, custom: dict = None): | ||
super().__init__(pubnub, space_id, name, description, space_type, space_status, custom) | ||
|
||
self._operation_type = PNOperationType.PNUpsertSpaceOperation | ||
self._operation_name = "Upsert Space V3" | ||
self._operation_http_method = HttpMethod.PUT | ||
self._custom_headers = {"Content-type": "application/json"} | ||
|
||
def create_response(self, envelope) -> PNUpsertSpaceResult: | ||
return PNUpsertSpaceResult(envelope) | ||
|
||
|
||
class RemoveSpace(EntitiesSpaceEndpoint): | ||
def __init__(self, pubnub, space_id: str): | ||
super().__init__(pubnub, space_id) | ||
|
||
self._operation_type = PNOperationType.PNRemoveSpaceOperation | ||
self._operation_name = "Remove Space V3" | ||
self._operation_http_method = HttpMethod.DELETE | ||
|
||
def create_response(self, envelope): | ||
return PNRemoveSpaceResult(envelope) | ||
|
||
def build_data(self): | ||
return '' | ||
|
||
|
||
class FetchSpace(EntitiesSpaceEndpoint): | ||
def __init__(self, pubnub, space_id: str, include: list = []): | ||
super().__init__(pubnub, space_id) | ||
|
||
self._operation_type = PNOperationType.PNFetchSpaceOperation | ||
self._operation_name = "Fetch Space V3" | ||
|
||
self._operation_http_method = HttpMethod.GET | ||
|
||
if 'custom' in include: | ||
self._include_custom = True | ||
|
||
if 'status' in include: | ||
self._include_status = True | ||
|
||
if 'type' in include: | ||
self._include_type = True | ||
|
||
def create_response(self, envelope): | ||
return PNFetchSpaceResult(envelope) | ||
|
||
def build_data(self): | ||
return '' | ||
|
||
|
||
class FetchSpaces(EntitiesEndpoint): | ||
SPACE_PATH = "/v3/objects/%s/spaces" | ||
|
||
def __init__(self, pubnub, limit: str = None, filter_string: str = None, include_total_count: bool = None, | ||
include_custom: bool = None, page: PNPage = None, sort_keys=None): | ||
super().__init__(pubnub) | ||
|
||
self._limit = limit | ||
self._filter = filter_string | ||
self._include_total_count = include_total_count | ||
self._sort_keys = sort_keys | ||
self._page = page | ||
self._include_custom = include_custom | ||
|
||
def build_path(self): | ||
return self.SPACE_PATH % self.pubnub.config.subscribe_key | ||
|
||
def create_response(self, envelope): | ||
return PNFetchSpacesResult(envelope) | ||
|
||
def operation_type(self): | ||
return PNOperationType.PNFetchSpacesOperation | ||
|
||
def name(self): | ||
return "Fetch Spaces" | ||
|
||
def http_method(self): | ||
return HttpMethod.GET | ||
|
||
def custom_params(self): | ||
params = {} | ||
inclusions = [] | ||
|
||
if self._include_custom: | ||
inclusions.append("custom") | ||
|
||
if self._filter: | ||
params["filter"] = str(self._filter) | ||
|
||
if self._limit: | ||
params["limit"] = int(self._limit) | ||
|
||
if self._include_total_count: | ||
params["count"] = bool(self._include_total_count) | ||
|
||
if self._sort_keys: | ||
joined_sort_params_array = [] | ||
for sort_key in self._sort_keys: | ||
joined_sort_params_array.append("%s:%s" % (sort_key.key_str(), sort_key.dir_str())) | ||
|
||
params["sort"] = ",".join(joined_sort_params_array) | ||
|
||
if self._page: | ||
if isinstance(self._page, Next): | ||
params["start"] = self._page.hash | ||
elif isinstance(self._page, Previous): | ||
params["end"] = self._page.hash | ||
else: | ||
raise ValueError() | ||
|
||
if len(inclusions) > 0: | ||
params["include"] = ",".join(inclusions) | ||
|
||
return params |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
https://peps.python.org/pep-0008/#programming-recommendations
Initially
self._custom_headers
is equalNone
. In this case you should check if your variable is None or not. I assume thatcustom_headers
variable is a dict. So you can change initial value from None to empty dict ;)