def getEntity(res: Response, returnRawJSON: bool = True): """ Deserializes a Response object to POJO of some type. @param res: response @param returnRawJSON: true if an array should be returned @return: an object """ if res: code = res.status_code if code == 200 or code == 201 or code == 304: if returnRawJSON: try: body = res.json() return body if body else {} except JSONDecodeError: return res.text else: obj = ParaObject() obj.setFields(res.json()) return obj elif code != 404 or code != 304 or code != 204: error = res.json() if error and error["code"]: msg = (error["message"] if error["message"] else "error") logging.error(msg + " - " + error["code"]) else: logging.error(code + " - " + res.reason) return None
def signIn(self, provider: str, providertoken: str, rememberjwt: bool = True): """ Takes an identity provider access token and fetches the user data from that provider. A new User object is created if that user doesn't exist. Access tokens are returned upon successful authentication using one of the SDKs from Facebook, Google, Twitter, etc. <b>Note:</b> Twitter uses OAuth 1 and gives you a token and a token secret. <b>You must concatenate them like this: <code>{oauth_token}:{oauth_token_secret}</code> and use that as the provider access token.</b> @param provider: identity provider, e.g. 'facebook', 'google'... @param providertoken: access token from a provider like Facebook, Google, Twitter @param rememberjwt: it true the access token returned by Para will be stored locally and available through getAccessToken(). True by default. @return: a User object or None if something failed """ if not provider or not providertoken: return None credentials = {"appid": self.__accessKey, "provider": provider, "token": providertoken} result = self.getEntity(self.invokePost(self.JWT_PATH, json.dumps(credentials))) if result and result["user"] and result["jwt"]: jwt_data = result["jwt"] if rememberjwt: self.__tokenKey = jwt_data["access_token"] self.__tokenKeyExpires = jwt_data["expires"] self.__tokenKeyNextRefresh = jwt_data["refresh"] obj = ParaObject() obj.setFields(result["user"]) return obj else: self.clearAccessToken() return None
def testSetFields(self): o1 = ParaObject("123", "dog") o1.one = 1 o1.two = "two" assert o1.type == "dog" assert o1.id == "123" obj = json.loads(o1.jsonSerialize()) obj1 = ParaObject() obj1.setFields(obj) assert obj1.type == "dog" assert obj1.id == "123" assert obj1.two == "two"
def getItemsFromList(result: list): """ Deserializes ParaObjects from a JSON array (the "items:[]" field in search results). @param result: a list of deserialized objects @return: a list of ParaObjects """ if result and len(result) > 0: # this isn't very efficient but there's no way to know what type of objects we're reading objects = [] for obj in result: if obj and len(obj) > 0: p = ParaObject() p.setFields(obj) objects.append(p) return objects return []