访问最大API的客户端库包装。
maxclient的Python项目详细描述
最大客户机
访问最大API的客户端库包装。
身份验证
maxclient使用max api,因此需要一个有效的maxauth2用户名/令牌对 任何要求。要验证maxlient并使其随时可用,请创建maxclient实例,同时指定max server url和oauth2 server url:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')
一旦拥有了客户机实例,就可以使用用户名/令牌对来对其进行身份验证:
>>> client.setActor('user.name') >>> client.setToken('NLfIgUgBgODd4sdAgDsFgdAffFigfBf0')
如果您没有用户名的令牌,maxclient可以为您获取它,并提供最初生成令牌的密码:
>>> client.login(username='user.name', password='password')
如果未提供用户名或密码,则client.login将变为交互式,因此您可以在cli脚本中使用它:
client.login() >>> Username: user.name >>> Password: *********
用法
客户端有两个版本,一个是类似rpc的,另一个是rest-ish的。默认值(rpc)为max上可用的每个api端点实现一个方法,例如:
>>> client.addUser('user.name') >>> client.getUser('user.name')
rpc client以元组形式返回具有3个值的响应,格式为:
(True, 201, {})
其中第一个值是请求是否成功完成,第二个值是请求的响应代码,第三个值是响应内容。响应内容可以是从json加载的dict或list,如果找不到有效的json响应,则无响应内容
注意,并不是所有的端点都可以在rpc客户机中实现,因为每个端点都需要不同的方法。有关每个方法的参数,请参见client.py中的代码
rest ish客户端
rest-ish客户端试图为max api创建一个通用的包装器,这样您就可以使用一个唯一的客户端轻松地访问所有可用的端点,而无需不断地更新它。身份验证就像在rpc客户机中一样,您只需从其他位置导入即可:
>>> from maxclient.rest import MaxClient
要使用这个客户机,您必须知道max api是如何工作的,以及它们是如何构造的,因为对api的访问是通过模仿url访问来实现的,例如。要访问端点以添加用户(如max文档中所述),必须向 /people/{username} 发出 post 请求。要使用maxclient进行此操作:
>>> maxclient.people['user.name'].post()
其中, people 是一个资源集合(端点url上的固定名称),'user.name'是一个资源项(端点url上的变量名)。因此,资源集合作为属性访问,资源项作为dict类访问器访问。资源项和资源集合可以相互包含:
>>> maxclient.people['user.name'].activities.get()
命令的最后一部分指示将用于访问端点的方法。资源项和集合对象是惰性的,因此任何操作都将执行,直到在资源上执行方法为止。
当执行 .post() 和 .put() 请求时,传递给客户端的命名参数将作为json传递。所以如果我们执行:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')0
将从kwargs生成一个json并发送到请求正文中:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')1
如果需要,可以使用json中需要的所有参数准备一个dict,并通过 data 参数传递它,结果将是相同的。如果存在数据参数,则忽略所有其他kwarg:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')2
一些端点方法定义了一些合理的默认值。您可以查看defaults.py中定义的默认值,或按代码检查它们:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')3
此默认值用作使用提供的Kwargs更新的基础。这样,当我们提出此请求时:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')4
将在请求正文中发送的数据将是默认值和Kwargs的组合:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')5
可以在form key_子键中传递kwargs,并将其解释为嵌套键。所以你可以做如下事情:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')6
从而产生以下请求正文:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')7
如果您的请求需要查询字符串参数,则必须通过作为dict的 qs 参数和键值对将被urlencoded到querystring中,例如,用 限制请求的结果。极限01
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')8
最后一件事,如果你需要上传一个文件,用下面的文件上传参数给文件对象或者流对象。提供原始打开的文件,不读取它,我们需要的对象不是文件的内容:
>>> from maxclient import MaxClient >>> client = MaxClient('http://max.server.com', oauth_server='http://oauth.server.com')9
当请求成功时,maxclient将以max解析的json响应响应,在任何其他情况下都将引发request error异常,该消息将指出错误的原因。
有关Max的详细信息,请参见:
https://github.com/upcnet/max https://github.com/upcnet/maxserver.devel