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

Users and Spaces V3 implementation #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pubnub/endpoints/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Endpoint(object):

__metaclass__ = ABCMeta
_path = None
_custom_headers = None

def __init__(self, pubnub):
self.pubnub = pubnub
Expand Down Expand Up @@ -99,7 +100,8 @@ def request_headers(self):
headers["Content-Encoding"] = "gzip"
if self.http_method() == HttpMethod.POST:
headers["Content-type"] = "application/json"

if self._custom_headers:
Copy link
Contributor

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 equal None. In this case you should check if your variable is None or not. I assume that custom_headers variable is a dict. So you can change initial value from None to empty dict ;)

headers.update(self._custom_headers)
return headers

def build_file_upload_request(self):
Expand Down
228 changes: 228 additions & 0 deletions pubnub/endpoints/entities/space.py
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.
70 changes: 0 additions & 70 deletions pubnub/endpoints/entities/space/create_space.py

This file was deleted.

31 changes: 0 additions & 31 deletions pubnub/endpoints/entities/space/fetch_space.py

This file was deleted.

29 changes: 0 additions & 29 deletions pubnub/endpoints/entities/space/fetch_spaces.py

This file was deleted.

Loading