예제 #1
0
 def request(self, op: str, endpoint: str = None, *,
             params: typing.Dict[str, typing.Union[str, typing.List[str]]] = None,
             headers: typing.Dict[str, str] = None, data: typing.Union[str, bytes, QtCore.QBuffer] = None,
             request: QtNetwork.QNetworkRequest = None):
     """Issues a new request to Twitch's API."""
     # Stitch the endpoint to the API url
     api = QtCore.QUrl(self.HELIX)
     path = api.path().rstrip('/')
     
     api.setPath(f'{path}/{endpoint}')
     
     # Stitch the url query
     q = QtCore.QUrlQuery()
     
     # Add the parameters to the query
     for key, value in params.items():
         if isinstance(value, list):
             for v in value:
                 q.addQueryItem(key, v)
         
         else:
             q.addQueryItem(key, value)
     
     # Assign the query to the url
     api.setQuery(q)
     
     # Create a request object if it does not exist
     if request is None:
         request = QtNetwork.QNetworkRequest(api)
         
         # Populate the request's headers
         for key, value in headers.items():
             request.setRawHeader(key.encode(), value.encode())
     
     # Ensure the Twitch API headers exist
     if not request.hasRawHeader(b'Client-ID'):
         request.setRawHeader(b'Client-ID', self.client_id.encode())
     
     if not request.hasRawHeader(b'Authorization'):
         request.setRawHeader(b'Authorization', f'OAuth {self.token}'.encode())
     
     return self._request(op, request, data=data).json()
예제 #2
0
    def request_obj(self, url, headers=None, body=None):
        """ Return a QNetworkRequest object """
        request = QNetworkRequest()
        request.setUrl(to_qurl(url))
        request.setOriginatingObject(self.web_page.mainFrame())

        if headers is not None:
            self.web_page.skip_custom_headers = True
            self._set_request_headers(request, headers)

        if body and not request.hasRawHeader(b"content-type"):
            # there is POST body but no content-type
            # QT will set this header, but it will complain so better to do this here
            request.setRawHeader(b"content-type", b"application/x-www-form-urlencoded")

        return request
예제 #3
0
    def request_obj(self, url, headers=None, body=None):
        """ Return a QNetworkRequest object """
        request = QNetworkRequest()
        request.setUrl(to_qurl(url))
        request.setOriginatingObject(self.web_page.mainFrame())

        if headers is not None:
            self.web_page.skip_custom_headers = True
            self._set_request_headers(request, headers)

        if body and not request.hasRawHeader(b"content-type"):
            # there is POST body but no content-type
            # QT will set this header, but it will complain so better to do this here
            request.setRawHeader(b"content-type",
                                 b"application/x-www-form-urlencoded")

        return request