1. 程式人生 > 其它 >Python:swaggerUI介面get/post請求

Python:swaggerUI介面get/post請求

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 import requests
 5 import json
 6 import functools
 7 
 8 
 9 __API_BASE_URL = 'http://localhost:8800/api/'
10 
11 
12 # def apiRequest(path, method):
13 #     def wrapper(func):
14 #         @functools.wraps(func)
15 #         def return_wrapper(*args, **wkargs):
16 # if(method == 'get'): 17 # return get(path).content 18 # else: 19 # return func(*args, **wkargs) 20 # return return_wrapper 21 # return wrapper 22 23 24 def get(path): 25 ''' 26 發起get請求 27 ''' 28 headers = {'Content-type
': 'application/json', 29 'Accept': 'application/json'} 30 url = __API_BASE_URL+path 31 resp = requests.get(url, headers=headers, timeout=36000) 32 if(resp.status_code != 200): 33 raise Exception(resp.status_code) 34 result = json.loads(resp.content) 35 if(result['
Code'] != 200): 36 raise Exception(result['Message']) 37 return result["Data"] 38 39 40 41 def post(path, data=None): 42 ''' 43 發起post請求 44 ''' 45 headers = {'Content-type': 'application/json', 46 'Accept': 'application/json'} 47 url = __API_BASE_URL+path 48 if(data is None): 49 resp = requests.post(url, headers=headers, timeout=36000) 50 else: 51 resp = requests.post(url, data=json.dumps( 52 data), headers=headers, timeout=36000) 53 if(resp.status_code != 200): 54 raise Exception(resp.status_code) 55 result = json.loads(resp.content) 56 if(result['Code'] != 200): 57 raise Exception(result['Message']) 58 return result["Data"]