def get(self,
                identifier1=None,
                identifier2=None,
                params=None,
                data=None):
            """ Cloudflare v4 API"""

            raise CloudFlareAPIError(
                0, 'get() call not available for this endpoint')
        def call_with_certauth(self, method,
                               api_call_part1,
                               api_call_part2=None,
                               api_call_part3=None,
                               identifier1=None, identifier2=None,
                               params=None, data=None):
            """ CloudFlare v4 API"""

            if self.certtoken is '':
                raise CloudFlareAPIError(0, 'no email and/or cert token defined')
            headers = {
                'X-Auth-User-Service-Key': self.certtoken,
                'Content-Type': 'application/json'
            }
            return self._call(method, headers,
                              api_call_part1, api_call_part2, api_call_part3,
                              identifier1, identifier2,
                              params, data)
Exemple #3
0
        def call_with_auth(self,
                           method,
                           api_call_part1,
                           api_call_part2=None,
                           api_call_part3=None,
                           identifier1=None,
                           identifier2=None,
                           params=None,
                           data=None):
            """ Cloudflare v4 API"""

            if self.email is '' or self.token is '':
                raise CloudFlareAPIError(0, 'no email and/or token defined')
            headers = {
                'User-Agent': self.user_agent,
                'X-Auth-Email': self.email,
                'X-Auth-Key': self.token,
                'Content-Type': 'application/json'
            }
            return self._call(method, headers, api_call_part1, api_call_part2,
                              api_call_part3, identifier1, identifier2, params,
                              data)
        def _call(self,
                  method,
                  headers,
                  api_call_part1,
                  api_call_part2=None,
                  api_call_part3=None,
                  identifier1=None,
                  identifier2=None,
                  params=None,
                  data=None):
            """ Cloudflare v4 API"""

            response_data = self._raw(method, headers, api_call_part1,
                                      api_call_part2, api_call_part3,
                                      identifier1, identifier2, params, data)
            if response_data['success'] is False:
                code = response_data['errors'][0]['code']
                message = response_data['errors'][0]['message']
                if self.logger:
                    self.logger.debug('Response: error %d %s' %
                                      (code, message))
                raise CloudFlareAPIError(code, message)

            if self.logger:
                self.logger.debug('Response: %s' % (response_data['result']))
            if self.raw:
                result = {}
                # theres always a result value
                result['result'] = response_data['result']
                # theres may not be a result_info on every call
                if 'result_info' in response_data:
                    result['result_info'] = response_data['result_info']
                # no need to return success, errors, or messages as they return via an exception
            else:
                # theres always a result value
                result = response_data['result']
            return result
