Reputation: 12034
I need consume some methods defined in a library. In my module, I have
/mymodule/lib/mod.py
And my views.py
in /mymodule/views.py
In my mod.py
I have declared:
class ModClient(object):
"""REST client for Mod API"""
def __init__(self, client_id, secret, environment):
self.client_id = client_id
self.secret = secret
self.environment = environment
def _base_url(self):
base_url = ''
if self.environment == 'sandbox':
base_url = 'https://sandbox.mod.com'
elif self.environment == 'development':
base_url = 'https://development.mod.com'
elif self.environment == 'production':
base_url = 'https://production.mod.com'
return base_url
def _base_params(self):
params = {
'client_id': self.client_id,
'secret': self.secret
}
return params
def _parse_response(self, response):
result = response.json()
if response.status_code != 200:
raise ModClientException(message='HTTP status {}: {}'.format(response.status_code, result),
http_status=response.status_code,
error_type=result.get('error_type', None),
error_code=result.get('error_code', None))
return result
def get_accounts(self, access_token):
url = '{}/accounts/get'.format(self._base_url())
params = self._base_params()
params['access_token'] = access_token
response = requests.post(url, json=params)
return self._parse_response(response)
How I can access to my method get_accounts
from my view.py
assuming that both are in the same module?
Upvotes: 0
Views: 41
Reputation: 73470
If mymodule
is a package itself and its containing folder is in your environment's PYTHONPATH
, import the class via:
from lib.mod import ModClient
Then you should be able to instantiate the class in the view and call methods on the instance:
mc = ModClient()
accounts = mc.get_accounts(token)
A proper IDE (PyCharm, Eclipse, etc.) will do auto-imports for you.
Upvotes: 1