Пример #1
0
    def _base_request(self, url, content_type, response_type, data):
        """Send a generic authenticated POST request.

        Args:
            url (str): URL of request.
            content_type (str): Request content type.
            response_type (str): The desired response format. Valid options
                are: 'json' (JSON), 'protojson' (pblite), and 'proto' (binary
                Protocol Buffer). 'proto' requires manually setting an extra
                header 'X-Goog-Encode-Response-If-Executable: base64'.
            data (str): Request body data.

        Returns:
            FetchResponse: Response containing HTTP code, cookies, and body.

        Raises:
            NetworkError: If the request fails.
        """
        sapisid_cookie = self._get_cookie('SAPISID')
        headers = channel.get_authorization_headers(sapisid_cookie)
        headers['content-type'] = content_type
        required_cookies = ['SAPISID', 'HSID', 'SSID', 'APISID', 'SID']
        cookies = {cookie: self._get_cookie(cookie)
                   for cookie in required_cookies}
        params = {
            # "alternative representation type" (desired response format).
            'alt': response_type,
        }
        res = yield from http_utils.fetch(
            'post', url, headers=headers, cookies=cookies, params=params,
            data=data, connector=self._connector
        )
        return res
Пример #2
0
    def _base_request(self, url, content_type, response_type, data):
        """Send a generic authenticated POST request.

        Args:
            url (str): URL of request.
            content_type (str): Request content type.
            response_type (str): The desired response format. Valid options
                are: 'json' (JSON), 'protojson' (pblite), and 'proto' (binary
                Protocol Buffer). 'proto' requires manually setting an extra
                header 'X-Goog-Encode-Response-If-Executable: base64'.
            data (str): Request body data.

        Returns:
            FetchResponse: Response containing HTTP code, cookies, and body.

        Raises:
            NetworkError: If the request fails.
        """
        sapisid_cookie = self._get_cookie('SAPISID')
        headers = channel.get_authorization_headers(sapisid_cookie)
        headers['content-type'] = content_type
        required_cookies = ['SAPISID', 'HSID', 'SSID', 'APISID', 'SID']
        cookies = {cookie: self._get_cookie(cookie)
                   for cookie in required_cookies}
        params = {
            # "alternative representation type" (desired response format).
            'alt': response_type,
        }
        res = yield from http_utils.fetch(
            'post', url, headers=headers, cookies=cookies, params=params,
            data=data, connector=self._connector
        )
        return res
Пример #3
0
    def _base_request(self, url, content_type, data, use_json=True):
        """Make API request.

        Raises hangups.NetworkError if the request fails.
        """
        headers = channel.get_authorization_headers(
            self._get_cookie('SAPISID'))
        headers['content-type'] = content_type
        required_cookies = ['SAPISID', 'HSID', 'SSID', 'APISID', 'SID']
        cookies = {
            cookie: self._get_cookie(cookie)
            for cookie in required_cookies
        }
        params = {
            'key': self._api_key,
            'alt': 'json' if use_json else 'protojson',
        }
        res = yield from http_utils.fetch('post',
                                          url,
                                          headers=headers,
                                          cookies=cookies,
                                          params=params,
                                          data=data,
                                          connector=self._connector)
        logger.debug('Response to request for {} was {}:\n{}'.format(
            url, res.code, res.body))
        return res
Пример #4
0
    def _request(self, endpoint, body_json, use_json=True):
        """Make chat API request.

        Raises hangups.NetworkError if the request fails.
        """
        url = 'https://clients6.google.com/chat/v1/{}'.format(endpoint)
        headers = channel.get_authorization_headers(
            self._get_cookie('SAPISID'))
        headers['content-type'] = 'application/json+protobuf'
        required_cookies = ['SAPISID', 'HSID', 'SSID', 'APISID', 'SID']
        cookies = {
            cookie: self._get_cookie(cookie)
            for cookie in required_cookies
        }
        params = {
            'key': self._api_key,
            'alt': 'json' if use_json else 'protojson',
        }
        res = yield from http_utils.fetch('post',
                                          url,
                                          headers=headers,
                                          cookies=cookies,
                                          params=params,
                                          data=json.dumps(body_json),
                                          connector=self._connector)
        logger.debug('Response to request for {} was {}:\n{}'.format(
            endpoint, res.code, res.body))
        return res
Пример #5
0
    def _base_request(self, url, content_type, data, use_json=True):
        """Make API request.

        Raises hangups.NetworkError if the request fails.
        """
        headers = channel.get_authorization_headers(self._get_cookie('SAPISID'))
        headers['content-type'] = content_type
        required_cookies = ['SAPISID', 'HSID', 'SSID', 'APISID', 'SID']
        cookies = {cookie: self._get_cookie(cookie)
                   for cookie in required_cookies}
        params = {
            'key': self._api_key,
            'alt': 'json' if use_json else 'protojson',
        }
        res = yield from http_utils.fetch(
            'post', url, headers=headers, cookies=cookies, params=params,
            data=data, connector=self._connector
        )
        logger.debug('Response to request for {} was {}:\n{}'
                     .format(url, res.code, res.body))
        return res
Пример #6
0
    def _request(self, endpoint, body_json, use_json=True):
        """Make chat API request.

        Raises hangups.NetworkError if the request fails.
        """
        url = 'https://clients6.google.com/chat/v1/{}'.format(endpoint)
        headers = channel.get_authorization_headers(self._get_cookie('SAPISID'))
        headers['content-type'] = 'application/json+protobuf'
        required_cookies = ['SAPISID', 'HSID', 'SSID', 'APISID', 'SID']
        cookies = {cookie: self._get_cookie(cookie)
                   for cookie in required_cookies}
        params = {
            'key': self._api_key,
            'alt': 'json' if use_json else 'protojson',
        }
        res = yield from http_utils.fetch(
            'post', url, headers=headers, cookies=cookies, params=params,
            data=json.dumps(body_json), connector=self._connector
        )
        logger.debug('Response to request for {} was {}:\n{}'
                     .format(endpoint, res.code, res.body))
        return res
Пример #7
0
    def _base_request(self, url, content_type, response_type, data):
        """Send a generic authenticated POST request.

        Args:
            url (str): URL of request.
            content_type (str): Request content type.
            response_type (str): The desired response format. Valid options
                are: 'json' (JSON), 'protojson' (pblite), and 'proto' (binary
                Protocol Buffer). 'proto' requires manually setting an extra
                header 'X-Goog-Encode-Response-If-Executable: base64'.
            data (str): Request body data.

        Returns:
            FetchResponse: Response containing HTTP code, cookies, and body.

        Raises:
            NetworkError: If the request fails.
        """
        sapisid_cookie = self._get_cookie('SAPISID')
        headers = channel.get_authorization_headers(sapisid_cookie)
        headers['content-type'] = content_type
        # This header is required for Protocol Buffer responses, which causes
        # them to be base64 encoded:
        headers['X-Goog-Encode-Response-If-Executable'] = 'base64'
        params = {
            # "alternative representation type" (desired response format).
            'alt': response_type,
            # API key (required to avoid 403 Forbidden "Daily Limit for
            # Unauthenticated Use Exceeded. Continued use requires signup").
            'key': API_KEY,
        }
        res = yield from http_utils.fetch(
            self._session, 'post', url, headers=headers, params=params,
            data=data
        )
        return res