diff --git a/test/test_client.py b/test/test_client.py new file mode 100644 index 0000000..433de78 --- /dev/null +++ b/test/test_client.py @@ -0,0 +1,12 @@ +import inspect + +from welkin.models import __all__ as all_models + + +def test_build_resources(client): + for class_name in all_models: + assert hasattr(client, class_name) + + attr = getattr(client, class_name) + assert inspect.isclass(attr) + assert attr._client == client diff --git a/welkin/client.py b/welkin/client.py index e8645e1..dbc5fa5 100644 --- a/welkin/client.py +++ b/welkin/client.py @@ -10,10 +10,10 @@ from requests.compat import urljoin from requests.packages.urllib3.util.retry import Retry # type: ignore -from welkin import __version__ +from welkin import __version__, models from welkin.authentication import WelkinAuth from welkin.exceptions import WelkinHTTPError -from welkin.models import * +from welkin.models.base import Collection, Resource from welkin.util import clean_request_params, clean_request_payload logger = logging.getLogger(__name__) @@ -122,18 +122,39 @@ def __init__( def __build_resources(self) -> None: """Add each resource with a reference to this instance.""" - for k, v in globals().items(): + self.Assessment = models.Assessment + self.AssessmentRecord = models.AssessmentRecord + self.AssessmentRecordAnswers = models.AssessmentRecordAnswers + self.AssessmentRecords = models.AssessmentRecords + self.Assessments = models.Assessments + self.CalendarEvent = models.CalendarEvent + self.CalendarEvents = models.CalendarEvents + self.Schedules = models.Schedules + self.CarePlan = models.CarePlan + self.CarePlanOverview = models.CarePlanOverview + self.CDT = models.CDT + self.CDTs = models.CDTs + self.Chat = models.Chat + self.Chats = models.Chats + self.SearchChats = models.SearchChats + self.Disposition = models.Disposition + self.Encounter = models.Encounter + self.Encounters = models.Encounters + self.Formations = models.Formations + self.Patient = models.Patient + self.Patients = models.Patients + self.User = models.User + self.Users = models.Users + + for k, v in vars(self).items(): try: - for base in v.__bases__: - if base.__name__ not in ["Collection", "Resource"]: - continue - - v._client = self - setattr(self, k, v) - - except AttributeError: + issubclass(v, (Collection, Resource)) + except TypeError: + # Failed because `issubclass` expects a class. continue + getattr(self, k)._client = self + def prepare_request(self, request): if request.json: request.json = clean_request_payload(request.json) diff --git a/welkin/models/__init__.py b/welkin/models/__init__.py index 4bf560d..f7a7644 100644 --- a/welkin/models/__init__.py +++ b/welkin/models/__init__.py @@ -14,8 +14,6 @@ from welkin.models.patient import Patient, Patients from welkin.models.user import User, Users -# NOTE: If a class isn't imported here and added to __all__, it will not be callable -# from a `Client` instance. Also, for legibility, keep this list alphabetical. __all__ = [ "Assessment", "AssessmentRecord",