Skip to content

Commit cc86d13

Browse files
committed
Add type hints and improve code style with ruff
1 parent e1106c1 commit cc86d13

File tree

11 files changed

+163
-126
lines changed

11 files changed

+163
-126
lines changed

docs/conf.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- coding: utf-8 -*-
1+
# noqa: INP001
22
#
33
# pyixapi documentation build configuration file, created by
44
# sphinx-quickstart on Sun Sep 18 18:31:27 2022.
@@ -16,12 +16,12 @@
1616
# add these directories to sys.path here. If the directory is relative to the
1717
# documentation root, use os.path.abspath to make it absolute, like shown here.
1818
#
19-
import os
2019
import sys
20+
from pathlib import Path
2121

22-
from pkg_resources import get_distribution
22+
from pyixapi import __version__ as pyixapi_version
2323

24-
sys.path.insert(0, os.path.abspath("../"))
24+
sys.path.insert(0, str(Path().parent.resolve()))
2525

2626

2727
# -- General configuration ------------------------------------------------
@@ -49,14 +49,14 @@
4949

5050
# General information about the project.
5151
project = "pyixapi"
52-
copyright = "2012, Guillaume Mazoyer"
52+
copyright = "2022, Guillaume Mazoyer"
5353
author = "Guillaume Mazoyer"
5454

5555
# The version info for the project you're documenting, acts as replacement for
5656
# |version| and |release|, also used in various other places throughout the
5757
# built documents.
5858
# The full version, including alpha/beta/rc tags.
59-
release = get_distribution("pyixapi").version
59+
release = pyixapi_version
6060
#
6161
# The short X.Y version.
6262
version = ".".join(release.split(".")[:2])

pyixapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .core.api import API as api
1+
from .core.api import API as api # noqa: N811
22
from .core.api import __version__ # noqa: F401
33
from .core.query import ContentError, RequestError
44

pyixapi/core/api.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
import requests
24

35
from pyixapi.core.endpoint import Endpoint
@@ -32,7 +34,7 @@
3234
__version__ = "0.2.2"
3335

3436

35-
class API(object):
37+
class API:
3638
"""
3739
The API object is the entrypoint for pyixapi.
3840
@@ -42,13 +44,13 @@ class API(object):
4244

4345
def __init__(
4446
self,
45-
url,
46-
key,
47-
secret,
48-
access_token="",
49-
refresh_token="",
50-
user_agent=f"pyixapi/{__version__}",
51-
):
47+
url: str,
48+
key: str,
49+
secret: str,
50+
access_token: str = "",
51+
refresh_token: str = "",
52+
user_agent: str = f"pyixapi/{__version__}",
53+
) -> None:
5254
self.url = url.rstrip("/")
5355
self.key = key
5456
self.secret = secret
@@ -90,7 +92,7 @@ def __init__(
9092
self.role_assignments = Endpoint(self, "role-assignments", model=RoleAssignment)
9193

9294
@property
93-
def version(self):
95+
def version(self) -> int:
9496
"""
9597
Get the API version of IX-API.
9698
"""
@@ -99,20 +101,20 @@ def version(self):
99101
).get_version()
100102

101103
@property
102-
def accounts(self):
104+
def accounts(self) -> Endpoint:
103105
return Endpoint(
104106
self, "customers" if self.version == 1 else "accounts", model=Account
105107
)
106108

107109
@property
108-
def product_offerings(self):
110+
def product_offerings(self) -> Endpoint:
109111
return Endpoint(
110112
self,
111113
"products" if self.version == 1 else "product-offerings",
112114
model=ProductOffering,
113115
)
114116

115-
def authenticate(self):
117+
def authenticate(self) -> Record:
116118
"""
117119
Authenticate and generate a pair of tokens.
118120
@@ -139,7 +141,7 @@ def authenticate(self):
139141

140142
return Record(r, self, self.auth)
141143

142-
def refresh_authentication(self):
144+
def refresh_authentication(self) -> Record:
143145
"""
144146
Prolong authentication by refreshing the tokens pair.
145147
"""
@@ -154,7 +156,7 @@ def refresh_authentication(self):
154156

155157
return Record(r, self, self.auth)
156158

157-
def health(self):
159+
def health(self) -> dict[str, Any]:
158160
"""
159161
Get the health information from IX-API.
160162

pyixapi/core/endpoint.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1+
from http import HTTPStatus
2+
from typing import TYPE_CHECKING
3+
14
from pyixapi.core.query import Request, RequestError
25
from pyixapi.core.response import Record, RecordSet
36
from pyixapi.core.util import cat
47

8+
if TYPE_CHECKING:
9+
from .api import API
10+
511

6-
class Endpoint(object):
12+
class Endpoint:
713
"""
814
Represent actions available on endpoints in the IX-API.
915
1016
Build the correct URL to make queries and the proper :py:class:`.Response`
1117
object.
1218
"""
1319

14-
def __init__(self, api, name, model=None):
20+
def __init__(self, api: "API", name: str, model: Record | None = None) -> None:
1521
self.return_obj = model if model else Record
1622
self.api = api
1723
self.url = cat(api.url, name)
1824
self.name = name
1925

20-
def __str__(self):
26+
def __str__(self) -> str:
2127
return self.url
2228

23-
def all(self):
29+
def all(self) -> RecordSet:
2430
"""
2531
Return all objects from an endpoint.
2632
"""
@@ -31,7 +37,7 @@ def all(self):
3137
)
3238
return RecordSet(self, r)
3339

34-
def filter(self, *args, **kwargs):
40+
def filter(self, *args, **kwargs) -> RecordSet:
3541
"""
3642
Query the list of a given endpoint. Also take named arguments that match the
3743
usable filters on a given endpoint.
@@ -44,7 +50,7 @@ def filter(self, *args, **kwargs):
4450
)
4551
return RecordSet(self, r)
4652

47-
def get(self, *args, **kwargs):
53+
def get(self, *args, **kwargs) -> Record | None:
4854
"""
4955
Return a single object from an endpoint.
5056
"""
@@ -78,12 +84,12 @@ def get(self, *args, **kwargs):
7884
try:
7985
return next(RecordSet(self, r), None)
8086
except RequestError as e:
81-
if e.req.status_code == 404:
87+
if e.req.status_code == HTTPStatus.NOT_FOUND:
8288
return None
83-
else:
84-
raise e
8589

86-
def create(self, *args, **kwargs):
90+
raise e
91+
92+
def create(self, *args, **kwargs) -> Record:
8793
"""
8894
Creates an object on an endpoint.
8995

0 commit comments

Comments
 (0)