Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Adding support for the eve/CharacterAffiliation endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
regner committed Apr 1, 2014
1 parent c5ab566 commit f8fa282
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
55 changes: 55 additions & 0 deletions evelink/eve.py
Expand Up @@ -139,6 +139,61 @@ def character_id_from_name(self, name):
api_result = self.character_ids_from_names([name])
return api.APIResult(api_result.result.get(name), api_result.timestamp, api_result.expires)

@api.auto_call('eve/CharacterAffiliation', map_params={'id_list': 'ids'})
def affiliations_for_characters(self, id_list, api_result=None):
"""Retrieve a dict of the designated characters.
name_list:
A list of names to retrieve IDs for.
IDs for anything not a character will be returned with a name, but nothing else.
"""

rowset = api_result.result.find('rowset')
rows = rowset.findall('row')

results = {}
for row in rows:
char_id = int(row.attrib['characterID'])
char_name = row.attrib['characterName']
corp_id = int(row.attrib['corporationID']) or None
corp_name = row.attrib['corporationName'] or None
faction_id = int(row.attrib['factionID']) or None
faction_name = row.attrib['factionName'] or None
alliance_id = int(row.attrib['allianceID']) or None
alliance_name = row.attrib['allianceName'] or None
results[char_id] = {
'id': char_id,
'name': char_name,
'corp': {
'id': corp_id,
'name': corp_name
}
}

if faction_id is not None:
results[char_id]['faction'] = {
'id': faction_id,
'name': faction_name
}

if alliance_id is not None:
results[char_id]['alliance'] = {
'id': alliance_id,
'name': alliance_name
}

return api.APIResult(results, api_result.timestamp, api_result.expires)

def affiliations_for_character(self, char_id):
"""Retrieve the affiliations of a single character
Convenience wrapper around owner_ids_from_names().
"""

api_result = self.affiliations_for_characters([char_id])
return api.APIResult(api_result.result[char_id], api_result.timestamp, api_result.expires)

@api.auto_call('eve/CharacterInfo', map_params={'char_id': 'characterID'})
def character_info_from_id(self, char_id, api_result=None):
"""Retrieve a dict of info about the designated character."""
Expand Down
72 changes: 72 additions & 0 deletions tests/test_eve.py
Expand Up @@ -95,6 +95,78 @@ def test_character_id_from_name(self):
self.assertEqual(current, 12345)
self.assertEqual(expires, 67890)

def test_affiliations_for_characters(self):
self.api.get.return_value = self.make_api_result("eve/character_affiliation.xml")

result, current, expires = self.eve.affiliations_for_characters(set([92168909, 401111892, 1979087900]))
self.assertEqual(result, {
1979087900: {
'id': 1979087900,
'name': 'Marcel Devereux',
'faction': {
'id': 500004,
'name': 'Gallente Federation'
},
'corp': {
'id': 1894214152,
'name': 'Aideron Robotics'
}
},
401111892: {
'id': 401111892,
'name': 'ShadowMaster',
'alliance': {
'id': 99000652,
'name': 'RvB - BLUE Republic'
},
'corp': {
'id': 1741770561,
'name': 'Blue Republic'
}
},
92168909: {
'id': 92168909,
'name': 'CCP FoxFour',
'alliance': {
'id': 434243723,
'name': 'C C P Alliance'
},
'corp': {
'id': 109299958,
'name': 'C C P'
}
}
})

self.assertEqual(self.api.mock_calls, [
mock.call.get('eve/CharacterAffiliation', params={'ids': set([92168909, 401111892, 1979087900])})
])
self.assertEqual(current, 12345)
self.assertEqual(expires, 67890)

def test_affiliations_for_character(self):
self.api.get.return_value = self.make_api_result("eve/character_affiliation_single.xml")

result, current, expires = self.eve.affiliations_for_character(92168909)
self.assertEqual(result, {
'id': 92168909,
'name': 'CCP FoxFour',
'alliance': {
'id': 434243723,
'name': 'C C P Alliance'
},
'corp': {
'id': 109299958,
'name': 'C C P'
}
})

self.assertEqual(self.api.mock_calls, [
mock.call.get('eve/CharacterAffiliation', params={'ids': [92168909]})
])
self.assertEqual(current, 12345)
self.assertEqual(expires, 67890)

def test_character_info_from_id(self):
self.api.get.return_value = self.make_api_result("eve/character_info.xml")

Expand Down
7 changes: 7 additions & 0 deletions tests/xml/eve/character_affiliation.xml
@@ -0,0 +1,7 @@
<result>
<rowset>
<row characterID="92168909" characterName="CCP FoxFour" corporationID="109299958" corporationName="C C P" allianceID="434243723" allianceName="C C P Alliance" factionID="0" factionName=""/>
<row characterID="401111892" characterName="ShadowMaster" corporationID="1741770561" corporationName="Blue Republic" allianceID="99000652" allianceName="RvB - BLUE Republic" factionID="0" factionName=""/>
<row characterID="1979087900" characterName="Marcel Devereux" corporationID="1894214152" corporationName="Aideron Robotics" allianceID="0" allianceName="" factionID="500004" factionName="Gallente Federation"/>
</rowset>
</result>
5 changes: 5 additions & 0 deletions tests/xml/eve/character_affiliation_single.xml
@@ -0,0 +1,5 @@
<result>
<rowset>
<row characterID="92168909" characterName="CCP FoxFour" corporationID="109299958" corporationName="C C P" allianceID="434243723" allianceName="C C P Alliance" factionID="0" factionName=""/>
</rowset>
</result>

0 comments on commit f8fa282

Please sign in to comment.