zoukankan      html  css  js  c++  java
  • python 调用接口

    这个比较乱,抽口再修改一下。

    工作需要调有赞API的接口数据,  返回数据. 进行数据处理

    现在两部分比较重要:1 自动获取数据  ,  2处理excel的过程.

    1. 明白接口的过程.传入参数

        https://open.youzan.com/v3/apicenter/doc-api-main/1/2/salesman/youzan.salesman.trades.get 

           这个地址里面,都有 各个很细的API 过程. 例如

        导入 外部API  。例如获得分销用户的方法的API . 

      

      用户的api 文档

    from app.external.yz.api_util import YzApiUtil
    
    
    class YzUserApi(YzApiUtil):
        """
        有赞会员Api
        """
    
        def get_follower(self, fans_id=None, weixin_openid=None):
            """
            根据微信粉丝用户的 weixin_openid 或 fans_id 获取用户信息
            :param fans_id:
            :param weixin_openid:
            :return:
            """
    
            params = {}
    
            if fans_id:
                params['fans_id'] = fans_id
            elif weixin_openid:
                params['weixin_openid'] = weixin_openid
            else:
                return None
    
            data = self._invoke('youzan.users.weixin.follower.get', '3.0.0', 'GET', params=params)
    
            if 'response' in data:
                return data['response']['user']
            else:
                return None
    
        def get_customer(self, fans_id=None, mobile=None, youzan_account=None):
            """
            获取客户详情
            :param fans_id:
            :param mobile:
            :param youzan_account:
            :return:
            """
    
            params = {}
    
            if fans_id:
                params['account'] = '{"account_type":"FansID","account_id":"' + str(fans_id) + '"}'
            elif mobile:
                params['account'] = '{"account_type":"Mobile","account_id":"' + str(mobile) + '"}'
            elif youzan_account:
                params['account'] = '{"account_type":"YouZanAccount","account_id":"' + str(youzan_account) + '"}'
            else:
                return None
    
            data = self._invoke('youzan.scrm.customer.get', '3.1.0', 'GET', params=params)
    
            if 'response' in data:
                return data['response']
            else:
                return None
    
        def get_openid(self, mobile):
            """
            使用手机号获取用户openId(获取的商家自有粉丝的openId。fans_type =1时)
            :param mobile: 会员手机号
            :return:
            {
              "open_id": "oTtVis-xiMQjlBME5Xi4Bc_twuqA",
              "union_id": "oqY0-wpXFmBsPI2IrTUYx3DigfjY"
            }
            """
    
            params = {
                'mobile': mobile,
            }
    
            data = self._invoke('youzan.user.weixin.openid.get', '3.0.0', 'GET', params=params)
    
            if 'response' in data:
                return data['response']
            else:
                return None
    
        def import_user(self):
            """
            导入三方帐号信息,用于App开店帐号一键授权
            :doc: https://open.youzan.com/v3/apicenter/doc-api-main/1/2/account_import/youzan.user.platform.import
            """
    
            params = {
                'country_code': '+86',
                'mobile': '17392940060',
                'open_user_id': '17392940060',
            }
    
            data = self._invoke('youzan.user.platform.import', '1.0.0', 'GET', params=params)
    
            if 'response' in data:
                return data['response']
            else:
                return None

      2  项目中接口服务

      测试接口通的数据代码:

    from app.external.yz.user_api import YzUserApi
    from app.model.yz_user import YzUser
    from app.utils.x_logger import logger
    
    
    class YzUserTestService:
        """
        有赞用户Test Service
        """


    # 这是测试看是否通
    def __init__(self): self.yz_api_user = YzUserApi() def get_user(self, mobile): user = self.yz_api_user.get_customer(mobile=mobile) logger.info(user) return user


    #主程序执行,理论上下面的这些不要写在里面。 执行数据保存
    if __name__ == '__main__': result_user = YzUserTestService().get_user("18874189337")
      # 用户传入参数 yz_user
    = YzUser() yz_user.name = result_user['name'] yz_user.telephone = result_user['mobile'] yz_user.save() a = 1

      

     3  保存mysql 数据库表里

    在mabao51数据库里建好同样数据结构的表

    from app import db
    
    
    class YzUser(db.Model):
        # 自定义表名:
        __tablename__ = 'yz_user'
        id = db.Column(db.BigInteger, primary_key=True)
        name = db.Column(db.String(50), nullable=False, default='')
        telephone = db.Column(db.String(20), default='')
    
        def save(self):
            db.session.add(self)
            db.session.commit()
    
    
    if __name__ == '__main__':
        db.create_all()
        # test = YzUser.query.get(1)
    
        a = 1
  • 相关阅读:
    SGU 495 Kids and Prizes
    HDU 3853 LOOPS
    HDU 4089 Activation
    HDU 4405 Aeroplane chess
    ZOJ 3329 One Person Game
    POJ 2096 Collecting Bugs
    POJ1573(Robot Motion)
    poj2632(Crashing Robots)
    poj1068(Parencodings)
    poj2506(Tiling)
  • 原文地址:https://www.cnblogs.com/sakura3/p/10561389.html
Copyright © 2011-2022 走看看