From d84905af3038bc2fcc16768fe243814a7f659c98 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sun, 23 Apr 2017 14:45:18 +0200 Subject: [PATCH] Fixes #10 --- diaspy/errors.py | 12 ++++++++++++ diaspy/people.py | 22 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/diaspy/errors.py b/diaspy/errors.py index e7ff00d..3dbf9e7 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -40,6 +40,18 @@ class TokenError(DiaspyError): pass +class DataError(DiaspyError): + pass + + +class InvalidDataError(DataError): + pass + + +class KeyMissingFromFetchedData(InvalidDataError): + pass + + class UserError(DiaspyError): """Exception raised when something related to users goes wrong. """ diff --git a/diaspy/people.py b/diaspy/people.py index 8483d88..4410c4f 100644 --- a/diaspy/people.py +++ b/diaspy/people.py @@ -42,6 +42,26 @@ class User(): optional parameters. GUID takes precedence over handle when fetching user stream. When fetching user data, handle is required. """ + @classmethod + def parse(cls, connection, data): + person = data.get('person') + if person is None: + raise errors.KeyMissingFromFetchedData('person', data) + + guid = person.get('guid') + if guid is None: + raise errors.KeyMissingFromFetchedData('guid', person) + + handle = person.get('diaspora_id') + if handle is None: + raise errors.KeyMissingFromFetchedData('diaspora_id', person) + + person_id = person.get('id') + if person_id is None: + raise errors.KeyMissingFromFetchedData('id', person) + + return User(connection, guid, handle, id) + def __init__(self, connection, guid='', handle='', fetch='posts', id=0): self._connection = connection self.stream = [] @@ -206,4 +226,4 @@ class Contacts(): request = self._connection.get('contacts.json', params=params) if request.status_code != 200: raise Exception('status code {0}: cannot get contacts'.format(request.status_code)) - return [User(self._connection, guid=user['guid'], handle=user['handle'], fetch=None) for user in request.json()] + return [User.parse(self._connection, each) for each in request.json()] -- 2.25.1