Skip to content

Commit b4c04d0

Browse files
committed
Create python library for NameAPI Services
1 parent a92614f commit b4c04d0

25 files changed

+5008
-0
lines changed

.gitignore

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
.idea
4+
# Distribution / packaging
5+
.Python
6+
build/
7+
develop-eggs/
8+
dist/
9+
lib/
10+
lib64/
11+
parts/
12+
sdist/
13+
var/
14+
wheels/
15+
share/python-wheels/
16+
*.egg-info/
17+
.installed.cfg
18+
*.egg
19+
MANIFEST
20+
21+
# PyInstaller
22+
# Usually these files are written by a python script from a template
23+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
24+
*.manifest
25+
*.spec
26+
27+
28+
# Unit test / coverage reports
29+
.coverage
30+
.coverage.*
31+
.cache
32+
nosetests.xml
33+
coverage.xml
34+
.hypothesis/
35+
.pytest_cache/
36+
# Translations
37+
*.mo
38+
*.pot
39+
40+
# Django stuff:
41+
*.log
42+
local_settings.py
43+
db.sqlite3
44+
db.sqlite3-journal
45+
46+
# Sphinx documentation
47+
docs/_build/
48+
49+
# PyBuilder
50+
.pybuilder/
51+
target/
52+
53+
# Jupyter Notebook
54+
.ipynb_checkpoints
55+
56+
# IPython
57+
profile_default/
58+
ipython_config.py
59+
60+
# pdm
61+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
62+
#pdm.lock
63+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
64+
# in version control.
65+
# https://pdm.fming.dev/#use-with-ide
66+
.pdm.toml
67+
68+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
69+
__pypackages__/
70+
71+
.env
72+
.venv
73+
env/
74+
venv/
75+
env.bak/
76+
venv.bak/
77+
78+
79+
DevOps

README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
nameapi-client-python
2+
===================
3+
4+
Python Client for the NameAPI Web Services at https://www.nameapi.org.
5+
6+
There are functional tests that demonstrate how to use this library.
7+
8+
All you need to send requests is your own api key which you can get from nameapi.org.
9+
10+
This library requires at least Python 3.8.
11+
12+
13+
14+
## Library setup
15+
16+
Create virtual environment and activate it:
17+
18+
python -m venv myenv
19+
myenv\Scripts\activate
20+
21+
Install the required libraries:
22+
23+
pip install -r requirements.txt
24+
25+
26+
27+
## Send a ping
28+
29+
This code sends a simple ping to nameapi to test the connection:
30+
31+
32+
```python
33+
client = NameApiClient(None)
34+
response = client.ping() # return the string 'pong'
35+
```
36+
37+
## Input / Output
38+
39+
40+
#### Person input object
41+
42+
Most services accept a 'Person' as input. This person contains a name, and optionally
43+
more data such as gender, birth date etc.
44+
The name can be just a single "full name" string, or it can be composed of multiple
45+
fields like given name, middle name, surname.
46+
This standardized api makes it simple to use different services in a consistent way,
47+
and is very convenient in accepting the data however you have it at hands.
48+
49+
Creating a simple person looks something like this:
50+
51+
```python
52+
name_object = WesternInputPersonNameBuilder().fullname("Petra Müller").build()
53+
input_person = NaturalInputPersonBuilder().name(name_object).build()
54+
```
55+
56+
Creating name objects:
57+
58+
```python
59+
name_object_1 = WesternInputPersonNameBuilder() \
60+
.fullname("Petra Müller") \
61+
.build()
62+
63+
name_object_2 = WesternInputPersonNameBuilder() \
64+
.given_name("Petra") \
65+
.surname("Müller") \
66+
.build()
67+
68+
name_object_3 = WesternInputPersonNameBuilder() \
69+
.name_field(NameField("Petra", CommonNameFieldType.GIVENNAME)) \
70+
.name_field(NameField("Müller", CommonNameFieldType.SURNAME)) \
71+
.name_field(NameField("Alexa", AmericanNameFieldType.MIDDLENAME)) \
72+
.build()
73+
74+
name_object_4 = InputPersonName([NameField("petra müller", CommonNameFieldType.FULLNAME)])
75+
```
76+
77+
Creating complex input person objects:
78+
79+
```python
80+
input_person = NaturalInputPersonBuilder() \
81+
.name(name_object_1) \
82+
.gender(StoragePersonGender.MALE) \
83+
.add_email("[email protected]") \
84+
.add_tel_number("+5555555") \
85+
.age(BirthDate(year=1999, month=2, day=1))\
86+
.build()
87+
```
88+
89+
#### Result objects
90+
91+
Response from the requests, displayed as json: https://api.nameapi.org/rest/swagger-ui/
92+
93+
Access the link above to see all the return type and their json format.
94+
95+
96+
## Commands
97+
98+
The web service methods are implemented as commands. This brings the advantage that the
99+
command can be passed around and wrapped with other useful goodies such as logging
100+
in a unified way, without the need to put a wrapper around every service.
101+
For more specialized concerns such as auto-retry on failure this concept becomes
102+
a real advantage.
103+
104+
105+
106+
## Name Parser
107+
108+
Name parsing is the process of splitting a full name into its components.
109+
110+
111+
```python
112+
api_key = None # Set your api_key here
113+
client = NameApiClient(api_key)
114+
response = client.person_name_parser(input_person_1)
115+
```
116+
117+
118+
## Name Genderizer
119+
120+
Name genderizing is the process of identifying the gender based on a person's name.
121+
122+
123+
```python
124+
api_key = None # Set your api_key here
125+
client = NameApiClient(api_key)
126+
response = client.person_genderizer(input_person_1)
127+
```
128+
129+
130+
## Name Matcher
131+
132+
The Name Matcher compares names and name pairs to discover whether the people could possibly be one and the same person.
133+
134+
This service takes 2 people as input:
135+
136+
```python
137+
api_key = None # Set your api_key here
138+
client = NameApiClient(api_key)
139+
response = client.person_matcher(input_person_1, input_person_2)
140+
```
141+
142+
143+
## Name Formatter
144+
145+
The Name Formatter displays personal names in the desired form. This includes the order as well as upper and lower case writing.
146+
147+
```python
148+
api_key = None # Set your api_key here
149+
client = NameApiClient(api_key)
150+
response = client.person_name_formatter(input_person_1)
151+
```
152+
153+
## Risk Detector
154+
155+
Detects various types of possibly fake data in person records.
156+
157+
```python
158+
api_key = None # Set your api_key here
159+
client = NameApiClient(api_key)
160+
response = client.risk_detector(input_person_1)
161+
```
162+
163+
164+
165+
## Email Name Parser
166+
167+
The Email Name Parser extracts names out of email addresses.
168+
169+
```python
170+
api_key = None # Set your api_key here
171+
client = NameApiClient(api_key)
172+
email_address="[email protected]"
173+
response = client.email_name_parser(email_address)
174+
```
175+
176+
177+
## Disposable Email Address Detector
178+
179+
The DEA-Detector checks email addresses against a list of known "trash domains" such as mailinator.com.
180+
181+
```python
182+
api_key = None # Set your api_key here
183+
client = NameApiClient(api_key)
184+
email_address="[email protected]"
185+
response = client.disposable_email_detector(email_address)
186+
```
187+

