def process_request(self, request, http_client): now_time = datetime.datetime.now() if self._token is None or self._last_token_date is None or ( now_time - self._last_token_date).days * 24 * 3600 + ( now_time - self._last_token_date).seconds > 12 * 60 * 60: authorization = "Basic " + str( base64.b64encode( (self._user_name + ':' + self._user_password).encode('utf-8')), 'utf-8') body = {'account': self._user_name, 'clientType': 0} sdk_request = SdkRequest('POST', 'https', request.host, [], '/v1/usg/acs/auth/account', [], { 'Authorization': authorization, 'Content-Type': 'application/json' }, json.dumps(body), []) response = http_client.do_request_sync(sdk_request) content = json.loads(response.content.decode()) self._token = content['accessToken'] self._last_token_date = datetime.datetime.now() request.header_params["X-Auth-Token"] = self._token canonical_query_string = process_canonical_query_string(request) request.uri = request.resource_path + "?" + canonical_query_string if canonical_query_string != "" else request.resource_path return request else: request.header_params["X-Auth-Token"] = self._token canonical_query_string = process_canonical_query_string(request) request.uri = request.resource_path + "?" + canonical_query_string if canonical_query_string != "" else request.resource_path return request
def do_http_request(self, method, resource_path, path_params=None, query_params=None, header_params=None, body=None, post_params=None, response_type=None, collection_formats=None, request_type=None, async_request=False): url_parse_result = urlparse(self._endpoint) schema = url_parse_result.scheme host = url_parse_result.netloc header_params = self._parse_header_params(collection_formats, header_params) resource_path = self._parse_path_params(collection_formats, path_params, resource_path, self._credentials.get_update_path_params()) query_params = self._parse_query_params(collection_formats, query_params) post_params = self._parse_post_params(collection_formats, post_params) body = self._parse_body(body, post_params) stream = self._is_stream(response_type) sdk_request = SdkRequest(method=method, schema=schema, host=host, resource_path=resource_path, query_params=query_params, header_params=header_params, body=body, stream=stream) future_request = self._credentials.process_auth_request(sdk_request, self._http_client) if async_request: executor = ThreadPoolExecutor(max_workers=8) future_response = executor.submit(self._do_http_request_async, future_request, response_type) return FutureSdkResponse(future_response, self._logger) else: request = future_request.result() response = self._do_http_request_sync(request) return self.sync_response_handler(response, response_type)
def sdk_request(): sdk_request = SdkRequest(method="GET", schema="https", host="service.endpoint.myhuaweicloud.com", resource_path="/resources", query_params=[("size", "1")], header_params={"X-Sdk-Date": "20200608T023900Z"}, body="") yield sdk_request
def get_keystone_list_auth_domains_request(iam_endpoint=None): url_parse_result = urlparse(iam_endpoint) schema = url_parse_result.scheme host = url_parse_result.netloc resource_path = KEYSTONE_LIST_AUTH_DOMAINS_URI sdk_request = SdkRequest(method="GET", schema=schema, host=host, resource_path=resource_path, header_params={}, query_params=[], body="") return sdk_request
def get_keystone_list_projects_request(iam_endpoint=None, region_id=None): url_parse_result = urlparse(iam_endpoint) schema = url_parse_result.scheme host = url_parse_result.netloc resource_path = KEYSTONE_LIST_PROJECT_URI query_params = [('name', region_id)] sdk_request = SdkRequest(method="GET", schema=schema, host=host, resource_path=resource_path, header_params={}, query_params=query_params, body="") return sdk_request