Echo流服务器API客户端
python-echo-streamserver的Python项目详细描述
这是echo streamserver api的python版本。请参阅echoDevelopers文档。
功能接口
函数接口提供每个api作为一个简单的module或package包。api使用default帐户联系echo。例如,items api是echo.items并具有rest api方法。
>>> # Items API: Count EQL Query >>> from echo import items, StreamServerError >>> try: >>> n = items.count("scope:http//example.com/\*") >>> print "EQL Count: %d" % n >>> except StreamServerError, e: >>> print "Error: %s" % str(e)
默认帐户
默认帐户是echo.settings模块的一部分。在那里设置echoappkey和secretapi\u键。
- ::
- /usr/lib/python2.x/site packages/echo/settings.py
客户端接口
client接口将每个api作为类instance提供。client使用一个account对象来联系echo,或默认值。例如,key-value store api是echo.kvs.client并具有kvs api方法。
>>> # KVS Client API: Get a value for the key 'sample'. >>> from echo import kvs, StreamServerError >>> # Create a KVS client using the default account. >>> client = kvs.Client() >>> try: >>> v = client.get('sample') >>> print "KVS: %r" % v >>> except StreamServerError, e: >>> print "Error: %s" % str(e)
帐户对象
每个clientinstance都可以使用account对象来联系echo。这是支持multipleecho帐户所必需的。
>>> from echo import feeds, Account >>> # Non-default account: Login Account.BasicAuth with no secret >>> other_account = Account('test.echoenabled.com', '', Account.BasicAuth) >>> client = feeds.Client(account=other_account)
mux请求
items api支持mux或multiplexed请求。几个count和search请求可以组合成一个rest调用。items.mux方法将muxrequest对象的列表发送到echo。有关输出格式,请参阅echomux方法文档。
>>> from echo import items >>> from echo.items.mux_api import MuxRequest >>> # EQL Query String >>> query_eql = "scope:http://www.example.com/\*" >>> # Search Query (default) >>> search = MuxRequest(query_eql) >>> # Count Query >>> count = MuxRequest(query_eql, api_method='count') >>> # Form list of them for Mux API. >>> requests = [ search, count ] >>> # Send Mux API requests. >>> r = items.mux(requests)
回声查询语言生成器
有一个object-orientedecho查询语言api来构建查询字符串。可以将echo.eql.query对象传递给items api方法eql.items.count和eql.items.search而不是查询文本。添加echo.eql.filters以基于查询项生成完整的EQL查询字符串。
eql语法限制
此eql生成器不能保证整个eql文本有效。每个term单独有效,但echo streamserver仍可能拒绝eql字符串。eql语法规则限制如何构造query及其filters。打印echo.eql.query对象以检查其查询字符串,并根据需要检查reorder筛选条件。
查询方法api
使用方法调用构建一个echo.eql.query对象。添加echo.eql.filters.queryfilter对象以限制结果。大多数queryfilter对象可以negated来排除该术语。
>>> from echo import eql >>> q = eql.Query("http://site.example.com/index.html", uri_filter='url') >>> q.add_filter(eql.filters.ChildrenDepth(3)) >>> q.add_filter(eql.filters.TypeFilter('article'), negate=True) >>> print "EQL> ", q EQL> "url:"http://site.example.com/index.html" children:3 -type:article"
查询运算符api
使用布尔运算符将filters添加到echo.eql.query对象。有关详细信息,请阅读echo.eql.filters文档。
>>> from echo import eql >>> q = eql.Query("http://www.example.com/movies//") >>> # Exclude articles and notes with (-). >>> q = q - eql.filters.TypeFilter(['article', 'note']) >>> # Allow children up to depth 2. >>> q + (eql.filters.ChildrenDepth(2))
echo.eql.Query Operators | ||
---|---|---|
operator | example | description |
plus + | q + r | Add filter r to query q. |
minus - | q - r | Negate filter r on query q. |
and & | q1 & q2 | Combine queries q1 and q2. |
pipe | | q1 | q2 | Select query q1 or q2. |
echo.eql.filters Operators | ||
---|---|---|
operator | example | description |
minus - | -r | Negate filter r. |
and & | r1 & r2 | Combine filters r1 and r2. |
pipe | | r1 | r2 | Apply filter r1 or r2. |