Exemple #1
0
    def request(self, method, api_url, params={}, headers=None,
                file_name=None, file_content=None, axapi_args=None, **kwargs):
        LOG.debug("axapi_http: full url = %s", self.url_base + api_url)
        LOG.debug("axapi_http: %s url = %s", method, api_url)
        LOG.debug("axapi_http: params = %s", json.dumps(logutils.clean(params), indent=4))

        # Update params with axapi_args for currently unsupported configuration of objects
        if axapi_args is not None:
            formatted_axapi_args = dict([(k.replace('_', '-'), v) for k, v in
                                        axapi_args.iteritems()])
            params = acos_client.v21.axapi_http.merge_dicts(params, formatted_axapi_args)

        if (file_name is None and file_content is not None) or \
           (file_name is not None and file_content is None):
            raise ValueError("file_name and file_content must both be "
                             "populated if one is")

        hdrs = self.HEADERS.copy()
        if headers:
            hdrs.update(headers)

        if params:
            params_copy = params.copy()
            # params_copy.update(extra_params)
            LOG.debug("axapi_http: params_all = %s", logutils.clean(params_copy))

            payload = json.dumps(params_copy, encoding='utf-8')
        else:
            payload = None

        LOG.debug("axapi_http: headers = %s", json.dumps(logutils.clean(hdrs), indent=4))

        if file_name is not None:
            files = {
                'file': (file_name, file_content, "application/octet-stream"),
                'json': ('blob', payload, "application/json")
            }

            hdrs.pop("Content-type", None)
            hdrs.pop("Content-Type", None)
            z = requests.request(method, self.url_base + api_url, verify=False,
                                 files=files, headers=hdrs)
        else:
            z = requests.request(method, self.url_base + api_url, verify=False,
                                 data=payload, headers=hdrs)

        if z.status_code == 204:
            return None

        try:
            r = z.json()
        except ValueError as e:
            # The response is not JSON but it still succeeded.
            if z.status_code == 200:
                return {}
            else:
                raise e

        LOG.debug("axapi_http: data = %s", json.dumps(logutils.clean(r), indent=4))

        if 'response' in r and 'status' in r['response']:
            if r['response']['status'] == 'fail':
                    acos_responses.raise_axapi_ex(r, method, api_url)

        if 'authorizationschema' in r:
            acos_responses.raise_axapi_auth_error(
                r, method, api_url, headers)

        return r
    def request(self, method, api_url, params={}, **kwargs):
        LOG.debug("axapi_http: url = %s", api_url)
        LOG.debug("axapi_http: params = %s", params)

        self.headers = self.HEADERS

        if params:
            extra_params = kwargs.get('axapi_args', {})
            params_copy = merge_dicts(params, extra_params)
            LOG.debug("axapi_http: params_all = %s", params_copy)

            payload = json.dumps(params_copy, encoding='utf-8')
        else:
            try:
                payload = kwargs.pop('payload', None)
                self.headers = dict(self.headers, **kwargs.pop('headers', {}))
                LOG.debug("axapi_http: headers_all = %s", self.headers)
            except KeyError:
                payload = None

        last_e = None

        for i in xrange(0, 600):
            try:
                last_e = None
                data = self._http(method, api_url, payload)
                break
            except socket.error as e:
                # Workaround some bogosity in the API
                if (e.errno == errno.ECONNRESET or
                   e.errno == errno.ECONNREFUSED):
                    time.sleep(0.1)
                    last_e = e
                    continue
                raise e
            except httplib.BadStatusLine as e:
                time.sleep(0.1)
                last_e = e
                continue
            except EmptyHttpResponse as e:
                if e.response.status != httplib.OK:
                    msg = dict(e.response.msg.items())
                    data = json.dumps({"response": {'status': 'fail', 'err':
                                      {'code': e.response.status,
                                       'msg': msg}}})
                else:
                    data = json.dumps({"response": {"status": "OK"}})
                break

        if last_e is not None:
            raise e

        self.response_data = data   # MOE.
        LOG.debug("axapi_http: data = %s", data)

        # Fixup some broken stuff in an earlier version of the axapi
        # xmlok = ('<?xml version="1.0" encoding="utf-8" ?>'
        #          '<response status="ok"></response>')
        # if data == xmlok:
        #     return {'response': {'status': 'OK'}}
        if data in broken_replies:
            data = broken_replies[data]
            LOG.debug("axapi_http: broken reply, new response: %s", data)

        try:
            r = json.loads(data, encoding='utf-8')
        except ValueError as e:
            # Handle non json response
            LOG.debug("axapi_http: json = %s", e)
            return data

        if 'response' in r and 'status' in r['response']:
            if r['response']['status'] == 'fail':
                    acos_responses.raise_axapi_ex(
                        r, action=extract_method(api_url))

        return r
