def _make_request(method, type, url, params={}, data={}): parsed_url = urlparse(url) validate_url(parsed_url) path = get_target_path(parsed_url, params) conn = http.client.HTTPSConnection(parsed_url.hostname) if type == 'json': data = json.dumps(data) if method == 'GET': conn.request('GET', path) elif method == 'POST': headers = {'Content-Type': 'application/json'} conn.request('POST', path, body=data, headers=headers) response = conn.getresponse() if response.status // 100 != 2: exception = RequestError('HTTP Status Code: ' + str(response.status), response.status) sys.last_value = exception raise exception if type == 'json': return parse_json_response(response) return response
def send_email(): ''' A route to send a reset code via email to a user ''' payload = request.get_json() if not payload['email']: raise RequestError(description="Missing data in request body") auth_passwordreset_request(payload['email']) return dumps({})
def disconnect(): ''' A route to logout the user ''' payload = request.get_json() if not payload['token']: raise RequestError(description="Missing data in request body") successful = auth_logout(payload['token']) return dumps(successful)
def connect(): ''' A route to login a user ''' payload = request.get_json() if not payload['email'] or not payload['password']: raise RequestError(description="Missing data in request body") login_info = auth_login(payload['email'], payload['password']) return dumps(login_info)
def reset_password(): ''' A route to reset a user's password ''' payload = request.get_json() if not payload['reset_code'] or not payload['new_password']: raise RequestError(description="Missing data in request body") auth_passwordreset_reset(payload['reset_code'], payload['new_password']) return dumps({})
def user_remove(): ''' A wrapper for other.user_remove() ''' token = request.args.get('token') u_id = request.args.get('u_id') if not token or not u_id: raise RequestError(description='Missing data in request body') other.user_remove(token, int(u_id)) return json.dumps({})
def users_all(): ''' Wrapper for users_all ''' token = request.args.get('token') if not token: raise RequestError(description="Missing data in request body") every_user = other.users_all(token) return json.dumps(every_user)
def join(): ''' A route to call channel_join ''' payload = request.get_json() if not payload['token'] or not payload['channel_id']: raise RequestError(description="Missing data in request body") channel.channel_join(payload['token'], int(payload['channel_id'])) return json.dumps({})
def unpin(): ''' a route which calls message_pin from message ''' data = request.get_json() if not data['token'] or not data['message_id']: raise RequestError(description="Missing data in request body") message.message_unpin(data['token'], int(data['message_id'])) return dumps({})
def profile_uploadphoto(): ''' A route to upload a cropped photo given its url ''' payload = request.get_json() if not payload['token'] or not payload['img_url']: raise RequestError(description="Missing data in request body") if not payload['x_start'] or not payload['y_start']: raise RequestError(description="Missing data in request body") if not payload['x_end'] or not payload['y_end']: raise RequestError(description="Missing data in request body") box = (int(payload['x_start']), int(payload['y_start']), int(payload['x_end']), int(payload['y_end'])) user.profile_uploadphoto(payload['token'], payload['img_url'], box) return json.dumps({})
def listall(): ''' a route which calls channels_listall ''' token = request.args.get('token') if not token: raise RequestError(description="Missing data in request body") response = channels_listall(token) return dumps(response)
def profile_sethandle(): ''' A route to call user_profile_sethandle ''' payload = request.get_json() if not payload["token"] or not payload["handle_str"]: raise RequestError(description="Missing data in request body") user.user_profile_sethandle(payload["token"], payload["handle_str"]) return json.dumps({})
def create(): ''' a route which calls channels_create ''' data = request.get_json() if not data['token'] or not data['name'] or data['is_public'] is None: raise RequestError(description="Missing data in request body") response = channels_create(data['token'], data['name'], data['is_public']) return dumps(response)
def details(): ''' A route to gather a channel's details ''' token = request.args.get('token') channel_id = request.args.get('channel_id') if not token or not channel_id: raise RequestError(description="Missing data in request body") info = channel.channel_details(token, int(channel_id)) return json.dumps(info)
def create_user(): ''' A route to register a user ''' payload = request.get_json() if not payload['email'] or not payload['password'] \ or not payload['name_first'] or not payload['name_last']: raise RequestError(description="Missing data in request body") reg_info = auth_register( payload['email'], payload['password'], payload['name_first'], payload['name_last']) return dumps(reg_info)
def react(): ''' a route that calls message_react from message ''' data = request.get_json() if not data['token'] or not data['message_id'] or not data['react_id']: raise RequestError(description="Missing data in request body") message.message_react(data['token'], int(data['message_id']), int(data['react_id'])) return dumps({})
def send(): ''' calls message_send from message ''' data = request.get_json() if not data['token'] or not data['channel_id'] or not data['message']: raise RequestError(description="Missing data in request body") response = message.message_send(data['token'], int(data['channel_id']), data['message']) return dumps(response)
def profile(): ''' A route to call user_profile ''' token = request.args.get('token') u_id = int(request.args.get('u_id')) if not token or not u_id: raise RequestError(description="Missing data in request body") to_send = user.user_profile(token, u_id) return json.dumps(to_send)
def u_per_change(): ''' A wrapper for userpermission_change ''' payload = request.get_json() if not payload['token'] or not payload['u_id'] or not payload['permission_id']: raise RequestError(description=f"Missing data in request body") other.userpermission_change( payload['token'], int(payload['u_id']), int(payload['permission_id'])) return json.dumps({})
def search(): ''' Wrappers for search ''' token = request.args.get('token') query_str = request.args.get('query_str') if not token or (not query_str and not query_str == ""): raise RequestError(description="Missing data in request body") matching_msgs = other.search(token, query_str) return json.dumps(matching_msgs)
def edit(): ''' a route which calls message_edit from message ''' data = request.get_json() if not data["token"] or not data["message_id"] \ or (not data["message"] and not data["message"] == ""): raise RequestError(description="Missing data in request body") message.message_edit(data["token"], int(data["message_id"]), data["message"]) return dumps({})
def st_active(): ''' Wrapper for standup_active ''' token = request.args.get('token') channel_id = request.args.get('channel_id') if not token or not channel_id: raise RequestError(description=f"Missing data in request body") to_send = standup.standup_active(token, int(channel_id)) return json.dumps(to_send)
def st_send(): ''' Wrapper for standup_send ''' payload = request.get_json() if not payload['token'] or not payload['channel_id'] or not payload[ 'message']: raise RequestError(description=f"Missing data in request body") standup.standup_send(payload['token'], int(payload['channel_id']), payload['message']) return json.dumps({})
def messages(): ''' A route to call channel_messages ''' token = request.args.get('token') channel_id = request.args.get('channel_id') start = request.args.get('start') if not token or not channel_id or start is None: raise RequestError(description="Missing data in request body") to_send = channel.channel_messages(token, int(channel_id), int(start)) return json.dumps(to_send)
def st_start(): ''' Wrapper for standup_start ''' payload = request.get_json() if not payload['token'] or not payload['channel_id'] or not payload[ 'length']: raise RequestError(description=f"Missing data in request body") to_send = standup.standup_start(payload['token'], int(payload['channel_id']), int(payload['length'])) return json.dumps(to_send)