Skip to main content

为您的 HTTP API 开发客户端并记录其怪癖和特性

项目描述

Cosmicray 是一个简单的高级 http 客户端 API 开发框架。它提供了用于定义端点、处理请求响应和自动将结果转换为模型的基本构建块。

动机:

  • 使用方便

  • 每个级别的可配置性和自定义

  • 命名空间不同的后端(一个客户端来统治它们)

  • 将路由定义/响应处理与模型或“业务逻辑”分开

  • 能够在发出请求之前验证请求

  • 根据需要验证每个请求

  • 能够将路线与模型相关联

安装

$ pip install cosmicray

快速开始

创建应用

>>> from cosmicray import Cosmicray
>>> api = Cosmicray('myapp', domain='http://mydomain.com')

定义路由和响应处理程序

使用我们创建的应用程序,我们现在可以为其添加路由并为每个路由定义一个响应处理程序。响应处理程序只是一个常规函数,它接受类型为requests.Response的单个参数并返回处理后的结果。

>>> @api.route('/v1/dogs/{id}', ['GET', 'POST', 'PUT', 'DELETE'])
>>> def dogs(response):
...     return response.json()
  • 装饰器api.route创建了一个名为dogs的cosmicray.Route实例,并将 给定的函数在内部存储为响应处理程序。

  • cosmicray.Route的实例是可调用的并接受参数:

    • model_cls:可选:实现_make(cls, response)类方法的类。

    • **kwargs:关键字参数。

      • urlargs : url 格式化参数的映射

      • headers : 标题的映射

      • params : 查询参数的映射

      • 数据json文件:请求正文

      • 验证器:验证器回调

      • &rest : 请求关键字参数

  • 当调用cosmicray.Route的实例时,它会返回一个Request对象,您可以:

    • 使用为每个 http 方法定义的函数(例如:get()post()put()delete()

    • 使用 setter覆盖传入的任何参数(例如:paramsheaders等)

    • 根据路线上定义的参数自动验证给定参数

    • 如果应用程序配置了身份验证器,则对请求进行身份验证

    • 在响应处理程序处理响应后,结果会自动映射到模型类(如果提供了)

如何提出请求

>>> dogs().get()
>>> dogs(urlargs={id: 12345}).get()
>>> dogs(json={'name': 'Manu'}).post()
>>> dogs(urlargs={'id': 12345}, json={'age': 4}).put()
>>> dogs(urlargs={'id': 12345}).delete()

指定请求参数

>>> dogs(params={'breed': 'husky'},
...      headers={'Content-Type': 'application/json'}).get()

验证请求

通常您需要对访问私有资源的请求进行身份验证,而 Cosmicray 具有执行此步骤的内置机制。

>>> def authenticator(request):
...     if not request.is_request_for(login):
...         auth = login(json={'username': 'me', 'password': 'mysecret'}).post()
...         return request.set_headers({'X-AUTH-TOKEN': auth['token']})
...     return request
...
>>> @api.route('/oauth', ['POST'])
... def login(response):
...     """Get an auth token for the given credentials"""
...     return response.json()
...
>>> @api.route('/private/resource', ['GET'])
... def private_resource(response):
...     """Must be authenticated to access this"""
...     return response.json()
...
>>> api.configure(authenticator=authenticator)
>>> # Now the private resourse will be automatically updated to include auth headers
>>> private_resource.get()

楷模

基本

  • Cosmicray 附带一个内置的模型类

  • 这个基类绑定到一个特定的路由处理程序,并定义了所有将映射到响应或作为postput请求的有效负载的字段

  • 它自动使用其定义的字段作为 url 参数和请求正文

  • 提供进行 http 调用的函数(例如:getcreateupdatedelete

  • 您可以覆盖默认行为,例如创建/更新paylods

>>> from cosmicray.model import Model
>>> class Dog(Model):
...     __route__ = dogs
...     __slots__ = [
...         'id',
...         'name',
...         'breed',
...         'age'
...     ]
>>> manu = Dog(name='Manu', age=4).create()
>>> manu.breed = 'Husky'
>>> manu.update()
>>> manu.delete()
>>> manu = Dog(id=12345).get()
>>> alldogs = Dog().get()

与其他模型/路线的关系

>>> from cosmicray.model import relationhip, Model, ModelParam
>>> class Cat(cosmicray.model.Model):
...     __route__ = cats
...     __slots__ = [
...         'id',
...         'name',
...         'age'
...     ]
...     friends = relationhip('Friend', urlargs={'id': ModelParam('id')})

如果你不想使用cosmicray.Model作为你的基础,你可以定义你自己的或者甚至使用只使用collections.namedtuple作为模型。

>>> class MyModel(object):
...     @classmethod
...     def _make(cls, response):
...         obj = cls()
...         ... do stuff with the response
...         return obj

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

cosmicray-0.0.13.tar.gz (16.1 kB 查看哈希

已上传 source

内置分布

cosmicray-0.0.13-py2.py3-none-any.whl (18.7 kB 查看哈希

已上传 py2 py3