nameapi_client/__init__.py

Whitespace-only changes.

nameapi_client/client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from nameapi_client.command import DisposableEmailDetectorCommand, EmailNameParserCommand, \
2+
PersonNameFormatterCommand, PersonGenderizerCommand, PersonMatcherCommand, PersonNameParserCommand, \
3+
RiskDetectorCommand, PingCommand
4+
from nameapi_client.ontology.input_person import InputPerson
5+
from nameapi_client.services.email_parser import EmailDisposableResult, EmailNameParserResult
6+
from nameapi_client.services.person_formatter import FormatterResult
7+
from nameapi_client.services.person_genderizer import GenderizerResult
8+
from nameapi_client.services.person_matcher import PersonMatcherResult
9+
from nameapi_client.services.person_parser import PersonNameParserResult
10+
from nameapi_client.services.risk_detector import RiskDetectorResult
11+
12+
13+
class NameApiClient(object):
14+
__cmd_disposable_email_detector = DisposableEmailDetectorCommand()
15+
__cmd_email_name_parser = EmailNameParserCommand()
16+
__cmd_person_name_formatter = PersonNameFormatterCommand()
17+
__cmd_person_genderizer = PersonGenderizerCommand()
18+
__cmd_person_matcher = PersonMatcherCommand()
19+
__cmd_person_name_parser = PersonNameParserCommand()
20+
__cmd_risk_detector = RiskDetectorCommand()
21+
__cmd_ping = PingCommand()
22+
23+
def __init__(self, api_key: str):
24+
self.__api_key = api_key
25+
26+
def disposable_email_detector(self, email_address: str) -> EmailDisposableResult:
27+
return NameApiClient.__cmd_disposable_email_detector.execute(self.__api_key, email_address)
28+
29+
def email_name_parser(self, email_address: str) -> EmailNameParserResult:
30+
return NameApiClient.__cmd_email_name_parser.execute(self.__api_key, email_address)
31+
32+
def person_name_formatter(self, person: InputPerson) -> FormatterResult:
33+
return NameApiClient.__cmd_person_name_formatter.execute(self.__api_key, person)
34+
35+
def person_genderizer(self, person: InputPerson) -> GenderizerResult:
36+
return NameApiClient.__cmd_person_genderizer.execute(self.__api_key, person)
37+
38+
def person_matcher(self, person1: InputPerson, person2: InputPerson) -> PersonMatcherResult:
39+
return NameApiClient.__cmd_person_matcher.execute(self.__api_key, person1, person2)
40+
41+
def person_name_parser(self, person: InputPerson) -> PersonNameParserResult:
42+
return NameApiClient.__cmd_person_name_parser.execute(self.__api_key, person)
43+
44+
def risk_detector(self, person: InputPerson) -> RiskDetectorResult:
45+
return NameApiClient.__cmd_risk_detector.execute(self.__api_key, person)
46+
47+
def ping(self) -> str:
48+
return NameApiClient.__cmd_ping.execute(self.__api_key)
49+
50+

0 commit comments

Comments
 (0)