esy是一个esi包装器,旨在简单和蟒蛇。

es的Python项目详细描述


简介

Build StatusCoverage StatusDocumentation Status

使用esi web服务有很多选择。ESY的目标是成为一个易于使用的图书馆 处理抽象出来的openapi接口的螺栓。

埃西的灵感来源于实体辉煌的Python图书馆。

安装

最新稳定版本的esy可从pypi获得:

$ pip install esy

文档

文档可在esy.readthedocs.io获得。

对于各种esi路由的文档,esy还提供了 terse list的参数和返回类型。进一步 信息可以在mainESI documentation site

用法

ESY与ESI有两个接口:

  • Bravado生成的接口,添加了一些用于处理分页的糖。
  • 通过Python巫毒产生的语义包装。

这两个接口满足不同的需求。bravado接口是围绕esi的openapi包装器,它将 对于大多数熟悉虚张声势的开发人员来说,请熟悉。另一方面,语义包装器生成 类,用于ESI中的各种实体,如字符、公司和联盟。两个接口都不是互斥的 对于许多用例来说,同时使用两者是有意义的。

无论界面如何,要使用ESY,必须首先初始化客户端:

fromesy.clientimportESIClientclient=ESIClient.get_client(user_agent='my-user-agent')

客户机可能需要一两秒钟的时间来初始化,因为可以下载和解析swagger规范。加快速度 向上,您可以在本地下载规范:

$ curl https://esi.evetech.net/latest/swagger.json -o swagger.json

然后使用本地文件初始化客户端:

importjsonfromesy.clientimportESIClientwithopen('swagger.json','r')asspec_file:spec=json.load(spec_file)client=ESIClient.get_client('my-user-agent',spec=spec)

对于生产实例,强烈建议将规范保存在Redis或其他一些缓存中。

分页

对于分页的esi路由,esy将返回一个ESIPageGenerator,它是生成 路由的本机数据类型。

使用语义包装器

fromesy.entitiesimportCharacter,Corporation,Alliance,Entity# You can initialize entities using their IDsvittoros=Character(941287462)evolution=Corporation(144749962)ccp=Alliance(434243723)# To speed up initialization, it's recommended to create and share an ESIClient among instances. If no client is # supplied, a new one will be created for each entity instance.fromesy.clientimportESIClientclient=ESIClient.get_client(user_agent='my-user-agent')vittoros=Character(941287462,_client=client)# You can also initialize instances just using the entity namesvittoros=Character.from_name('Vittoros',_client=client)evolution=Corporation.from_name('Evolution',_client=client)# or speed things up by initializing many entites at the same timeentities=Entity.from_names('Vittoros','Evolution','Northern Coalition.',_client=client)print(entities){'Northern Coalition.':<esy.entities.Allianceobjectat0x0000023967F10860>,'Vittoros':<esy.entities.Characterobjectat0x0000023967F10BE0>,'Evolution':<esy.entities.Corporationobjectat0x0000023967F10B70>}

任何初始化的实体都允许您访问各种公共信息。要访问私人服务,您需要 先提供授权令牌。

vittoros=Character.from_name('Vittoros',_client=client,_token='hunter2')# or you can set the token after initializationvittoros.set_token('hunter2')

在此之后,您可以访问所有私人服务:

forasset_pageinvittoros.get_assets():print(asset_page)forcontract_pageinvittoros.get_contracts():forcontractincontract_page:forbidinvittoros.get_contracts_contract_bids(contract_id=contract.get('contract_id')):print(bid.get('bidder_id'),bid.get('amount'),bid.get('date_bid'))

查看entities API以获得更广泛的可用服务列表。

使用Bravado接口

bravado接口可从esiclient实例获得。

fromesy.clientimportESIClientclient=ESIClient.get_client(user_agent='my-user-agent')# Get list of alliancesalliances=client.Alliance.get_alliances()# Get info on a corporationevolution=client.Corporation.get_corporations_corporation_id(corporation_id=144749962)print(evolution){'alliance_id':1727758877,'ceo_id':144509256,'creator_id':144509256,'date_founded':datetime.datetime(2003,7,30,8,33,tzinfo=tzutc()),'description':'Those who cannot adapt become victims of Evolution.','home_station_id':60013843,'member_count':316,'name':'Evolution','shares':1000,'tax_rate':0.5,'ticker':'EVOL','url':'http://www.eve-evol.com','faction_id':None}
# Get paginated asset listswag=client.Corporation.get_corporations_corporation_id_assets(corporation_id=144749962,_token='esi token')# swag is an ESIPageGenerator, implementing the generator interface# Loop through it to get the asset pagesforpageinswag:# Returns a list of assetsforassetinpage:# Asset dict print(asset.get('type_id'),asset.get('location_id'))# 22457# 16074150552

