2016-03-01 17:23:23 +00:00
|
|
|
import json
|
|
|
|
|
|
2016-02-11 15:27:08 +00:00
|
|
|
from notifications_python_client.notifications import BaseAPIClient
|
2016-02-23 12:47:48 +00:00
|
|
|
from notifications_python_client.errors import HTTPError
|
2016-01-19 15:50:31 +00:00
|
|
|
|
2016-03-01 10:45:13 +00:00
|
|
|
from app.notify_client.models import User
|
2016-01-28 15:34:02 +00:00
|
|
|
|
2016-01-19 15:50:31 +00:00
|
|
|
|
|
|
|
|
class UserApiClient(BaseAPIClient):
|
2016-01-19 22:47:42 +00:00
|
|
|
def __init__(self, base_url=None, client_id=None, secret=None):
|
|
|
|
|
super(self.__class__, self).__init__(base_url=base_url or 'base_url',
|
|
|
|
|
client_id=client_id or 'client_id',
|
|
|
|
|
secret=secret or 'secret')
|
|
|
|
|
|
|
|
|
|
def init_app(self, app):
|
|
|
|
|
self.base_url = app.config['API_HOST_NAME']
|
|
|
|
|
self.client_id = app.config['ADMIN_CLIENT_USER_NAME']
|
|
|
|
|
self.secret = app.config['ADMIN_CLIENT_SECRET']
|
2016-01-22 11:21:30 +00:00
|
|
|
self.max_failed_login_count = app.config["MAX_FAILED_LOGIN_COUNT"]
|
2016-01-19 15:50:31 +00:00
|
|
|
|
2016-01-22 10:02:41 +00:00
|
|
|
def register_user(self, name, email_address, mobile_number, password):
|
2016-01-19 15:50:31 +00:00
|
|
|
data = {
|
|
|
|
|
"name": name,
|
|
|
|
|
"email_address": email_address,
|
|
|
|
|
"mobile_number": mobile_number,
|
2016-01-19 22:47:42 +00:00
|
|
|
"password": password
|
|
|
|
|
}
|
|
|
|
|
user_data = self.post("/user", data)
|
2016-01-22 11:21:30 +00:00
|
|
|
return User(user_data['data'], max_failed_login_count=self.max_failed_login_count)
|
2016-01-19 22:47:42 +00:00
|
|
|
|
2016-01-20 15:13:15 +00:00
|
|
|
def get_user(self, id):
|
2016-01-22 10:01:14 +00:00
|
|
|
url = "/user/{}".format(id)
|
2016-01-20 15:13:15 +00:00
|
|
|
user_data = self.get(url)
|
2016-01-22 11:21:30 +00:00
|
|
|
return User(user_data['data'], max_failed_login_count=self.max_failed_login_count)
|
2016-01-20 15:13:15 +00:00
|
|
|
|
2016-02-23 12:47:48 +00:00
|
|
|
def get_user_by_email(self, email_address):
|
2016-03-02 15:25:04 +00:00
|
|
|
try:
|
|
|
|
|
params = {'email': email_address}
|
|
|
|
|
user_data = self.get('/user/email', params=params)
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
if e.status_code == 404:
|
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
2016-02-23 12:47:48 +00:00
|
|
|
return User(user_data['data'], max_failed_login_count=self.max_failed_login_count)
|
|
|
|
|
|
2016-01-21 11:33:53 +00:00
|
|
|
def get_users(self):
|
2016-01-22 09:22:18 +00:00
|
|
|
users_data = self.get("/user")['data']
|
2016-01-21 11:33:53 +00:00
|
|
|
users = []
|
|
|
|
|
for user in users_data:
|
2016-01-22 11:21:30 +00:00
|
|
|
users.append(User(user, max_failed_login_count=self.max_failed_login_count))
|
2016-01-21 11:33:53 +00:00
|
|
|
return users
|
|
|
|
|
|
2016-01-20 15:13:15 +00:00
|
|
|
def update_user(self, user):
|
|
|
|
|
data = user.serialize()
|
2016-01-22 10:01:14 +00:00
|
|
|
url = "/user/{}".format(user.id)
|
2016-01-20 15:13:15 +00:00
|
|
|
user_data = self.put(url, data=data)
|
2016-01-22 11:21:30 +00:00
|
|
|
return User(user_data['data'], max_failed_login_count=self.max_failed_login_count)
|
2016-01-20 15:13:15 +00:00
|
|
|
|
2016-01-27 17:26:22 +00:00
|
|
|
def verify_password(self, user_id, password):
|
2016-01-21 11:33:53 +00:00
|
|
|
try:
|
2016-01-27 17:26:22 +00:00
|
|
|
url = "/user/{}/verify/password".format(user_id)
|
|
|
|
|
data = {"password": password}
|
2016-01-27 17:13:56 +00:00
|
|
|
self.post(url, data=data)
|
|
|
|
|
return True
|
2016-01-21 11:33:53 +00:00
|
|
|
except HTTPError as e:
|
|
|
|
|
if e.status_code == 400 or e.status_code == 404:
|
|
|
|
|
return False
|
|
|
|
|
|
2016-02-22 12:33:59 +00:00
|
|
|
def send_verify_code(self, user_id, code_type, to):
|
|
|
|
|
data = {'to': to}
|
2016-02-19 16:08:44 +00:00
|
|
|
endpoint = '/user/{0}/{1}-code'.format(user_id, code_type)
|
2016-01-27 12:22:32 +00:00
|
|
|
resp = self.post(endpoint, data=data)
|
|
|
|
|
|
|
|
|
|
def check_verify_code(self, user_id, code, code_type):
|
|
|
|
|
data = {'code_type': code_type, 'code': code}
|
|
|
|
|
endpoint = '/user/{}/verify/code'.format(user_id)
|
|
|
|
|
try:
|
|
|
|
|
resp = self.post(endpoint, data=data)
|
|
|
|
|
return True, ''
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
if e.status_code == 400 or e.status_code == 404:
|
|
|
|
|
if 'Code not found' in e.message:
|
|
|
|
|
return False, 'Code not found'
|
|
|
|
|
elif 'Code has expired' in e.message:
|
|
|
|
|
return False, 'Code has expired'
|
2016-02-11 16:23:41 +00:00
|
|
|
else:
|
|
|
|
|
# TODO what is the default message?
|
|
|
|
|
return False, 'Code not found'
|
2016-01-27 12:22:32 +00:00
|
|
|
raise e
|
|
|
|
|
|
2016-02-23 17:51:09 +00:00
|
|
|
def get_users_for_service(self, service_id):
|
|
|
|
|
endpoint = '/service/{}/users'.format(service_id)
|
|
|
|
|
resp = self.get(endpoint)
|
2016-03-01 10:45:13 +00:00
|
|
|
return [User(data) for data in resp['data']]
|
2016-02-29 17:35:21 +00:00
|
|
|
|
2016-03-01 17:23:23 +00:00
|
|
|
def add_user_to_service(self, service_id, user_id):
|
2016-02-29 17:35:21 +00:00
|
|
|
endpoint = '/service/{}/users/{}'.format(service_id, user_id)
|
2016-03-01 17:32:04 +00:00
|
|
|
resp = self.post(endpoint, data={})
|
2016-02-29 17:35:21 +00:00
|
|
|
return User(resp['data'], max_failed_login_count=self.max_failed_login_count)
|