Skip to main content

Cainiao Waybill SDK.

Project description

Cainiao

适用于菜鸟电子面单服务的非官方Python SDK。

Install

pip install cainiao

WayBill

  1. 第一种使用方法

    import asyncio
    
    from cainiao import WayBillClient, WayBill
    from cainiao.templates import WayBillGetForm, Sender, PackageInfo, Recipient
    
    
    sender = Sender(**{
        "cpCode": "YTO",
        "name": "XXX",
        "province": "XX省",
        "city": "XX市",
        "district": "XX区",
        "detail": "XX街道",
        "mobile": "1XXXXXXXXXX",
    })
    
    recipient = Recipient(**{
        "name": "XXX",
        "province": "XX省",
        "city": "XX市",
        "district": "XX区",
        "detail": "XX街道",
        "mobile": "1XXXXXXXXXX",
        "tradeOrderList": [str(item) for item in range(10)],
        "orderChannelsType": "OTHERS",
        "templateUrl": ("http://cloudprint.cainiao.com"
                        "/template/standard/111/12003"),
        "packageInfo": PackageInfo(),
        "userId": "520",
    })
    
    waybill_get_form = WayBillGetForm(sender=sender, recipients=[recipient])
    
    
    async def main():
        async with WayBillClient() as session:
            waybill = WayBill(
                url='http://link.cainiao.com/gateway/link.do',
                token='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                appsecret='yyyyyyyyyyyyyyyyyyyyy',
                session=session
            )
            print(await waybill.tms_waybill_get(data=waybill_get_form))
            print(await waybill.subscription_query())
            print(await waybill.cloudprint_standard_templates())
            print(await waybill.cloudprint_isv_templates())
            print(await waybill.cloudprint_seller_custom_areas())
            print(await waybill.cloudpirnt_seller_custom_area_detail(mappingId=1))
    
    asyncio.run(main())
    
  2. 第二种使用方法

    # -- snip --
    
    async def main():
        session = await WayBillClient().connection()
        waybill = WayBill(
            url='http://link.cainiao.com/gateway/link.do',
            token='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            appsecret='yyyyyyyyyyyyyyyyyyyyy',
            session=session
        )
        print(await waybill.tms_waybill_get(data=waybill_get_form))
        print(await waybill.subscription_query())
        print(await waybill.cloudprint_standard_templates())
        print(await waybill.cloudprint_isv_templates())
        print(await waybill.cloudprint_seller_custom_areas())
        print(await waybill.cloudpirnt_seller_custom_area_detail(mappingId=1))
        await session.close()
    
    asyncio.run(main())
    

CloudPrint

  1. 第一种使用方法

    import asyncio
    
    from cainiao import CloudPrintClient, CloudPrint
    from cainiao.templates import Content, Document, TaskForm
    
    content = Content(**{
        "data": {"nick": "Hello"},
        "templateURL": "http://cloudprint.cainiao.com/template/standard/278250/1",
    })
    
    document = Document(**{
        "documentID": "1",
        "contents": [content for _ in range(5)]
    })
    
    task = TaskForm(printer="Microsoft Print to PDF", documents=[document])
    
    
    async def main():
        async with CloudPrintClient(ws_url='ws://127.0.0.1:13528') as ws:
            cloudprint = CloudPrint(ws=ws)
            print(await cloudprint.get_printers())
            print(await cloudprint.print(task=task.content))
    
    
    asyncio.run(main())
    
  2. 第二种使用方法

    # -- snip --
    
    async def main():
        # 注意使用区别,session.close() 才是关闭 aiohttp.ClientSession()
        session, ws = await CloudPrintClient(ws_url='ws://127.0.0.1:13528').connect()
        cloudprint = CloudPrint(ws=ws)
        print(await cloudprint.get_printers())
        print(await cloudprint.print(task=task.content))
        await session.close()
    
    
    asyncio.run(main())
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cainiao-0.2.0.tar.gz (8.2 kB view details)

Uploaded Source

File details

Details for the file cainiao-0.2.0.tar.gz.

File metadata

  • Download URL: cainiao-0.2.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.0

File hashes

Hashes for cainiao-0.2.0.tar.gz
Algorithm Hash digest
SHA256 94c10f57890ee623f84d2f379c480a8f004e7cce9c191a46e5d5cb53e692eceb
MD5 f3edc73018abddff962922af3c241feb
BLAKE2b-256 d18465a164080f74c37a5e587e14b7245c614f0c87e169ef8e8c6458383d3664

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page