def request(self, method, path, data=None, headers={}, query=None, response_type=None):

        url = '{0}{1}'.format(ApiClient.__api_root, path)

        req_headers = CaseInsensitiveDict({ 'User-Agent': 'sonic-cli' })
        req_headers.update(headers)

        body = None
        if data is not None:
            if 'Content-Type' not in req_headers:
                req_headers['Content-Type'] = 'application/yang-data+json'
            body = json.dumps(data)

        try:
            r = ApiClient.__session.request(
                method,
                url,
                headers=req_headers,
                data=body,
                params=query,
                verify=False)

            return Response(r, response_type)

        except requests.RequestException as e:
            log_warning('cli_client request exception: ' + str(e))
            #TODO have more specific error message based
            msg = '%Error: Could not connect to Management REST Server'
            return ApiClient.__new_error_response(msg)
Ejemplo n.º 2
0
    def __init__(self, response):
        self.response = response
        self.status_code = response.status_code
        self.content = response.content

        try:
            if response.content is None or len(response.content) == 0:
                self.content = None
            elif has_json_content(response):
                self.content = json.loads(response.content, object_pairs_hook=OrderedDict)
        except ValueError:
            log_warning('Server returned invalid json! using raw content..')
            self.content = response.content
    def __init__(self, response, response_type=None):
        self.response = response
        self.response_type = response_type
        self.status_code = response.status_code
        self.content = response.content

        try:
            if response.content is None or len(response.content) == 0:
                self.content = None
            elif self.response_type and self.response_type.lower() == 'string':
                self.content = str(response.content).decode('string_escape')
            elif has_json_content(response):
                self.content = json.loads(response.content, object_pairs_hook=OrderedDict)
        except ValueError:
            log_warning('Server returned invalid json! using raw content..')
            self.content = response.content