Ejemplo n.º 1
0
 def post(self, url, json=None, data=None, headers=None, **kwargs):
     # Process args
     assert isinstance(url, basestring)
     abs_url = self.urljoin(url)
     # Process listed kwargs
     request_args = {}
     assert json is None or isinstance(json, dict)
     assert headers is None or isinstance(headers, dict)
     if json and data:
         raise TypeError("You must provide either a json or data argument, "
                         "not both.")
     elif json:
         request_args['json'] = json
     elif data:
         request_args['data'] = data
     elif not json and not data:
         raise TypeError("You must provide either a json or data argument.")
     if headers:
         request_args['headers'] = headers
     # Process unlisted kwargs
     request_args['timeout'] = kwargs.pop('timeout', self.timeout)
     erc = kwargs.pop('erc', ERC['POST'])
     raise_if_extra_kwargs(kwargs)
     # API request
     response = self._req_session.post(abs_url, **request_args)
     # Process response
     check_response_code(response, erc)
     return extract_and_parse_json(response)
Ejemplo n.º 2
0
 def get_pages(self, url, params=None, **kwargs):
     # Process args
     assert isinstance(url, basestring)
     assert params is None or isinstance(params, dict)
     abs_url = self.urljoin(url)
     # Process kwargs
     timeout = kwargs.pop('timeout', self.timeout)
     erc = kwargs.pop('erc', ERC['GET'])
     raise_if_extra_kwargs(kwargs)
     # API request - get first page
     response = self._req_session.get(abs_url,
                                      params=params,
                                      timeout=timeout)
     while True:
         # Process response - Yield page's JSON data
         check_response_code(response, erc)
         yield extract_and_parse_json(response)
         # Get next page
         if response.links.get('next'):
             next_url = response.links.get('next').get('url')
             # Patch for Cisco Spark 'max=null' in next URL bug.
             next_url = _fix_next_url(next_url)
             # API request - get next page
             response = self._req_session.get(next_url, timeout=timeout)
         else:
             raise StopIteration
Ejemplo n.º 3
0
 def delete(self, url, **kwargs):
     # Process args
     assert isinstance(url, basestring)
     abs_url = self.urljoin(url)
     # Process kwargs
     timeout = kwargs.pop('timeout', self.timeout)
     erc = kwargs.pop('erc', ERC['DELETE'])
     raise_if_extra_kwargs(kwargs)
     # API request
     response = self._req_session.delete(abs_url, timeout=timeout)
     # Process response
     check_response_code(response, erc)
Ejemplo n.º 4
0
 def put(self, url, json, **kwargs):
     # Process args
     assert isinstance(url, basestring)
     assert isinstance(json, dict)
     abs_url = self.urljoin(url)
     # Process kwargs
     timeout = kwargs.pop('timeout', self.timeout)
     erc = kwargs.pop('erc', ERC['PUT'])
     raise_if_extra_kwargs(kwargs)
     # API request
     response = self._req_session.put(abs_url, json=json, timeout=timeout)
     # Process response
     check_response_code(response, erc)
     return extract_and_parse_json(response)
Ejemplo n.º 5
0
 def get(self, url, params=None, **kwargs):
     # Process args
     assert isinstance(url, basestring)
     assert params is None or isinstance(params, dict)
     abs_url = self.urljoin(url)
     # Process kwargs
     timeout = kwargs.pop('timeout', self.timeout)
     erc = kwargs.pop('erc', ERC['GET'])
     raise_if_extra_kwargs(kwargs)
     # API request
     response = self._req_session.get(abs_url,
                                      params=params,
                                      timeout=timeout)
     # Process response
     check_response_code(response, erc)
     return extract_and_parse_json(response)
Ejemplo n.º 6
0
    def get(self, client_id, client_secret, code, redirect_uri):
        """Exchange an Authorization Code for an Access Token.

        Exchange an Authorization Code for an Access Token that can be used to
        invoke the APIs.

        Args:
            client_id(basestring): Provided when you created your
                integration.
            client_secret(basestring): Provided when you created your
                integration.
            code(basestring): The Authorization Code provided by the user
                OAuth process.
            redirect_uri(basestring): The redirect URI used in the user OAuth
                process.

        Returns:
            AccessToken: With the access token provided by the Cisco Spark
                cloud.

        Raises:
            AssertionError: If the parameter types are incorrect.
            SparkApiError: If the Cisco Spark cloud returns an error.

        """
        # Process args
        assert isinstance(client_id, basestring)
        assert isinstance(client_secret, basestring)
        assert isinstance(code, basestring)
        assert isinstance(redirect_uri, basestring)
        # Build request parameters
        data = {}
        data["grant_type"] = "authorization_code"
        data["client_id"] = client_id
        data["client_secret"] = client_secret
        data["code"] = code
        data["redirect_uri"] = redirect_uri
        # API request
        response = requests.post(self._endpoint_url,
                                 data=data,
                                 **self._request_kwargs)
        check_response_code(response, ERC['POST'])
        json_data = extract_and_parse_json(response)
        # Return a AccessToken object created from the response JSON data
        return AccessToken(json_data)
