-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- changed id to ibge_id - location service class now requires the model which service deals with. - removed @AbstractMethod from parse, making it the default parser if no override.
- Loading branch information
1 parent
b0bc7bf
commit 1a81f23
Showing
6 changed files
with
57 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
@dataclass(frozen=True) | ||
class Location: | ||
id: int | ||
ibge_id: int | ||
name: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
from typing import override | ||
from ibgeapi.models.country import Country | ||
from ibgeapi.services.location import LocationService | ||
|
||
class CountryService(LocationService): | ||
|
||
def __init__(self) -> None: | ||
super().__init__("/paises") | ||
super().__init__(Country, "/paises") | ||
self._prefix = "pais-" | ||
self._filters["view"] = "nivelado" | ||
|
||
def parse(self, response) -> Country: | ||
@override | ||
def parse(self, obj: dict) -> Country: | ||
return Country( | ||
id=response[f"{self._prefix}M49"], | ||
iso_alpha2=response[f"{self._prefix}ISO-ALPHA-2"], | ||
iso_alpha3=response[f"{self._prefix}ISO-ALPHA-3"], | ||
name=response[f"{self._prefix}nome"] | ||
ibge_id=obj[f"{self._prefix}M49"], | ||
iso_alpha2=obj[f"{self._prefix}ISO-ALPHA-2"], | ||
iso_alpha3=obj[f"{self._prefix}ISO-ALPHA-3"], | ||
name=obj[f"{self._prefix}nome"] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,41 @@ | ||
from abc import abstractmethod | ||
from collections import defaultdict | ||
from typing import Generic, Type, final | ||
from ibgeapi.models.location import Location | ||
from ibgeapi.services.ibge import IGBEService | ||
from typing import TypeVar | ||
|
||
class LocationService(IGBEService): | ||
T = TypeVar('T', bound=Location) | ||
|
||
@abstractmethod | ||
def parse(self, response): | ||
pass | ||
class LocationService(IGBEService, Generic[T]): | ||
|
||
def get(self, loc_id:int): | ||
response = self.client.get(f"{self.endpoint}/{loc_id}", | ||
params=self._filters) | ||
|
||
if isinstance(response, list): | ||
response = response[0] | ||
def __init__(self, model: Type[T], endpoint: str) -> None: | ||
super().__init__() | ||
self._model = model | ||
self._filters = defaultdict(str) | ||
self.endpoint = f"/localidades{endpoint}" | ||
|
||
return self.parse(response) \ | ||
if response else None | ||
def parse(self, obj: dict) -> T: | ||
return self._model( | ||
ibge_id=obj["id"], | ||
name=obj["nome"] | ||
) | ||
|
||
def get_all(self, orderBy:str="nome"): | ||
self._filters["orderBy"] = orderBy | ||
response = self.client.get(self.endpoint, | ||
@final | ||
def get(self, loc_id:int) -> T: | ||
response = self.client.get(f"{self.endpoint}/{loc_id}", | ||
params=self._filters) | ||
return [self.parse(location) for location in response] \ | ||
if response else [] | ||
data = response.json() | ||
if isinstance(data, list): | ||
data = data[0] | ||
|
||
def __init__(self, endpoint: str) -> None: | ||
super().__init__() | ||
self._filters = defaultdict(str) | ||
self.endpoint = f"/localidades{endpoint}" | ||
return self.parse(data) \ | ||
if data else None | ||
|
||
@final | ||
def get_all(self, order_by:str="nome") -> list[T]: | ||
self._filters["orderBy"] = order_by | ||
response = self.client.get(self.endpoint, | ||
params=self._filters) | ||
data = response.json() | ||
return [self.parse(location) for location in data] \ | ||
if data else [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
from typing import override | ||
from ibgeapi.models.region import Region | ||
from ibgeapi.services.location import LocationService | ||
|
||
class RegionService(LocationService): | ||
class RegionService(LocationService[Region]): | ||
|
||
def __init__(self) -> None: | ||
super().__init__("/regioes") | ||
super().__init__(Region, "/regioes") | ||
|
||
def parse(self, response) -> Region: | ||
@override | ||
def parse(self, obj: dict) -> Region: | ||
return Region( | ||
id=response["id"], | ||
name=response["nome"], | ||
acronym=response["sigla"] | ||
ibge_id=obj["id"], | ||
name=obj["nome"], | ||
acronym=obj["sigla"] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
from typing import override | ||
from ibgeapi.models.state import State | ||
from ibgeapi.services.location import LocationService | ||
|
||
class StateService(LocationService): | ||
|
||
def __init__(self) -> None: | ||
super().__init__("/estados") | ||
super().__init__(State, "/estados") | ||
|
||
def parse(self, response) -> State: | ||
@override | ||
def parse(self, obj: dict) -> State: | ||
return State( | ||
id=response["id"], | ||
name=response["nome"], | ||
acronym=response["sigla"] | ||
ibge_id=obj["id"], | ||
name=obj["nome"], | ||
acronym=obj["sigla"] | ||
) |