Exemple #5
0
        def _raw(self,
                 method,
                 headers,
                 api_call_part1,
                 api_call_part2=None,
                 api_call_part3=None,
                 identifier1=None,
                 identifier2=None,
                 params=None,
                 data=None):
            """ Cloudflare v4 API"""

            if api_call_part2 is not None or (data is not None
                                              and method == 'GET'):
                if identifier2 is None:
                    url = (self.base_url + '/' + api_call_part1 + '/' +
                           identifier1 + '/' + api_call_part2)
                else:
                    url = (self.base_url + '/' + api_call_part1 + '/' +
                           identifier1 + '/' + api_call_part2 + '/' +
                           identifier2)
            else:
                if identifier1 is None:
                    url = (self.base_url + '/' + api_call_part1)
                else:
                    url = (self.base_url + '/' + api_call_part1 + '/' +
                           identifier1)
            if api_call_part3:
                url += '/' + api_call_part3

            if self.logger:
                self.logger.debug('Call: %s,%s,%s,%s,%s' %
                                  (str(api_call_part1), str(identifier1),
                                   str(api_call_part2), str(identifier2),
                                   str(api_call_part3)))
                self.logger.debug('Call: optional params and data %s %s' %
                                  (str(params), str(data)))
                self.logger.debug('Call: method and url %s %s' %
                                  (str(method), str(url)))
                self.logger.debug('Call: headers %s' %
                                  str(sanitize_secrets(headers)))

            if (method is None) or (api_call_part1 is None):
                # should never happen
                raise CloudFlareInternalError(
                    0, 'You must specify a method and endpoint')

            method = method.upper()

            if self.logger:
                self.logger.debug('Call: doit!')

            try:
                if method == 'GET':
                    response = requests.get(url,
                                            headers=headers,
                                            params=params,
                                            data=data)
                elif method == 'POST':
                    response = requests.post(url,
                                             headers=headers,
                                             params=params,
                                             json=data)
                elif method == 'PUT':
                    response = requests.put(url,
                                            headers=headers,
                                            params=params,
                                            json=data)
                elif method == 'DELETE':
                    response = requests.delete(url, headers=headers, json=data)
                elif method == 'PATCH':
                    response = requests.request('PATCH',
                                                url,
                                                headers=headers,
                                                params=params,
                                                json=data)
                else:
                    # should never happen
                    raise CloudFlareAPIError(0, 'method not supported')
                if self.logger:
                    self.logger.debug('Call: done!')
            except Exception as e:
                if self.logger:
                    self.logger.debug('Call: exception!')
                raise CloudFlareAPIError(0, 'connection failed.')

            if self.logger:
                self.logger.debug('Response: url %s', response.url)

            response_data = response.text
            if self.logger:
                self.logger.debug('Response: data %s' % response_data)
            try:
                response_data = json.loads(response_data)
            except ValueError:
                raise CloudFlareAPIError(0, 'JSON parse failed.')

            return response_data
Exemple #6
0
        def _call(self,
                  method,
                  headers,
                  api_call_part1,
                  api_call_part2=None,
                  api_call_part3=None,
                  identifier1=None,
                  identifier2=None,
                  params=None,
                  data=None):
            """ Cloudflare v4 API"""

            response_data = self._raw(method, headers, api_call_part1,
                                      api_call_part2, api_call_part3,
                                      identifier1, identifier2, params, data)

            # Sanatize the returned results - just in case API is messed up
            if 'success' not in response_data:
                if 'errors' in response_data:
                    if self.logger:
                        self.logger.debug(
                            'Response: assuming success = "False"')
                    response_data['success'] = False
                else:
                    if 'result' not in response_data:
                        # Only happens on /certificates call
                        # should be fixed in /certificates API
                        if self.logger:
                            self.logger.debug(
                                'Response: assuming success = "False"')
                        r = response_data
                        response_data['errors'] = []
                        response_data['errors'].append(r)
                        response_data['success'] = False
                    else:
                        if self.logger:
                            self.logger.debug(
                                'Response: assuming success = "True"')
                        response_data['success'] = True

            if response_data['success'] is False:
                errors = response_data['errors'][0]
                code = errors['code']
                if 'message' in errors:
                    message = errors['message']
                elif 'error' in errors:
                    message = errors['error']
                else:
                    message = ''
                if 'error_chain' in errors:
                    error_chain = errors['error_chain']
                    for error in error_chain:
                        if self.logger:
                            self.logger.debug(
                                'Response: error %d %s - chain' %
                                (error['code'], error['message']))
                    if self.logger:
                        self.logger.debug('Response: error %d %s' %
                                          (code, message))
                    raise CloudFlareAPIError(code, message, error_chain)
                else:
                    if self.logger:
                        self.logger.debug('Response: error %d %s' %
                                          (code, message))
                    raise CloudFlareAPIError(code, message)

            if self.logger:
                self.logger.debug('Response: %s' % (response_data['result']))
            if self.raw:
                result = {}
                # theres always a result value
                result['result'] = response_data['result']
                # theres may not be a result_info on every call
                if 'result_info' in response_data:
                    result['result_info'] = response_data['result_info']
                # no need to return success, errors, or messages as they return via an exception
            else:
                # theres always a result value
                result = response_data['result']
            return result