def __init__(self, options=None, **kwargs):
        """Create Client object
        Usage::
            >>> import cloudconvert.cloudconvertrestclient as cloudconvertrestclient
            >>> rest_client = cloudconvertrestclient.CloudConvertRestClient(token='access_token', ssl_options={"cert": "/path/to/server.pem"})
        """
        kwargs = util.merge_dict(options or {}, kwargs)

        self.mode = 'sandbox' if kwargs.get("sandbox", False) else 'live'

        if self.mode != "live" and self.mode != "sandbox":
            raise exceptions.InvalidConfig("Configuration Mode Invalid",
                                           "Received: %s" % (self.mode),
                                           "Required: live or sandbox")

        self.endpoint = kwargs.get("endpoint", self.default_endpoint())
        self.sync_endpoint = kwargs.get("sync_endpoint",
                                        self.default_sync_endpoint())
        # Mandatory parameter, so not using `dict.get`
        self.proxies = kwargs.get("proxies", None)
        self.token_hash = None
        # setup SSL certificate verification if private certificate provided
        ssl_options = kwargs.get("ssl_options", {})
        if "cert" in ssl_options:
            os.environ["REQUESTS_CA_BUNDLE"] = ssl_options["cert"]

        if kwargs.get("api_key"):
            self.token_hash = {
                "access_token": kwargs["api_key"],
                "token_type": "Bearer"
            }

        self.options = kwargs
    def request(self, url, method, body=None, headers=None):
        """Make HTTP call, formats response and does error handling. Uses http_call method in CloudConvertRestClient class.
        Usage::
            >>> cloudconvertrestclient.request("https://api.sandbox.cloudconvert.com/v2/jobs/JOB-ID", "GET", {})
            >>> cloudconvertrestclient.request("https://api.sandbox.cloudconvert.com/v2/tasks/TASK-ID", "POST", "{}", {} )
        """

        http_headers = util.merge_dict(self.headers(), headers or {})

        return self.http_call(url, method, json=body, headers=http_headers)
    def request(self, url, method, body=None, headers=None):
        """Make HTTP call, formats response and does error handling. Uses http_call method in CloudConvertRestClient class.
        Usage::
            >>> cloudconvertrestclient.request("https://api.sandbox.cloudconvert.com/v2/jobs/JOB-ID", "GET", {})
            >>> cloudconvertrestclient.request("https://api.sandbox.cloudconvert.com/v2/tasks/TASK-ID", "POST", "{}", {} )
        """

        http_headers = util.merge_dict(self.headers(), headers or {})

        try:
            return self.http_call(url, method, json=body, headers=http_headers)

        # Format Error message for bad request
        except exceptions.BadRequest as error:
            return {"error": json.loads(error.content)}

        # Handle Expired token
        except exceptions.UnauthorizedAccess as error:
            if self.token_hash:
                self.token_hash = None
                return self.request(url, method, body, headers)
            else:
                raise error
Exemplo n.º 4
0
 def http_headers(self):
     """Generate HTTP header
     """
     return util.merge_dict(self.header, self.headers,
                            {'CloudConvert-Request-Id': self.generate_request_id()})