def __init__(self, request_id=None, product=None, env=None): # requestId self.request_id = TeaConverter.to_unicode(request_id) # type: unicode # pop产品 self.product = TeaConverter.to_unicode(product) # type: unicode # 环境 self.env = TeaConverter.to_unicode(env) # type: unicode
def __init__(self, code=None, data=None, request_id=None, success=None): # code self.code = TeaConverter.to_unicode(code) # type: unicode # 产品信息 self.data = data # type: GetAllProductResponseBodyData # Id of the request self.request_id = TeaConverter.to_unicode(request_id) # type: unicode # success self.success = success # type: bool
def recognize_vehicle_parts_advance(self, request, runtime): # Step 0: init client access_key_id = self._credential.get_access_key_id() access_key_secret = self._credential.get_access_key_secret() auth_config = rpc_models.Config(access_key_id=access_key_id, access_key_secret=access_key_secret, type='access_key', endpoint='openplatform.aliyuncs.com', protocol=self._protocol, region_id=self._region_id) auth_client = OpenPlatformClient(auth_config) auth_request = open_platform_models.AuthorizeFileUploadRequest( product='objectdet', region_id=self._region_id) auth_response = open_platform_models.AuthorizeFileUploadResponse() oss_config = oss_models.Config(access_key_secret=access_key_secret, type='access_key', protocol=self._protocol, region_id=self._region_id) oss_client = None file_obj = file_form_models.FileField() oss_header = oss_models.PostObjectRequestHeader() upload_request = oss_models.PostObjectRequest() oss_runtime = ossutil_models.RuntimeOptions() OpenApiUtilClient.convert(runtime, oss_runtime) recognize_vehicle_parts_req = objectdet_20191230_models.RecognizeVehiclePartsRequest( ) OpenApiUtilClient.convert(request, recognize_vehicle_parts_req) auth_response = auth_client.authorize_file_upload_with_options( auth_request, runtime) oss_config.access_key_id = auth_response.access_key_id oss_config.endpoint = OpenApiUtilClient.get_endpoint( auth_response.endpoint, auth_response.use_accelerate, self._endpoint_type) oss_client = OSSClient(oss_config) file_obj = file_form_models.FileField( filename=auth_response.object_key, content=request.image_urlobject, content_type='') oss_header = oss_models.PostObjectRequestHeader( access_key_id=auth_response.access_key_id, policy=auth_response.encoded_policy, signature=auth_response.signature, key=auth_response.object_key, file=file_obj, success_action_status='201') upload_request = oss_models.PostObjectRequest( bucket_name=auth_response.bucket, header=oss_header) oss_client.post_object(upload_request, oss_runtime) recognize_vehicle_parts_req.image_url = 'http://%s.%s/%s' % ( TeaConverter.to_unicode(auth_response.bucket), TeaConverter.to_unicode(auth_response.endpoint), TeaConverter.to_unicode(auth_response.object_key)) recognize_vehicle_parts_resp = self.recognize_vehicle_parts_with_options( recognize_vehicle_parts_req, runtime) return recognize_vehicle_parts_resp
def get_signature(sign, secret): """ Get signature according to stringToSign, secret @param sign the signed string @param secret accesskey secret @return the signature """ hash_val = hmac.new(TeaConverter.to_str(secret), TeaConverter.to_str(sign), hashlib.sha1).digest() signature = base64.b64encode(hash_val).decode('utf-8') return signature
def __init__(self, description=None, domains=None, name_space=None, product=None, type=None): # description self.description = TeaConverter.to_unicode(description) # type: unicode # 域名 self.domains = domains # type: list[unicode] # nameSpace self.name_space = TeaConverter.to_unicode(name_space) # type: unicode # product self.product = TeaConverter.to_unicode(product) # type: unicode # type self.type = TeaConverter.to_unicode(type) # type: unicode
def __init__(self, lang=None, user_client_ip=None, instance_id=None, data_type=None): self.lang = TeaConverter.to_unicode(lang) # type: unicode self.user_client_ip = TeaConverter.to_unicode( user_client_ip) # type: unicode self.instance_id = TeaConverter.to_unicode( instance_id) # type: unicode self.data_type = TeaConverter.to_unicode(data_type) # type: unicode
def prepare_headers(headers): canon_keys = [] tmp_headers = {} for k, v in headers.items(): if v is not None: if k.lower() not in canon_keys: canon_keys.append(k.lower()) tmp_headers[k.lower()] = [TeaConverter.to_string(v).strip()] else: tmp_headers[k.lower()].append(TeaConverter.to_string(v).strip()) canon_keys.sort() return {key: ','.join(sorted(tmp_headers[key])) for key in canon_keys}
def __init__(self, status=None, error_message=None, result=None, error_code=None, job_id=None): self.status = TeaConverter.to_unicode(status) # type: unicode self.error_message = TeaConverter.to_unicode( error_message) # type: unicode self.result = TeaConverter.to_unicode(result) # type: unicode self.error_code = TeaConverter.to_unicode(error_code) # type: unicode self.job_id = TeaConverter.to_unicode(job_id) # type: unicode
def _flat_repeat_list(dic): query = {} if dic: Client._object_handler('', dic, query) l = [] q = sorted(query) for i in q: k = quote_plus(TeaConverter.to_str(i)) v = quote_plus(TeaConverter.to_str(query[i])) l.append(k + '=' + v) return '&&'.join(l)
def __init__(self, type=None, update_timestamp=None, update_time=None, version=None, download_count=None): self.type = TeaConverter.to_unicode(type) # type: unicode self.update_timestamp = update_timestamp # type: long self.update_time = TeaConverter.to_unicode( update_time) # type: unicode self.version = TeaConverter.to_unicode(version) # type: unicode self.download_count = download_count # type: long
def _get_stream_length(self): file_length = 0 for k, ff in self.files.items(): field_length = len(TC.to_bytes(ff.filename)) + len(ff.content_type) +\ len(TC.to_bytes(k)) + len(self.boundary) + 78 if isinstance(ff.content, BytesIO): file_length += len(ff.content.getvalue()) + field_length else: file_length += os.path.getsize(ff.content.name) + field_length stream_length = self.str_length + file_length + len(self.boundary) + 6 return stream_length
def __init__(self, lang=None, user_client_ip=None, instance_id=None, start_date=None, end_date=None): self.lang = TeaConverter.to_unicode(lang) # type: unicode self.user_client_ip = TeaConverter.to_unicode( user_client_ip) # type: unicode self.instance_id = TeaConverter.to_unicode( instance_id) # type: unicode self.start_date = TeaConverter.to_unicode(start_date) # type: unicode self.end_date = TeaConverter.to_unicode(end_date) # type: unicode
def assert_as_number(value): """ Assert a value, if it is a number, return it, otherwise throws @return: the number value """ if not isinstance(value, TeaConverter.number): raise ValueError('%s is not a number' % TeaConverter.to_str(value)) return value
def assert_as_bytes(value): """ Assert a value, if it is a bytes, return it, otherwise throws @return: the bytes value """ if not isinstance(value, bytes): raise ValueError('%s is not a bytes' % TeaConverter.to_str(value)) return value
def assert_as_string(value): """ Assert a value, if it is a string, return it, otherwise throws @return: the string value """ if not isinstance(value, TeaConverter.unicode): raise ValueError('%s is not a string' % TeaConverter.to_str(value)) return value
def assert_as_boolean(value): """ Assert a value, if it is a boolean, return it, otherwise throws @return: the boolean value """ if not isinstance(value, bool): raise ValueError('%s is not a bool' % TeaConverter.to_str(value)) return value
def assert_as_map(value): """ Assert a value, if it is a map, return it, otherwise throws @return: the map value """ if not isinstance(value, dict): raise ValueError('%s is not a dict' % TeaConverter.to_str(value)) return value
def _build_str_forms(self): form_str = '' str_fmt = '--%s\r\nContent-Disposition: form-data; name="%s"\r\n\r\n%s\r\n' forms_list = sorted(list(self.forms)) for key in forms_list: value = self.forms[key] form_str += str_fmt % (self.boundary, key, value) self.form_str = TC.to_bytes(form_str)
def get_canonicalized_headers(headers): canon_keys = [] tmp_headers = {} for k, v in headers.items(): if v is not None: if k.lower() not in canon_keys: canon_keys.append(k.lower()) tmp_headers[k.lower()] = [TeaConverter.to_string(v).strip()] else: tmp_headers[k.lower()].append(TeaConverter.to_string(v).strip()) canon_keys.sort() canonical_headers = '' for key in canon_keys: header_entry = ','.join(sorted(tmp_headers[key])) s = '%s:%s\n' % (key, header_entry) canonical_headers += s return canonical_headers, ';'.join(canon_keys)
def assert_as_readable(value): """ Assert a value, if it is a readable, return it, otherwise throws @return: the readable value """ if isinstance(value, TeaConverter.basestring): value = BytesIO(TeaConverter.to_bytes(value)) if not isinstance(value, READABLE): raise ValueError('The value is not a readable') return value
def get_rpcsignature(signed_params, method, secret): """ Get signature according to signedParams, method and secret @type signed_params: dict @param signed_params: params which need to be signed @type method: str @param method: http method e.g. GET @type secret: str @param secret: AccessKeySecret @return: the signature """ queries = signed_params.copy() keys = list(queries.keys()) keys.sort() canonicalized_query_string = "" for k in keys: if queries[k] is not None: canonicalized_query_string += "&" canonicalized_query_string += quote(TeaConverter.to_str(k), safe='') canonicalized_query_string += "=" canonicalized_query_string += quote(TeaConverter.to_str(queries[k]), safe='') string_to_sign = "" string_to_sign += method string_to_sign += '&' string_to_sign += quote_plus("/") string_to_sign += '&' string_to_sign += quote_plus( canonicalized_query_string[1:]) digest_maker = hmac.new(TeaConverter.to_bytes(secret + '&'), TeaConverter.to_bytes(string_to_sign), digestmod=hashlib.sha1) hash_bytes = digest_maker.digest() signed_str = TeaConverter.to_string(base64.b64encode(hash_bytes)) return signed_str
def to_form_string(val): """ Format a map to form string, like a=a%20b%20c @return: the form string """ if not val: return "" keys = sorted(list(val)) dic = [(k, TeaConverter.to_str(val[k])) for k in keys if not isinstance(val[k], READABLE)] return urlencode(dic)
def parse_json(val): """ Parse it by JSON format @return: the parsed result """ try: return json.loads(val) except ValueError: raise RuntimeError( 'Failed to parse the value as json format, Value: "%s".' % TeaConverter.to_str(val))
def _get_rpc_signature(signed_params, method, secret): queries = signed_params.copy() keys = list(queries.keys()) keys.sort() canonicalized_query_string = "" for k in keys: if queries[k] is not None: canonicalized_query_string += '&{}={}'.format( quote(k, safe=''), quote(queries[k], safe='')) string_to_sign = '{}&%2F&{}'.format( method, quote_plus(canonicalized_query_string[1:])) digest_maker = hmac.new(TeaConverter.to_str(secret + Client.SEPARATOR), TeaConverter.to_str(string_to_sign), digestmod=hashlib.sha1) hash_bytes = digest_maker.digest() signed_str = str(base64.b64encode(hash_bytes)) return signed_str
def __init__(self, status=None, expire_timestamp=None, expire_time=None, max_qps=None, create_time=None, max_qpd=None, instance_id=None, version_code=None, create_timestamp=None, product_code=None): self.status = TeaConverter.to_unicode(status) # type: unicode self.expire_timestamp = expire_timestamp # type: long self.expire_time = TeaConverter.to_unicode( expire_time) # type: unicode self.max_qps = max_qps # type: long self.create_time = TeaConverter.to_unicode( create_time) # type: unicode self.max_qpd = max_qpd # type: long self.instance_id = TeaConverter.to_unicode( instance_id) # type: unicode self.version_code = TeaConverter.to_unicode( version_code) # type: unicode self.create_timestamp = create_timestamp # type: long self.product_code = TeaConverter.to_unicode( product_code) # type: unicode
def _get_canonicalized_resource(pathname, query): if len(query) <= 0: return pathname resource = '%s?' % pathname query_list = sorted(list(query)) for key in query_list: if query[key] is not None: if query[key] == '': s = '%s&' % key else: s = '%s=%s&' % (key, TeaConverter.to_string(query[key])) resource += s return resource[:-1]
def read_as_bytes(stream): """ Read data from a readable stream, and compose it to a bytes @param stream: the readable stream @return: the bytes result """ if isinstance(stream, TeaConverter.basestring): return TeaConverter.to_bytes(stream) else: b = '' for part in Client.__read_part(stream, 1024): b += part return b
def stringify_map_value(m): """ Stringify the value of map @return: the new stringified map """ if m is None: return {} dic_result = {} for k, v in m.items(): if v is not None: v = TeaConverter.to_string(v) dic_result[k] = v return dic_result
def _object_handler(key, value, out): if value is None: return if isinstance(value, dict): dic = value for k, v in dic.items(): Client._object_handler('%s.%s' % (key, k), v, out) elif isinstance(value, (list, tuple)): lis = value for index, val in enumerate(lis): Client._object_handler('%s.%s' % (key, index + 1), val, out) else: if key.startswith('.'): key = key[1:] out[key] = TeaConverter.to_str(value)
def file_str(self, size): # handle file object form_str = b'' start_fmt = '--%s\r\nContent-Disposition: form-data; name="%s";' content_fmt = b' filename="[%s]"\r\nContent-Type: [%s]\r\n\r\n[%s]' if self.file_size_left: for key in self.files_keys[:]: if size <= 0: break file_field = self.files[key] file_content = TC.to_bytes(file_field.content.read(size)) if self.file_size_left <= size: form_str += b'[%s]\r\n'.replace(FMT, file_content, 1) self.file_size_left = 0 size -= len(file_content) self.files_keys.remove(key) else: form_str += file_content self.file_size_left -= size size -= len(file_content) else: for key in self.files_keys[:]: if size <= 0: break file_field = self.files[key] if isinstance(file_field.content, BytesIO): file_size = len(file_field.content.getvalue()) else: file_size = os.path.getsize(file_field.content.name) self.file_size_left = file_size file_content = TC.to_bytes(file_field.content.read(size)) # build form_str start = start_fmt % (self.boundary, key) content = content_fmt.replace(FMT, TC.to_bytes(file_field.filename), 1)\ .replace(FMT, TC.to_bytes(file_field.content_type), 1)\ .replace(FMT, file_content, 1) if self.file_size_left < size: form_str += b'[%s][%s]\r\n'.replace(FMT, TC.to_bytes(start), 1).replace(FMT, content, 1) self.file_size_left = 0 size -= len(file_content) self.files_keys.remove(key) else: form_str += b'[%s][%s]'.replace(FMT, TC.to_bytes(start), 1).replace(FMT, content, 1) self.file_size_left -= size size -= len(file_content) return form_str