缓存

ESY本身不实现缓存,但支持通过缓存代理对象使用缓存。代理需要 实现以下接口:

classCache(object):defget(self,key:int)->object:passdefset(self,key:int,data:object,cached_until:datetime.datetime):passdef__contains__(self,item:object)->bool:pass

身份验证和开发模式

ESY可以为您处理身份验证流:

fromesy.authimportESIAuthenticatorauth=ESIAuthenticator()refresh_token,access_token=auth.verify_authorization_code('authorization code from esi','your client ID','your secret key')auth.verify_access_token(access_token){'CharacterID':941287462,'CharacterName':'Vittoros','ExpiresOn':'2018-06-11T19:01:15.182864Z','Scopes':' ','TokenType':'Character','CharacterOwnerHash':'**********'}new_access_token=auth.get_access_token(refresh_token,'your client ID','your secret key')auth.revoke_token(refresh_token,'your client ID','your secret key')auth.revoke_token(access_token,'your client ID','your secret key',token_type='access_token')

帮助开发人员在不必实现整个身份验证的情况下入门 工作流中,esy还实现了一个ad-hoc web服务器来获取刷新令牌。你可以用 直接在python提示符中进行api探索,或者在测试中使用它 生成刷新或访问令牌以测试ESI调用。

首先,在https://developers.eveonline.com/创建一个新应用程序 将回调url设置为http://localhost:8000或任何地址和端口 运行devel服务器。

importesy.devel# get_authorization_code has many parameters, but for basic usage:auth_code=esy.devel.get_authorization_code(client_id='your client ID',callback_url='your callback URL',scopes='your space-delimited scopes')# This will start the web server in the background (per-default listening on localhost:8000)# and print the login URL on stdout. After authenticating in your browser, the web server # will get redirect from the SSO with the authorization code, then return that.# For situations where you are not able to reach the network where you are running ESY, # you can also use CLI login: auth_code=esy.devel.get_authorization_code(cli_login=True,client_id='your client ID',callback_url='your callback URL',scopes='your space-delimited scopes')# This will prompt for username and password, then let you pick a character.# If you are running tests, you can also supply username, password and character_id as # keyword arguments to get_authorization_code, in addition to cli_login=True. This will# automate the entire flow. Remember to revoke your tokens afterwards and for bob's sake; # don't display your username and/or password!# After getting the authorization code, you can get the tokens:refresh_token,access_token=esy.devel.verify_authorization_code(auth_code,client_id='your client ID',secret_key='your secret key')# Character infochar_info=esy.devel.verify_access_token(access_token)# Get your swagfromesy.clientimportESIClientclient=ESIClient.get_client(user_agent='your-user-agent')assets=client.Assets.get_characters_character_id_assets(character_id=char_info.get('CharacterId'),_token=access_token)forpageinassets:print(page)

devel模式将使用环境设置中的参数(如果存在):

ParameterEnvironment settingDefault
CLIENT_IDESY_CLIENT_IDNone
SECRET_KEYESY_SECRET_KEYNone
SCOPESESY_SCOPESNone
CALLBACK_URLESY_CALLBACK_URLhttp://localhost:8000
SERVER_ADDRESSESY_SERVER_ADDRESSlocalhost
SERVER_PORTESY_SERVER_PORT8000

开发

esy使用Bravadoopenapi库解析esi swagger模式并创建 一个可用的接口。为esi创建bravado的自定义包装器的目的是使接口更加 用户友好。分页是通过为任何接受页面的路由返回生成器来自动处理的。 参数,而非分页数据则在其本机数据类型中处理。可以为每个调用设置令牌,而不是 每个客户机,允许使用头并仍然获取许多令牌的数据,而无需ned重新初始化客户机。

身份验证流使用requests-oauthlib

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

推荐PyPI第三方库


热门话题
OS X上的macos Java DTrace桥   java[]int和int[]之间有什么区别   sonarqube Java 8将两个空检查替换为可选   java按后退按钮进入另一个活动   从Java创建HTML表,检查TD是否已经在它的右边有一个TD   java将十进制128序列化为JSON   java SimpleDataFormat以不同语言返回字符串日期   java通过某个键将<Long,List<String>>映射到字符串Lambda的列表   java中为二维数组赋值时出现的问题   java通过nativeimage编译spring启动应用程序   @QueryParam+@PathParam的java日期参数   Java:startsWith()具有不同的前缀,而不仅仅是一个   java执行器服务。调用所有(超时,时间单位)与未来。获取(超时,时间单位)   java中检查空引用的乐观方法