Exemple #3
0
    def request(self, method, api_url, params={}, **kwargs):
        LOG.debug("axapi_http: url = %s", api_url)
        LOG.debug("axapi_http: params = %s", params)

        self.headers = self.HEADERS

        if params:
            extra_params = kwargs.get('axapi_args', {})
            params_copy = merge_dicts(params, extra_params)
            LOG.debug("axapi_http: params_all = %s", params_copy)

            payload = json.dumps(params_copy, encoding='utf-8')
        else:
            try:
                payload = kwargs.pop('payload', None)
                self.headers = dict(self.headers, **kwargs.pop('headers', {}))
                LOG.debug("axapi_http: headers_all = %s", self.headers)
            except KeyError:
                payload = None

        last_e = None

        for i in xrange(0, 600):
            try:
                last_e = None
                data = self._http(method, api_url, payload)
                break
            except socket.error as e:
                # Workaround some bogosity in the API
                if (e.errno == errno.ECONNRESET or
                   e.errno == errno.ECONNREFUSED):
                    time.sleep(0.1)
                    last_e = e
                    continue
                raise e
            except httplib.BadStatusLine as e:
                time.sleep(0.1)
                last_e = e
                continue
            except EmptyHttpResponse as e:
                if e.response.status != httplib.OK:
                    msg = dict(e.response.msg.items())
                    data = json.dumps({"response": {'status': 'fail', 'err':
                                      {'code': e.response.status,
                                       'msg': msg}}})
                else:
                    data = json.dumps({"response": {"status": "OK"}})
                break

        if last_e is not None:
            raise e

        LOG.debug("axapi_http: data = %s", data)

        # Fixup some broken stuff in an earlier version of the axapi
        # xmlok = ('<?xml version="1.0" encoding="utf-8" ?>'
        #          '<response status="ok"></response>')
        # if data == xmlok:
        #     return {'response': {'status': 'OK'}}
        if data in broken_replies:
            data = broken_replies[data]
            LOG.debug("axapi_http: broken reply, new response: %s", data)

        try:
            r = json.loads(data, encoding='utf-8')
        except ValueError as e:
            # Handle non json response
            LOG.debug("axapi_http: json = %s", e)
            return data

        if 'response' in r and 'status' in r['response']:
            if r['response']['status'] == 'fail':
                    acos_responses.raise_axapi_ex(
                        r, action=extract_method(api_url))

        return r
Exemple #4
0
    def request(self, method, api_url, params={}, **kwargs):
        LOG.debug("axapi_http: url = %s", api_url)
        LOG.debug("axapi_http: params = %s", params)

        self.headers = self.HEADERS

        if params:
            extra_params = kwargs.get("axapi_args", {})
            params_copy = merge_dicts(params, extra_params)
            LOG.debug("axapi_http: params_all = %s", params_copy)

            payload = json.dumps(params_copy, encoding="utf-8")
        else:
            try:
                payload = kwargs.pop("payload", None)
                self.headers = dict(self.headers, **kwargs.pop("headers", {}))
                LOG.debug("axapi_http: headers_all = %s", self.headers)
            except KeyError:
                payload = None

        last_e = None

        for i in xrange(0, 600):
            try:
                last_e = None
                data = self._http(method, api_url, payload)
                break
            except requests.exceptions.ConnectionError as e:
                # Workaround some bogosity in the API
                if len(e.args) >= 1 and hasattr(e.args[0], "errno"):
                    sock_errno = e.args[0].errno
                    if sock_errno == errno.ECONNRESET or sock_errno == errno.ECONNREFUSED:
                        time.sleep(0.1)
                        last_e = e
                        continue
                raise e
            except requests.exceptions.HTTPError as e:
                time.sleep(0.1)
                last_e = e
                continue
            except EmptyHttpResponse as e:
                if e.response.status_code != requests.codes.ok:
                    msg = dict(e.response.headers, body=e.response.text)
                    data = json.dumps({"response": {"status": "fail", "err": {"code": e.response.status, "msg": msg}}})
                else:
                    data = json.dumps({"response": {"status": "OK"}})
                break

        if last_e is not None:
            raise e

        LOG.debug("axapi_http: data = %s", data)

        # Fixup some broken stuff in an earlier version of the axapi
        # xmlok = ('<?xml version="1.0" encoding="utf-8" ?>'
        #          '<response status="ok"></response>')
        # if data == xmlok:
        #     return {'response': {'status': 'OK'}}
        if data in broken_replies:
            data = broken_replies[data]
            LOG.debug("axapi_http: broken reply, new response: %s", data)

        try:
            r = json.loads(data, encoding="utf-8")
        except ValueError as e:
            # Handle non json response
            LOG.debug("axapi_http: json = %s", e)
            return data

        if "response" in r and "status" in r["response"]:
            if r["response"]["status"] == "fail":
                acos_responses.raise_axapi_ex(r, action=extract_method(api_url))

        return r
