http_request.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. # coding=utf-8
  18. from aliyunsdkcore.http import format_type
  19. from aliyunsdkcore.utils import parameter_helper as helper
  20. class HttpRequest:
  21. content_md5 = "Content-MD5"
  22. content_length = "Content-Length"
  23. content_type = "Content-Type"
  24. def __init__(self, host="", url="/", method=None, headers=None):
  25. self.__host = host
  26. self.__url = url
  27. self.__method = method
  28. self.__content_type = None
  29. self.__content = None
  30. self.__encoding = None
  31. self.__headers = headers or {}
  32. self.__body = None
  33. def get_host(self):
  34. return self.__host
  35. def set_host(self, host):
  36. self.__host = host
  37. def get_body(self):
  38. return self.__body
  39. def set_body(self, body):
  40. self.__body = body
  41. def get_url(self):
  42. return self.__url
  43. def set_url(self, url):
  44. self.__url = url
  45. def get_encoding(self):
  46. return self.__encoding
  47. def set_encoding(self, encoding):
  48. self.__encoding = encoding
  49. def get_content_type(self):
  50. return self.__content_type
  51. def set_content_type(self, content_type):
  52. self.__content_type = content_type
  53. def get_method(self):
  54. return self.__method
  55. def set_method(self, method):
  56. self.__method = method
  57. def get_content(self):
  58. return self.__content
  59. def get_header_value(self, name):
  60. return self.__headers.get(name)
  61. def put_header_parameter(self, key, value):
  62. if key is not None and value is not None:
  63. self.__headers[key] = value
  64. def remove_header_parameter(self, key):
  65. if key is not None:
  66. if key in self.__headers:
  67. self.__headers.pop(key)
  68. def set_content(self, content, encoding, format=format_type.RAW):
  69. self.__content = content
  70. if content is None:
  71. self.remove_header_parameter(self.content_md5)
  72. self.remove_header_parameter(self.content_type)
  73. self.remove_header_parameter(self.content_length)
  74. self.__content_type = None
  75. self.__encoding = None
  76. else:
  77. str_md5 = helper.md5_sum(content)
  78. content_length = len(content)
  79. self.__headers[self.content_md5] = str_md5
  80. self.__headers[self.content_length] = str(content_length)
  81. self.__headers[self.content_type] = format
  82. self.__encoding = encoding
  83. def get_headers(self):
  84. return self.__headers