dms-client/util/http_util.py

159 lines
5.4 KiB
Python
Raw Normal View History

# 导入requests包
import json
import os
from typing import Dict
import requests
from requests_toolbelt import MultipartEncoder
class httpUtil(object):
def __init__(self, *, url: str, params: Dict = None, data: Dict = None, file_path: str = None, token: str = None):
self.url = url
self.params = params
self.file_path = file_path
self.data = data
self.token = token
def get_no_param(self):
"""
get请求, 无参数
:return:
"""
# 发送请求,获取响应, res即返回的响应对象
res = requests.get(url=self.url)
return res
def get(self):
"""
get请求
:return:
"""
# 发送请求,获取响应, Res即返回的响应对象
try:
res = requests.get(url=self.url, params=self.params)
return res
except:
return -1
def get_herder(self):
"""
get请求,带token
:return:
"""
# 发送请求,获取响应, Res即返回的响应对象
try:
headers = {
""
"Authorization": self.token
}
res = requests.get(url=self.url, params=self.params, headers=headers)
return res
except:
return -1
def post_no_patam(self):
"""
post请求
:return:
"""
# 多行文本, 字符串格式,也可以单行(注意外层有引号,为字符串) data = '{"name": "hanzhichao", "age": 18}'
# data支持字典或字符串
data = json.dumps(self.data)
res = requests.post(url=self.url, data=data)
return res
def post_no_patam_herder(self):
"""
post请求
:return:
"""
# 多行文本, 字符串格式,也可以单行(注意外层有引号,为字符串) data = '{"name": "hanzhichao", "age": 18}'
# data支持字典或字符串
headers = {
"Content-Type": "application/json",
"Authorization": self.token
}
res = requests.post(url=self.url, data=json.dumps(self.data), headers=headers)
return res
def post_patam_herder(self):
"""
post请求
:return:
"""
# 多行文本, 字符串格式,也可以单行(注意外层有引号,为字符串) data = '{"name": "hanzhichao", "age": 18}'
# data支持字典或字符串
headers = {
"Authorization": self.token
}
res = requests.post(url=self.url, param=self.params, headers=headers)
return res
def post(self):
"""
post请求
:return:
"""
# 多行文本, 字符串格式,也可以单行(注意外层有引号,为字符串) data = '{"name": "hanzhichao", "age": 18}'
# data支持字典或字符串
res = requests.post(url=self.url, data=json.dumps(self.data), params=self.params)
return res
def post_param(self):
"""
post请求
:return:
"""
# 多行文本, 字符串格式,也可以单行(注意外层有引号,为字符串) data = '{"name": "hanzhichao", "age": 18}'
# data支持字典或字符串
res = requests.post(url=self.url, params=self.params)
return res
def post_file(self):
"""
发送文件
"""
filepath, filename = os.path.split(self.file_path)
payload = {
'file': (filename, open(self.file_path, 'rb'))
}
m = MultipartEncoder(payload)
headers = {
"Content-Type": m.content_type,
"other-keys": "other-values"
}
res = requests.post(url=self.url, data=m, params=self.params, headers=headers)
return res
if __name__ == '__main__':
res = httpUtil(url='http://192.168.2.105:8820/api/login',
params={"userName": "client1", "password": "sxy1998"}).post_param()
print(res.json()['data'])
token_s = res.json()['data']
# res1 = httpUtil(url='http://192.168.2.105:8820/api/data-storage-task-record/get/collection-code/revision',
# params={"collectionCode": "GF4-0001", "revision": 1}, token=token_s).get_herder()
# print(res1.json())
#
# res2 = httpUtil(url='http://192.168.2.105:8820/api/remote-sensing-data/get/collection-code',
# params={"collectionCode": "GF4-0001"}, token=token_s).get_herder()
# print(res2.json())
res3 = httpUtil(url='http://192.168.2.105:8820/api/dic-remote-sensing-data/get/code',
params={"code": "GF4-0001"}, token=token_s).get_herder()
print(res3.json())
# res = httpUtil(url='http://192.168.2.105:8820/api/data-storage-task-record/add',
# data={"clientCode": "client1", "collectionCode": "GF4-0001", "storageFileList": "file_total_name",
# "storageFileSizeList": "file_total_name", "remarks": ""}, token=token_s).post_no_patam_herder()
# print(res.json())
# res = httpUtil(url='http://192.168.2.105:8820/api/data-storage-task-record/end',
# params={"taskCode": 6856963234793680896}, token=token_s).post_patam_herder()
# print(res.json())
# header = {"Authorization": token_s}
# res = requests.post(url='http://192.168.2.105:8820/api/data-storage-task-record/end',
# params={"taskCode": 6856963234793680896}, headers=header).json()
# print(res)