首页 > 编程语言 > 详细

Python 基于request库的get,post,delete,封装

时间:2019-03-16 01:00:52      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:url   position   cookies   val   odin   ()   tty   类型   args   

# coding=utf-8
import json
import requests
 
class TestApi(object):
    """
    /*
        @param: @session ,@cookies
        the request can be divided into session request and cookie request according to user‘s own choice
        however,url and header is must ,other parameters are given by user to make it is None or not
    */
    """
    def get(self,url,param,header,cookie=None,session=None,**kwargs):
        if session:
                return session.request("GET",url,param,headers=header,**kwargs)
        elif cookie:
                return requests.get(url,params=param,headers=header,cookies=cookie,**kwargs)
    """
    /*
    @param: @session ,@cookies
        传入的是dict类型 python object 对象
        header is form data: application/x-www-urlencoded
            transfer data to data directly ,finally requests‘s submit will be like ‘aa=dd&bb=ff‘ formation
        header is json :application/json
            due to the data can be ‘str‘,‘dict‘and tuple and so on  ,so when we choose data and
            data is given by dict,we must transfer it to json str,but when is json type str ,we must must
            transfer python object dict to json str with json.dumps(),
            finally the request submit data format is str like:
            ‘aa=dd&bb=ff‘,but when choose json the submit will become like {‘a‘: ‘cc‘ ,‘b‘: ‘dd‘} ,
            data and json cant not be used in the requests at the same time
    */
    """
    def post_data(self,url,type,data,header,cookie=None,session=None,**kwargs):
            if cookie:
                if type is "data":
                    return requests.post(url,data=data,headers=header,cookies=cookie,**kwargs)
                elif type is "json":
                    return requests.post(url,data=json.dumps(data),headers=header,cookies=cookie,**kwargs)
            elif session:
                if type is "data":
                    return session.request("POST",url,data=data,headers=header,cookies=cookie,**kwargs)
                elif type is "json":
                    return session.request("POST",url,data=json.dumps(data),headers=header,cookies=cookie,**kwargs)
    """
    /*
    @:param:@json object
    json的value为传入的json对象
    请求header默认:ContentType: application/json
    */
 
    """
    def post_json(self,url,header,json,cookie=None,session=None,**kwargs):
        if cookie:
            return requests.post(url,headers=header,json=json,cookies=cookie,**kwargs)
        elif session:
            return session.request("POST",url,headers=header,json=json,**kwargs)
 
    """
    /*
    @:param: @url,@data,@**kwargs
    Tip: header you need to according to your api to be given in **kwargs position
    */
    """
    def put(self,url,data,cookie=None,session=None,**kwargs):
        if cookie:
            return requests.put(url,data,cookies=cookie,**kwargs)
        elif session:
            return session.request("PUT",url,data,**kwargs)
    """
    /*
    @:param: @url,@data,@**kwargs
    Tip: header you need to according to your api to given in **kwargs position
    */
    """
    def delete(self,url,data,cookie=None,session=None,**kwargs):
        if cookie:
            return requests.delete(url,data,cookies=cookie,**kwargs)
        elif session:
            return session.request("DELETE",url,data,**kwargs)

 

Python 基于request库的get,post,delete,封装

标签:url   position   cookies   val   odin   ()   tty   类型   args   

原文:https://www.cnblogs.com/SunshineKimi/p/10540218.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号