一个高度灵活和可扩展的服务集成框架,用于抓取web或使用api

bors的Python项目详细描述


https://travis-ci.org/RobotStudio/bors.svg?branch=masterDocumentation StatusUpdatesPython 3

bors

一种高度灵活、可扩展的服务集成框架 抓取网络或使用api。

用法

  1. 根据希望合并的数据创建模型。
  2. 决定要对数据做什么,然后添加它。
  3. 创建或使用现有的API集成库。
  4. 创建根应用程序以将其绑定在一起。

对象模型

我们用marshmallow 对于基础对象架构定义。下面是一个示例模型:

frommarshmallowimportSchema,fieldsclassNewsItemSchema(Schema):"""News item"""id=f.Str(required=True)url=f.Str(required=True)title=f.Str(required=True)pubDate=f.Str(required=True)timestamp=f.Str(required=True)feed_id=f.Int(required=True)published_date=f.Str(required=True)feed_name=f.Str(required=True)feed_url=f.Str(required=True)feed_enabled=f.Int(required=True)feed_description=f.Str(required=True)url_field=f.Str(required=True)title_field=f.Str(required=True)date_field=f.Str(required=True)feed_image=f.Str(required=True)

有关详细信息,请参阅marshmallowdocs。

中间件策略

中间件api以策略的形式实现,并遵循以下内容 基本布局:

"""
Simple context display strategy
"""frombors.app.strategyimportIStrategyclassPrint(IStrategy):"""Print Strategy implementation"""defbind(self,context):"""
        Bind the strategy to the middleware pipeline,
        returning the context
        """print(f"""PrintStrategy: {context}""")# just a pass-throughreturncontext

这里要注意的是:*我们继承了 IStrategy。*我们正在实现一个bind方法。*束缚 方法接收、潜在地增加,然后返回context

API集成

请求模式

因为我们的api很简单,所以我们将按原样使用它。

frombors.generics.requestimportRequestSchema

响应模式

我们的api以以下格式向我们发送数据:

{"data":...,"status":"OK"}

为此,我们需要补充一点,删除根字段和 返回data值:

frommarshmallowimportfieldsfrombors.generics.requestimportResponseSchemaclassMyAPIResponseSchema(ResponseSchema):"""Schema defining how the API will respond"""status=fields.Str()defget_result(self,data):"""Return the actual result data"""returndata.get("data","")classMeta:"""Add 'data' field"""strict=Trueadditional=("data",)

API类

frombors.api.requestorimportReqclassMyAPI(LoggerMixin):name="my_api"def__init__(self,context):self.create_logger()self.request_schema=RequestSchemaself.result_schema=MyAPIResponseSchemaself.context=contextself.req=Req("http://some.api.endpoint/v1",payload,self.log)# We don't need to deal directly with requests, so we pass them throughself.call=self.req.calldefshutdown(self):"""Perform last-minute stuff"""pass

在这里,我们使用内置的Req类向api发出请求,我们 将request_schemaresult_schema分配给 对象,然后设置namecontextcall属性。 通过API传递的结果可从 密钥my_api下的中间件上下文。

将其全部组合在一起

frombors.app.builderimportAppBuilderfrombors.app.strategyimportStrategydefmain():strat=Strategy(Print())app=AppBuilder([MyAPI],strat)app.run()if__name__=="__main__":main()

在这里,我们根据需要设置尽可能多的策略和api,然后创建和 运行app

架构

  +------------+
+-+ MIDDLEWARE +------> out
| +------------+
|                       API/WEB
| +------------+
+-+ PREPROCESS +<------ in
  +------------+

在最基本的层次上,一个borsintegrator与 集成库(API)通过前置处理器将传入数据传递到 生成并验证传入对象,然后通过 中间商。传出交互是从 中间件并直接传递给一个api,使得 除了观察和反应之外,请求/响应类型的行为。

摄取数据

      ^
      |
+-----+------+
| MIDDLEWARE |
+-----+------+
      ^
+-----+------+
| PREPROCESS |
+-----+------+
      ^
      |
      +
     API/
     WEB

摄取的数据会引发管道沿线的调用。

输出数据

     API/
     WEB
      ^
      |
+-----+------+
| MIDDLEWARE |
+------------+

制定的事件会刺激api或web操作。

预处理

预处理只不过是对传入的 数据。这提供了两个好处:1。数据可以在整个api中通用化 接口。2.可以验证和实施数据结构。

中间产品

中间件允许数据处理管道传递数据。

  +-+  +-+  +-+
  |M|  |M|  |M|
  |I|  |I|  |I|
  |D|  |D|  |D|
  |D|  |D|  |D|
->+L+->+L+->+L+->
  |E|  |E|  |E|
  |W|  |W|  |W|
  |A|  |A|  |A|
  |R|  |R|  |R|
  |E|  |E|  |E|
  +-+  +-+  +-+
这个模型在我们的行为中获得了很大的灵活性。 整合。中间件由开发人员创建,可以是任何 以下各项中的一项:

  • 数据后处理、过滤、聚合或扩充
  • 外部集成和接口
  • 从外部参与者或基于时间的刺激api/web事务 标准
  • 挂钩和回调

历史

0.3.5(2018-06-26)

  • 增加了徽章。
  • 已清理DEPS。

0.3.4(2018-06-26)

  • 删除并升级所有依赖项。

0.3.3(2018-06-26)

  • 已清除tox.ini

0.3.2(2018-06-26)

  • 设置example.py脚本供其他人使用。

0.3.1(2018-06-26)

  • 添加了几个单元测试,构建了一些框架。

0.3.0(2018-06-25)

  • 使用cookiecutter pypackage重新启动打包。

0.2.0(2018-05-17)

  • 初始化存储库,将其从nombot中分离出来。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在一个循环中循环,为什么这个数字会输出?   java Spring的注释类型需要弃用   java求矩阵中给定值的所有平方子矩阵之和   java我可以在jsp视图中使用文件夹名称吗   java Stripe PaymentMethodsActivity无法正常工作   java为什么重复调用repaint()方法不起作用   java如何解决类名冲突   swing Pacman java游戏运行非常慢,当智能幽灵移动时几乎冻结(DIjkstra算法最短路径)   java如何在firebase中增加数据读取延迟   函数中未初始化java变量   安卓如何将空格保留在字符串的结尾和/或开头?   java无法在Android 2.1上加载静态文件   带有主模板和子视图的web应用程序Java HTML视图引擎   Java asm方法错误   Java:将自定义类的2d数组传递给构造函数时,数组中的值将丢失