Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pydantic #45

Merged
merged 2 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"88"
],
"python.linting.enabled": true,
"editor.formatOnPaste": false,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"files.trimTrailingWhitespace": true,
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 22.1.0
rev: 22.3.0
hooks:
- id: black
name: black
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Patrik
Copyright (c) 2022 Patrik

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 1 addition & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
[![PyPI version](https://badge.fury.io/py/pygleif.svg)](https://badge.fury.io/py/pygleif)

This is a Python class that queries the API of GLEIF.org to return data about a specific entity (corporation.) It can also parse the XML-files provided by GLEIF. It is also possible to search for organisation id to find LEI codes and also to get child notes for a specific LEI code.

Usage if you query the API:

```
from pygleif import GLEIF

data = GLEIF('8RS0AKOLN987042F2V04')
print(data.registration.initial_registration_date)
```

## 1. How to use

Usage if you use the XML files:

```
from pygleif import GLEIFParseRelationshipRecord

# XML is the content (converted to text) of a <RelationshipRecord>
data = GLEIFParseRelationshipRecord(XML)
print(data.raw.Relationship.StartNode.NodeID.text) # Uses BeautifulSoup to convert to object

```

There are also some examples available in the sources' example folder.
This library queries the API of GLEIF.org to return data about a specific entity company. The result is typed using `Pydantic` It is also possible to search for organisation id to find LEI codes and also to get child notes for a specific LEI code.
8 changes: 0 additions & 8 deletions example.py

This file was deleted.

54 changes: 0 additions & 54 deletions examples/example_download_parse_xml.py

This file was deleted.

27 changes: 12 additions & 15 deletions examples/query_api.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
"""Example."""
# Hack to allow relative import above top level package
import os
import sys

folder = os.path.dirname(os.path.abspath(__file__)) # noqa
sys.path.insert(0, os.path.normpath("%s/.." % folder)) # noqa
from pprint import pprint
folder = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.normpath("%s/.." % folder))
from pygleif import PyGleif

from pygleif import Search
from pygleif.gleif import GLEIF

gleif_data = GLEIF("506700GE1G29325QX363")
gleif_search = Search("986228608")


pprint((gleif_data.raw))
print(gleif_data.entity.legal_jurisdiction)
print(gleif_data.entity.legal_form)
print(gleif_data.entity.business_register_entity_id)
print(gleif_search.lei)
for entity in [
"549300MLUDYVRQOOXS22",
"M312WZV08Y7LYUC71685",
"IGJSJL3JD5P30I6NJZ34",
"3C7474T6CDKPR9K6YT90",
]:
gleif: PyGleif = PyGleif(entity)
print(gleif.response.data.attributes.registration.initial_registration_date)
6 changes: 3 additions & 3 deletions pygleif/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Make Python interpret this as a package."""

from .gleif import GLEIF
from .search import DirectChild, GLEIFParseRelationshipRecord, Search
from .gleif import PyGleif
from .search import Search

__all__ = ["GLEIF"]
__all__ = ["PyGleif", "Search"]
40 changes: 0 additions & 40 deletions pygleif/address.py

This file was deleted.

12 changes: 12 additions & 0 deletions pygleif/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Pydantic Representation of models."""
from pydantic import BaseModel, Field

from .data import Data
from .meta import Meta


class GLEIFResponse(BaseModel):
"""Represent a base response."""

meta: Meta = Field(alias="meta")
data: Data = Field(alias="data")
145 changes: 145 additions & 0 deletions pygleif/api/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
"""Data."""
from datetime import datetime
from typing import Any, List, Optional

from pydantic import BaseModel, Field


class ValidatedAt(BaseModel):
"""Represent validated at information."""

id: str = Field(alias="id")
other: Optional[str] = Field(alias="other")


class Registration(BaseModel):
"""Represent registration information."""

corroboration_level: str = Field(alias="corroborationLevel")
initial_registration_date: datetime = Field(alias="initialRegistrationDate")
last_update_date: datetime = Field(alias="lastUpdateDate")
managing_lou: str = Field(alias="managingLou")
next_renewal_date: datetime = Field(alias="nextRenewalDate")
other_validation_authorities: List[Any] = Field(alias="otherValidationAuthorities")
status: str = Field(alias="status")
validated_as: str = Field(alias="validatedAs")
validated_at: ValidatedAt = Field(alias="validatedAt")


class GeneralEntity(BaseModel):
"""Represent a general entity ."""

lei: Optional[str] = Field(alias="lei")
name: Optional[str] = Field(alias="name")


class Address(BaseModel):
"""Represent address ."""

language: str = Field(alias="language")
address_lines: List[str] = Field(alias="addressLines")
address_number: Optional[str] = Field(alias="addressNumber")
address_number_within_building: Optional[str] = Field(
alias="addressNumberWithinBuilding"
)
mail_routing: Optional[str] = Field(alias="mailRouting")
city: str = Field(alias="city")
region: str = Field(alias="region")
country: str = Field(alias="country")
postal_code: str = Field(alias="postalCode")


class Expiration(BaseModel):
"""Represent expiration data."""

date: Optional[str] = Field(alias="date")
reason: Optional[str] = Field(alias="reason")


class LegalForm(BaseModel):
"""Represent the legal form."""

id: Optional[str] = Field(alias="id")
other: Optional[str] = Field(alias="other")


class Name(BaseModel):
"""Represent the name."""

language: str = Field(alias="language")
name: str = Field(alias="name")
type: Optional[str] = Field(alias="type")


class RegisteredAt(BaseModel):
"""Represent registered at."""

id: str = Field(alias="id")
other: Optional[str] = Field(alias="other")


class Entity(BaseModel):
"""Represent entity information."""

associated_entity: GeneralEntity = Field(alias="associatedEntity")
category: str = Field(alias="category")
creation_date: Optional[str] = Field(alias="creationDate")
event_groups: List[Any] = Field(alias="eventGroups")
expiration: Expiration = Field(alias="expiration")
headquarters_address: Address = Field(alias="headquartersAddress")
jurisdiction: str = Field(alias="jurisdiction")
legal_address: Address = Field(alias="legalAddress")
legal_form: LegalForm = Field(alias="legalForm")
legal_name: Name = Field("legalName")
other_addresses: List[Any] = Field(alias="otherAddresses")
other_names: Name = Field("otherNames")
registered_as: str = Field(alias="registeredAs")
registered_at: RegisteredAt = Field("registeredAt")
status: str = Field(alias="status")
successor_entities: List[Any] = Field(alias="successorEntities")
sub_category: Optional[str] = Field(alias="subCategory")
successor_entity: GeneralEntity = Field(alias="successorEntity")
transliteraded_other_names: List[Any] = Field(alias="transliteratedOtherNames")


class Attributes(BaseModel):
"""Represent attribute information."""

bic: Optional[List[str]] = Field(alias="bic")
lei: str = Field(alias="lei")
entity: Entity = Field(alias="entity")
registration: Registration = Field(alias="registration")


class LinkData(BaseModel):
"""Represent a link ."""

self: Optional[str] = Field(alias="self")
related: Optional[str] = Field(alias="related")
reporting_exception: Optional[str] = Field(alias="reporting-exception")


class Links(BaseModel):
"""Represent a link ."""

links: LinkData = Field(alias="links")


class Relationships(BaseModel):
"""Represent a relationships ."""

managing_lou: Links = Field(alias="managing-lou")
lei_issuer: Links = Field(alias="lei-issuer")
field_modifications: Links = Field(alias="field-modifications")
direct_parent: Links = Field(alias="direct-parent")
ultimate_parent: Links = Field(alias="ultimate-parent")


class Data(BaseModel):
"""Represent data information."""

id: str = Field(alias="id")
type: str = Field(alias="type")
attributes: Attributes = Field(alias="attributes")
links: LinkData = Field(alias="links")
relationships: Relationships = Field(alias="relationships")
16 changes: 16 additions & 0 deletions pygleif/api/meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Meta data."""
from datetime import datetime

from pydantic import BaseModel, Field


class GoldenCopy(BaseModel):
"""Represent golden copy information."""

publish_date: datetime = Field(alias="publishDate")


class Meta(BaseModel):
"""Represent meta information."""

golden_copy: GoldenCopy = Field(alias="goldenCopy")
Loading