Python 189 webdisk client.
Project description
Python 天翼网盘客户端
0. 安装
你可以从 pypi 安装最新版本
pip install -U p189client
1. 导入模块和创建实例
导入模块
from p189client import P189Client, P189APIClient
2. 接口调用
所有需要直接或间接执行 HTTP 请求的接口,都有同步和异步的调用方式,且默认是采用 GET 发送请求数据
# 同步调用
client.method(payload)
client.method(payload, async_=False)
# 异步调用
await client.method(payload, async_=True)
从根本上讲,除了几个 staticmethod,它们都会调用 P189Client.request
url = "https://cloud.189.cn/api/someapi"
response = client.request(url=url, json={...})
当你需要构建自己的扩展模块,以增加一些新的天翼网盘的接口时,就需要用到此方法了
from collections.abc import Coroutine
from typing import overload, Any, Literal
from p189client import P189Client
class MyCustom189Client(P189Client):
@overload
def foo(
self,
payload: dict,
/,
async_: Literal[False] = False,
**request_kwargs,
) -> dict:
...
@overload
def foo(
self,
payload: dict,
/,
async_: Literal[True],
**request_kwargs,
) -> Coroutine[Any, Any, dict]:
...
def foo(
self,
payload: dict,
/,
async_: bool = False,
**request_kwargs,
) -> dict | Coroutine[Any, Any, dict]:
api = "https://cloud.189.cn/api/foo"
return self.request(
api,
method="GET",
params=payload,
async_=async_,
**request_kwargs,
)
@overload
def bar(
self,
payload: dict,
/,
async_: Literal[False] = False,
**request_kwargs,
) -> dict:
...
@overload
def bar(
self,
payload: dict,
/,
async_: Literal[True],
**request_kwargs,
) -> Coroutine[Any, Any, dict]:
...
def bar(
self,
payload: dict,
/,
async_: bool = False,
**request_kwargs,
) -> dict | Coroutine[Any, Any, dict]:
api = "https://cloud.189.cn/api/bar"
return self.request(
api,
method="POST",
data=payload,
async_=async_,
**request_kwargs,
)
3. 检查响应
接口被调用后,如果返回的是 dict 类型的数据(说明原本是 JSON),则可以用 p189client.check_response 执行检查。如果检测为正常,则原样返回数据;否则,抛出一个 p189client.P189OSError 的实例。
from p189client import check_response
# 检查同步调用
data = check_response(client.method(payload))
# 检查异步调用
data = check_response(await client.method(payload, async_=True))
4. 辅助工具
一些简单的封装工具可能是必要的,特别是那种实现起来代码量比较少,可以封装成单个函数的。我把平常使用过程中,积累的一些经验具体化为一组工具函数。这些工具函数分别有着不同的功能,如果组合起来使用,或许能解决很多问题。
from p189client import tool
5. 学习案例
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file p189client-0.0.0.1.tar.gz.
File metadata
- Download URL: p189client-0.0.0.1.tar.gz
- Upload date:
- Size: 28.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.4 Darwin/23.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef532a7f266e9d86554fd2a85b3a1e4a6d8cb7d286df25463e9f5885466006ee
|
|
| MD5 |
ee0ed73ec34c19c39fc9294907b7478a
|
|
| BLAKE2b-256 |
98dd8d57d5321a11a59646cdbc0fb723273cf43be67373d4a2c5facf68b8381e
|
File details
Details for the file p189client-0.0.0.1-py3-none-any.whl.
File metadata
- Download URL: p189client-0.0.0.1-py3-none-any.whl
- Upload date:
- Size: 30.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.4 Darwin/23.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30eccde2feea3f6ae541d0dda8983cda604c454d1efcae036986eca38bf29422
|
|
| MD5 |
5cadc38903b9c83e03caebf62f290026
|
|
| BLAKE2b-256 |
1fff918a1cfa71899214b0e7acea689d3be7488b31084404851466ed4a1131c7
|