Exemple #5
0
    def request(self,
                method,
                api_url,
                params={},
                headers=None,
                file_name=None,
                file_content=None,
                axapi_args=None,
                **kwargs):
        LOG.debug("axapi_http: full url = %s", self.url_base + api_url)
        LOG.debug("axapi_http: %s url = %s", method, api_url)
        LOG.debug("axapi_http: params = %s",
                  json.dumps(logutils.clean(params), indent=4))

        # Update params with axapi_args for currently unsupported configuration of objects
        if axapi_args is not None:
            formatted_axapi_args = dict([(k.replace('_', '-'), v)
                                         for k, v in axapi_args.iteritems()])
            params = acos_client.v21.axapi_http.merge_dicts(
                params, formatted_axapi_args)

        if (file_name is None and file_content is not None) or \
           (file_name is not None and file_content is None):
            raise ValueError("file_name and file_content must both be "
                             "populated if one is")

        hdrs = self.HEADERS.copy()
        if headers:
            hdrs.update(headers)

        if params:
            params_copy = params.copy()
            # params_copy.update(extra_params)
            LOG.debug("axapi_http: params_all = %s",
                      logutils.clean(params_copy))

            payload = json.dumps(params_copy, encoding='utf-8')
        else:
            payload = None

        LOG.debug("axapi_http: headers = %s",
                  json.dumps(logutils.clean(hdrs), indent=4))

        if file_name is not None:
            files = {
                'file': (file_name, file_content, "application/octet-stream"),
                'json': ('blob', payload, "application/json")
            }

            hdrs.pop("Content-type", None)
            hdrs.pop("Content-Type", None)

        last_e = None

        for i in xrange(0, 1500):
            try:
                last_e = None
                if file_name is not None:
                    z = requests.request(method,
                                         self.url_base + api_url,
                                         verify=False,
                                         files=files,
                                         headers=hdrs)
                else:
                    #requests.packages.urllib3.disable_warnings()
                    z = requests.request(method,
                                         self.url_base + api_url,
                                         verify=False,
                                         data=payload,
                                         headers=hdrs)

                break
            except (socket.error, requests.exceptions.ConnectionError) as e:
                # Workaround some bogosity in the API
                if e.errno in self.retry_errnos or \
                   any(s in str(e) for s in self.retry_err_strings):
                    time.sleep(0.1)
                    last_e = e
                    continue
                raise e

        LOG.debug("acos_client retried %s %s times", self.url_base + api_url,
                  i)

        if last_e is not None:
            LOG.error(
                "acos_client failing with error %s after %s retries ignoring %s",
                last_e, i, self.retry_err_strings)
            raise e

        if z.status_code == 204:
            return None

        try:
            r = z.json()
        except ValueError as e:
            # The response is not JSON but it still succeeded.
            if z.status_code == 200:
                return {}
            else:
                raise e

        LOG.debug("axapi_http: data = %s",
                  json.dumps(logutils.clean(r), indent=4))

        if 'response' in r and 'status' in r['response']:
            if r['response']['status'] == 'fail':
                acos_responses.raise_axapi_ex(r, method, api_url)

        if 'authorizationschema' in r:
            acos_responses.raise_axapi_auth_error(r, method, api_url, headers)

        return r