Skip to content

Commit

Permalink
Merge pull request #2 from code4romania/danniel/refactor_connect_to_n…
Browse files Browse the repository at this point in the history
…gohub
  • Loading branch information
tudoramariei authored Aug 21, 2024
2 parents 678f4e3 + 91ed48d commit 62a50df
Show file tree
Hide file tree
Showing 15 changed files with 583 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.env*
!.env.example*

### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Project name
# NGO Hub

[![GitHub contributors][ico-contributors]][link-contributors]
[![GitHub last commit][ico-last-commit]][link-last-commit]
Expand Down
18 changes: 14 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name = "NGOHub"
version = "0.0.3"
description = "Python client for ngohub.ro API"
readme = "README.md"
license = { file = "LICENSE" }
classifiers = [
"Development Status :: 1 - Planning",
"Intended Audience :: Developers",
Expand All @@ -28,6 +27,9 @@ Download = "https://github.com/code4romania/pyngohub/tags"
requires = ["setuptools>=72.1"]
build-backend = "setuptools.build_meta"

[lint]
ignore = []

[tool.ruff]
exclude = [
".eggs",
Expand All @@ -42,9 +44,6 @@ exclude = [
line-length = 120
target-version = "py311"

# TEMP: Disable until we have some code
ignore = ["F401"]

[tool.black]
line-length = 120
target-version = ["py311"]
Expand All @@ -59,3 +58,14 @@ known_tests = "tests"
known_first_party = "ngohub"
default_section = "THIRDPARTY"
sections = "FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER,TESTS"

[tool.pytest.ini_options]
minversion = "8.0"
addopts = "-ra -q"
testpaths = [
"tests",
]
env_files = [
".env",
".env.test",
]
34 changes: 30 additions & 4 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,32 @@ click==8.1.7
# pip-tools
colorama==0.4.6
# via tox
coverage==7.6.1
# via
# -r tests.txt
# pytest-cov
distlib==0.3.8
# via virtualenv
filelock==3.15.4
# via
# tox
# virtualenv
iniconfig==2.0.0
# via
# -r tests.txt
# pytest
mypy-extensions==1.0.0
# via
# -r format.txt
# black
packaging==24.1
# via
# -r format.txt
# -r tests.txt
# black
# build
# pyproject-api
# pytest
# tox
pathspec==0.12.1
# via
Expand All @@ -49,20 +59,36 @@ platformdirs==4.2.2
# tox
# virtualenv
pluggy==1.5.0
# via tox
# via
# -r tests.txt
# pytest
# tox
pyproject-api==1.7.1
# via tox
pyproject-hooks==1.1.0
# via
# build
# pip-tools
ruff==0.5.5
pytest==8.3.2
# via
# -r tests.txt
# pytest-cov
# pytest-env
pytest-cov==5.0.0
# via -r tests.txt
pytest-env==1.1.3
# via -r tests.txt
python-dotenv==1.0.1
# via -r tests.txt
ruff==0.5.7
# via -r format.txt
tox==4.16.0
schema==0.7.7
# via -r tests.txt
tox==4.18.0
# via -r dev.in
virtualenv==20.26.3
# via tox
wheel==0.43.0
wheel==0.44.0
# via pip-tools

# The following packages are considered to be unsafe in a requirements file:
Expand Down
2 changes: 1 addition & 1 deletion requirements/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ pathspec==0.12.1
# via black
platformdirs==4.2.2
# via black
ruff==0.5.5
ruff==0.5.7
# via -r format.in
7 changes: 7 additions & 0 deletions requirements/tests.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pytest
pytest-env
pytest-cov

python-dotenv

schema
21 changes: 21 additions & 0 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@
#
# pip-compile --strip-extras tests.in
#
coverage==7.6.1
# via pytest-cov
iniconfig==2.0.0
# via pytest
packaging==24.1
# via pytest
pluggy==1.5.0
# via pytest
pytest==8.3.2
# via
# -r tests.in
# pytest-cov
# pytest-env
pytest-cov==5.0.0
# via -r tests.in
pytest-env==1.1.3
# via -r tests.in
python-dotenv==1.0.1
# via -r tests.in
schema==0.7.7
# via -r tests.in
2 changes: 1 addition & 1 deletion src/ngohub/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from core import NGOHub
from .core import NGOHub as NGOHub
100 changes: 98 additions & 2 deletions src/ngohub/core.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,98 @@
class NGOHub:
pass
from abc import ABC, abstractmethod
from typing import Any, Dict, List

from ngohub.network import HTTPClient, HTTPClientResponse


class BaseHub(ABC):
"""
Abstract class used to define all the required methods for a hub interface
"""

@abstractmethod
def __init__(self, api_base_url: str) -> None:
pass


class NGOHub(BaseHub):
def __init__(self, api_base_url: str) -> None:
self.api_base_url: str = api_base_url or ""
self.client: HTTPClient = HTTPClient(self.api_base_url)

def is_healthy(self) -> bool:
response: HTTPClientResponse = self.client.api_get("/health/")

response_is_ok: bool = response.to_str() == "OK"

return response_is_ok

def get_version(self) -> Dict[str, str]:
response: HTTPClientResponse = self.client.api_get("/version/")

response_dict: Dict = response.to_dict()
version_revision: Dict[str, str] = {
"version": response_dict["version"],
"revision": response_dict["revision"],
}

return version_revision

def get_file_url(self, path: str) -> str:
response: HTTPClientResponse = self.client.api_get(f"/file?path={path}")

return response.to_str()

def _get_nomenclature(self, nomenclature: str) -> Any:
response: HTTPClientResponse = self.client.api_get(f"/nomenclatures/{nomenclature}")

return response.to_dict()

def get_cities_nomenclatures(
self, search: str = None, county_id: int = None, city_id: int = None
) -> List[Dict[str, Any]]:
mandatory_params: List[Any] = [search, county_id]
if all(param is None for param in mandatory_params):
raise ValueError("Please provide at least one of the following: county_id, search")

search_query: List[str] = []
if search:
search_query.append(f"search={search}")
if county_id:
search_query.append(f"countyId={county_id}")
if city_id:
search_query.append(f"cityId={city_id}")

return self._get_nomenclature(f"cities?{'&'.join(search_query)}")

def get_counties_nomenclatures(self) -> List[Dict[str, Any]]:
return self._get_nomenclature("counties")

def get_domains_nomenclatures(self):
return self._get_nomenclature("domains")

def get_regions_nomenclatures(self):
return self._get_nomenclature("regions")

def get_federations_nomenclatures(self):
return self._get_nomenclature("federations")

def get_coalitions_nomenclatures(self):
return self._get_nomenclature("coalitions")

def get_faculties_nomenclatures(self):
return self._get_nomenclature("faculties")

def get_skills_nomenclatures(self):
return self._get_nomenclature("skills")

def get_practice_domains_nomenclatures(self):
return self._get_nomenclature("practice-domains")

def get_service_domains_nomenclatures(self):
return self._get_nomenclature("service-domains")

def get_beneficiaries_nomenclatures(self):
return self._get_nomenclature("beneficiaries")

def get_issuers_nomenclatures(self):
return self._get_nomenclature("issuers")
46 changes: 46 additions & 0 deletions src/ngohub/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class HubException(Exception):
"""The base exception for all Hub issues"""

pass


class OrganizationException(HubException):
"""The base exception for all Hub Organization issues"""

pass


class ClosedOrganizationRegistrationException(OrganizationException):
"""New organizations cannot be registered anymore"""

pass


class DisabledOrganizationException(OrganizationException):
"""The requested organization has been disabled from the platform"""

pass


class DuplicateOrganizationException(OrganizationException):
"""An organization with the same NGO Hub ID already exists"""

pass


class MissingOrganizationException(OrganizationException):
"""The requested organization does not exist"""

pass


class HubHTTPException(HubException):
"""The base exception for all Hub HTTP/network issues"""

pass


class HubDecodeException(HubHTTPException):
"""Failed to decode response"""

pass
Loading

0 comments on commit 62a50df

Please sign in to comment.