Skip to content

Commit 7656101

Browse files
author
Dario Varotto
committed
Entity mapping endpoint usage and examples
1 parent 977e9d8 commit 7656101

File tree

5 files changed

+151
-1
lines changed

5 files changed

+151
-1
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,39 @@ for record in ds.request_realtime():
8888

8989
The Result object takes care of converting the various fields to the appropriate type, so `record.timestamp_utc` will be a `datetime`
9090

91+
### Entity mapping
92+
93+
The entity mapping endpoint allow you to find the RP_ENTITY_ID mapped to your universe of entities.
94+
95+
```python
96+
universe = [
97+
"RavenPack",
98+
{'ticker': 'AAPL'},
99+
'California USA',
100+
{ # Amazon, specifying various fields
101+
"client_id": "12345-A",
102+
"date": "2017-01-01",
103+
"name": "Amazon Inc.",
104+
"entity_type": "COMP",
105+
"isin": "US0231351067",
106+
"cusip": "023135106",
107+
"sedol": "B58WM62",
108+
"listing": "XNAS:AMZN"
109+
},
110+
111+
]
112+
mapping = api.get_entity_mapping(universe)
113+
114+
# in this case we match everything
115+
assert len(mapping.matched) == len(universe)
116+
assert [m.name for m in mapping.matched] == [
117+
"RavenPack International S.L.",
118+
"Apple Inc.",
119+
"California, U.S.",
120+
"Amazon.com Inc."
121+
]
122+
```
123+
91124
### Entity reference
92125

93126
The entity reference endpoint give you all the available information for an Entity given the RP_ENTITY_ID

ravenpackapi/core.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ravenpackapi import Dataset
88
from ravenpackapi.exceptions import APIException
99
from ravenpackapi.models.dataset_list import DatasetList
10+
from ravenpackapi.models.mapping import RPMappingResults
1011
from ravenpackapi.models.reference import RpEntityReference
1112
from ravenpackapi.models.results import Results
1213
from ravenpackapi.util import to_curl
@@ -42,7 +43,10 @@ def headers(self):
4243
}
4344

4445
def request(self, endpoint, data=None, params=None, method='get'):
45-
assert method in _VALID_METHODS, 'Method {used} not accepted. Please use {valid_methods}'
46+
assert method in _VALID_METHODS, \
47+
'Method {used} not accepted. Please choose one of {valid_methods}'.format(
48+
used=method, valid_methods=", ".join(_VALID_METHODS)
49+
)
4650
logger.debug("Request to %s" % endpoint)
4751
requests_call = getattr(requests, method)
4852
logger.debug("Request {method} to {endpoint}".format(method=method,
@@ -125,3 +129,14 @@ def get_entity_reference(self, rp_entity_id):
125129
)
126130
data = response.json()
127131
return RpEntityReference(rp_entity_id, data)
132+
133+
def get_entity_mapping(self, identifiers):
134+
response = self.request(
135+
endpoint="/entity-mapping",
136+
method='post',
137+
data={
138+
"identifiers": identifiers
139+
},
140+
)
141+
data = response.json()
142+
return RPMappingResults(data)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from ravenpackapi import RPApi
2+
3+
4+
class TestEntityMapping(object):
5+
api = RPApi()
6+
7+
def test_matching_entity_mapping(self):
8+
entities = [{'ticker': 'AAPL', 'name': 'Apple Inc.'},
9+
{'ticker': 'JPM'},
10+
{'listing': 'XNYS:DVN'}]
11+
api = self.api
12+
mapping = api.get_entity_mapping(entities)
13+
assert not mapping.errors
14+
assert len(mapping.matched) == len(mapping.submitted) == 3
15+
16+
# let's get the first mapped entities
17+
rp_entity_ids = [match.id for match in mapping.matched]
18+
assert rp_entity_ids == ['D8442A', '619882', '14BA06']
19+
20+
def test_mismatch_mapping(self):
21+
entities = ["unknown!"]
22+
api = self.api
23+
mapping = api.get_entity_mapping(entities)
24+
rp_entity_ids = [match.id for match in mapping.matched]
25+
assert rp_entity_ids == []

ravenpackapi/models/mapping.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class RPMappingResults(object):
2+
def __init__(self, data):
3+
self.errors = []
4+
self.matched = []
5+
self.submitted = []
6+
for mapping in data['identifiers_mapped']:
7+
match = RPMappingMatch(mapping)
8+
self.submitted.append(match)
9+
if match.errors:
10+
self.errors.append(match)
11+
else:
12+
self.matched.append(match)
13+
14+
15+
class RPMappingMatch(object):
16+
def __init__(self, data):
17+
self.request = data['request_data']
18+
self.errors = data['errors']
19+
if not self.errors:
20+
self.candidates = data['rp_entities']
21+
# let's put the best candidate data on the obj for convenience
22+
best_match = self.candidates[0]
23+
self.id = best_match['rp_entity_id']
24+
self.name = best_match['rp_entity_name']
25+
self.type = best_match['rp_entity_type']
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from ravenpackapi import RPApi
2+
3+
4+
class TestEntityMapping(object):
5+
api = RPApi()
6+
7+
def test_matching_entity_mapping(self):
8+
entities = [{'ticker': 'AAPL', 'name': 'Apple Inc.'},
9+
{'ticker': 'JPM'},
10+
{'listing': 'XNYS:DVN'}]
11+
mapping = self.api.get_entity_mapping(entities)
12+
assert not mapping.errors
13+
assert len(mapping.matched) == len(mapping.submitted) == 3
14+
15+
# let's get the first mapped entities
16+
rp_entity_ids = [match.id for match in mapping.matched]
17+
assert rp_entity_ids == ['D8442A', '619882', '14BA06']
18+
19+
def test_mismatch_mapping(self):
20+
entities = ["unknown!"]
21+
mapping = self.api.get_entity_mapping(entities)
22+
rp_entity_ids = [match.id for match in mapping.matched]
23+
assert rp_entity_ids == []
24+
25+
def test_mapping_example(self):
26+
invalid_entity_request = "Unknown entity specified"
27+
universe = [
28+
"RavenPack",
29+
{'ticker': 'AAPL'},
30+
'California USA',
31+
{ # Amazon, specifying various fields
32+
"client_id": "12345-A",
33+
"date": "2017-01-01",
34+
"name": "Amazon Inc.",
35+
"entity_type": "COMP",
36+
"isin": "US0231351067",
37+
"cusip": "023135106",
38+
"sedol": "B58WM62",
39+
"listing": "XNAS:AMZN"
40+
},
41+
invalid_entity_request
42+
]
43+
mapping = self.api.get_entity_mapping(universe)
44+
assert len(mapping.matched) == 4
45+
assert [m.name for m in mapping.matched] == [
46+
"RavenPack International S.L.",
47+
"Apple Inc.",
48+
"California, U.S.",
49+
"Amazon.com Inc."
50+
]
51+
assert len(mapping.errors) == 1
52+
assert mapping.errors[0].request == invalid_entity_request

0 commit comments

Comments
 (0)