44 datetime ,
55 timedelta ,
66)
7+ from inspect import isclass
78import requests
89from atomx .version import API_VERSION , VERSION
910from atomx import models
@@ -78,7 +79,7 @@ def login(self, email=None, password=None):
7879 raise InvalidCredentials
7980 raise APIError (r .json ()['error' ])
8081 self .auth_tkt = r .json ()['auth_tkt' ]
81- self .user = models .User (** r .json ()['user' ])
82+ self .user = models .User (session = self , ** r .json ()['user' ])
8283
8384 def logout (self ):
8485 """Removes authentication token from session."""
@@ -236,9 +237,9 @@ def report(self, scope=None, groups=None, metrics=None, where=None, from_=None,
236237 self .last_response = r_json
237238
238239 if is_scheduled_report :
239- return models .ScheduledReport (self , ** report )
240+ return models .ScheduledReport (session = self , ** report )
240241
241- return models .Report (self , ** report )
242+ return models .Report (session = self , ** report )
242243
243244 def report_status (self , report ):
244245 """Get the status for a `report`.
@@ -314,6 +315,8 @@ def get(self, resource, *args, **kwargs):
314315 >>> publisher = atomx.get('publisher/23')
315316 >>>> # or get the same publisher using the id as parameter
316317 >>> publisher = atomx.get('publisher', 23)
318+ >>>> # or use an atomx model
319+ >>> publisher = atomx.get(atomx.models.Publisher(23))
317320 >>> assert publisher.id == 23
318321 >>> assert isinstance(publisher, atomx.models.Publisher)
319322
@@ -347,7 +350,15 @@ def get(self, resource, *args, **kwargs):
347350
348351 :return: a class from :mod:`.models` or a list of models depending on param `resource`
349352 """
350- resource = resource .strip ('/' )
353+ if isclass (resource ) and issubclass (resource , models .AtomxModel ):
354+ resource = resource ._resource_name
355+ elif isinstance (resource , models .AtomxModel ):
356+ resource_path = resource ._resource_name
357+ if hasattr (resource , 'id' ):
358+ resource_path += '/' + str (resource .id )
359+ resource = resource_path
360+ else :
361+ resource = resource .strip ('/' )
351362 for a in args :
352363 resource += '/' + str (a )
353364 r = self .session .get (self .api_endpoint + resource , params = kwargs )
@@ -363,12 +374,12 @@ def get(self, resource, *args, **kwargs):
363374 model = get_model_name (model_name )
364375 if model and res :
365376 if isinstance (res , list ):
366- return [getattr (models , model )(self , ** m ) for m in res ]
367- return getattr (models , model )(self , ** res )
377+ return [getattr (models , model )(session = self , ** m ) for m in res ]
378+ return getattr (models , model )(session = self , ** res )
368379 elif model_name == 'reporting' : # special case for `/reports` status
369380 return {
370- 'reports' : [models .Report (self , ** m ) for m in res ['reports' ]],
371- 'scheduled' : [models .ScheduledReport (self , ** m ) for m in res ['scheduled' ]]
381+ 'reports' : [models .Report (session = self , ** m ) for m in res ['reports' ]],
382+ 'scheduled' : [models .ScheduledReport (session = self , ** m ) for m in res ['scheduled' ]]
372383 }
373384 return res
374385
@@ -394,7 +405,7 @@ def post(self, resource, json, **kwargs):
394405 self .last_response = r_json
395406 model = get_model_name (model_name )
396407 if model and isinstance (res , list ):
397- return [getattr (models , model )(self , ** m ) for m in res ]
408+ return [getattr (models , model )(session = self , ** m ) for m in res ]
398409 return res
399410
400411 def put (self , resource , id , json , ** kwargs ):
0 commit comments