Ejemplo n.º 7
0
    def get(self, client_id, client_secret, code, redirect_uri):
        """Exchange an Authorization Code for an Access Token.

        Exchange an Authorization Code for an Access Token that can be used to
        invoke the APIs.

        Args:
            client_id(string_types): Provided when you created your
                integration.
            client_secret(string_types): Provided when you created your
                integration.
            code(string_types): The Authorization Code provided by the user
                OAuth process.
            redirect_uri(string_types): The redirect URI used in the user OAuth
                process.

        Returns:
            AccessToken: With the access token provided by the Cisco Spark
                cloud.

        Raises:
            AssertionError: If the parameter types are incorrect.
            SparkApiError: If the Cisco Spark cloud returns an error.

        """
        # Process args
        assert isinstance(client_id, string_types)
        assert isinstance(client_secret, string_types)
        assert isinstance(code, string_types)
        assert isinstance(redirect_uri, string_types)
        # Build request parameters
        data = {}
        data["grant_type"] = "authorization_code"
        data["client_id"] = client_id
        data["client_secret"] = client_secret
        data["code"] = code
        data["redirect_uri"] = redirect_uri
        # API request
        response = requests.post(self._endpoint_url, data=data,
                                 **self._request_kwargs)
        check_response_code(response, ERC['POST'])
        json_data = extract_and_parse_json(response)
        # Return a AccessToken object created from the response JSON data
        return AccessToken(json_data)
Ejemplo n.º 8
0
    def refresh(self, client_id, client_secret, refresh_token):
        """Return a refreshed Access Token via the provided refresh_token.

        Args:
            client_id(basestring): Provided when you created your
                integration.
            client_secret(basestring): Provided when you created your
                integration.
            refresh_token(basestring): Provided when you requested the Access
                Token.

        Returns:
            AccessToken: With the access token provided by the Cisco Spark
                cloud.

        Raises:
            AssertionError: If the parameter types are incorrect.
            SparkApiError: If the Cisco Spark cloud returns an error.

        """
        # Process args
        assert isinstance(client_id, basestring)
        assert isinstance(client_secret, basestring)
        assert isinstance(refresh_token, basestring)
        # Build request parameters
        data = {}
        data["grant_type"] = "refresh_token"
        data["client_id"] = client_id
        data["client_secret"] = client_secret
        data["refresh_token"] = refresh_token
        # API request
        response = requests.post(self._endpoint_url,
                                 data=data,
                                 **self._request_kwargs)
        check_response_code(response, ERC['POST'])
        json_data = extract_and_parse_json(response)
        # Return a AccessToken object created from the response JSON data
        return AccessToken(json_data)
Ejemplo n.º 9
0
    def refresh(self, client_id, client_secret, refresh_token):
        """Return a refreshed Access Token via the provided refresh_token.

        Args:
            client_id(string_types): Provided when you created your
                integration.
            client_secret(string_types): Provided when you created your
                integration.
            refresh_token(string_types): Provided when you requested the Access
                Token.

        Returns:
            AccessToken: With the access token provided by the Cisco Spark
                cloud.

        Raises:
            AssertionError: If the parameter types are incorrect.
            SparkApiError: If the Cisco Spark cloud returns an error.

        """
        # Process args
        assert isinstance(client_id, string_types)
        assert isinstance(client_secret, string_types)
        assert isinstance(refresh_token, string_types)
        # Build request parameters
        data = {}
        data["grant_type"] = "refresh_token"
        data["client_id"] = client_id
        data["client_secret"] = client_secret
        data["refresh_token"] = refresh_token
        # API request
        response = requests.post(self._endpoint_url, data=data,
                                 **self._request_kwargs)
        check_response_code(response, ERC['POST'])
        json_data = extract_and_parse_json(response)
        # Return a AccessToken object created from the response JSON data
        return AccessToken(json_data)