Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 4 add random data generator #9

Merged
merged 12 commits into from Feb 18, 2021
4 changes: 2 additions & 2 deletions .github/workflows/pytest.yml
Expand Up @@ -5,8 +5,8 @@ name: Test Flask App
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ issue-4-add-random-data-generator ]
#push:
# branches: [ issue-4-add-random-data-generator ]
pull_request:
branches: [ main ]

Expand Down
62 changes: 49 additions & 13 deletions README.md
Expand Up @@ -16,10 +16,11 @@ To run, simply bring up the webserver, web app, and database with the following
docker-compose build
docker-compose up -d

# Registering a User
To register a user
# Sample Requests
## Registering a User

POST http://localhost/register

POST http://localhost/register
{
"MemberNationalAssociationId": "084001677",
"MemberEmail": "SarahConnor@gmail.com",
Expand Down Expand Up @@ -54,16 +55,17 @@ To register a user
"status": true,
"uli": "602ac76cd790ab1c770cbbbf"
}
## Sample Return - Potential Matches Found
## Sample Return - Potential Matches Found, No User Created, ULI Returned
{
"message": "ULI May Exist!",
"status": true
"message": "Found ULI!",
"status": true,
"uli": "602c6bbead2cef1872ea1e8d"
}
# Querying a User
Sample POST to http://localhost/query
## Querying a User
POST http://localhost/query

{
"MemberNationalAssociationId": "hjRWlmDkIHlxvrcQTbYBSvmKvVRcBw2",
"MemberNationalAssociationId": "hjRWlmDkIHlxvrcQTbYBSvmKvVRcBw",
"MemberFirstName": "Tracy",
"MemberLastName": "Washington",
"MemberEmail": "william05@marquez.com",
Expand All @@ -82,21 +84,55 @@ Sample POST to http://localhost/query
}
## Sample Return - Match Found
{
"message": "ULI May Exist!",
"status": true
"message": "Found ULI!",
"status": true,
"uli": "602c6bbead2cef1872ea1e8d"
}

## Sample Return - No Match Found
{
"message": "ULI Not Found!",
"status": true
"status": true,
"uli": null
}

# Admin Functions
## Find Licensee

POST to http://localhost/find_licensee

{
"token": "_admin_token",
"uli": "602c5445dadc7a3f8160971f"
}


{
"message": "ULI found!",
"status": true,
"uli": "602c6bbead2cef1872ea1e8d"
}
## Remove Licensee
DELETE http://localhost/remove_licensees

{
"token": "_admin_token",
"uli": "602c5445dadc7a3f8160971f"
}

## Generate Test Data
Sample POST to http://localhost/generate_licensees

{
"token": "_admin_token",
"NumLicensees": 1000
}

Sample Return

{
"NumLicensees": 100000
"message": "1000 generated!",
"status": true
}
## Outstanding Questions

Expand Down
104 changes: 95 additions & 9 deletions app/app.py
Expand Up @@ -4,7 +4,7 @@
import settings
import json
from models import Member
from registry import search_licensee, generate_licensees #, create_licensee
from registry import search_licensee, generate_licensees, find_licensee, remove_licensee
from flask import Flask, request, jsonify
from mongoengine import *

Expand Down Expand Up @@ -40,17 +40,17 @@ def licensee():
message = result.get('status_message')
), 201


if result['has_match']:
return jsonify(
status=True,
message = result.get('status', 'ULI May Exist!')
message = result.get('status', 'Found ULI!'),
uli = result.get('uli')
), 201


return jsonify(
status=True,
message='ULI Not Found!'
message='ULI Not Found!',
uli = result.get('uli')
), 404

@app.route('/register', methods=['POST'])
Expand All @@ -70,14 +70,16 @@ def registerLicensee():
if result['has_error']:
return jsonify(
status=False,
message = result.get('status_message')
message = result.get('status_message'),
uli = result.get('uli')
), 201

if result['has_match']:
#We've found possible Licensees that match the registered data, return them
return jsonify(
status=True,
message = result.get('status', 'ULI May Exist!')
message = result.get('status', "Found ULI!"),
uli = result.get('uli')
), 201

else:
Expand All @@ -90,11 +92,95 @@ def registerLicensee():
), 201




### ADMIN METHODS ###
API_KEY = '_admin_token' #TODO: add real admin tokens

@app.route('/generate_licensees', methods=['POST'])
def generateLicensees():
post_data = request.get_json(force=True)
num_licensees = generate_licensees(post_data)
return jsonify(status=True,message=str(num_licensees) + ' generated!'), 201
api_key = post_data.get('token', None)
num_licensees = post_data.get('NumLicensees', None)

if api_key is None or api_key != API_KEY:
return jsonify(
status=False,
message='Invalid Access Token!'
), 403

num_licensees = generate_licensees(num_licensees)

return jsonify(
status=True,
message=str(num_licensees) + ' generated!'
), 201

@app.route('/find_licensee', methods=['POST'])
def getLicensee():
"""This method fetches ULIs by Id"""
post_data = request.get_json(force=True)
api_key = post_data.get('token', None)
uli = post_data.get('uli', None)

if api_key is None or api_key != API_KEY:
return jsonify(
status=False,
message='Invalid Access Token!'
), 403

if uli is None:
return jsonify(
status=False,
message='uli is required when making this request!'
), 400

licensee = find_licensee(uli)

if licensee:
return jsonify(
status=True,
message='ULI found!',
uli=uli
), 200
else:
return jsonify(
status=False,
message='uli: ' + uli + ' not found!'
), 404

@app.route('/remove_licensee', methods=['DELETE'])
def removeLicensee():
"""This method is primarily used for testing purposes and requires a token"""
post_data = request.get_json(force=True)

api_key = post_data.get('token', None)
uli = post_data.get('uli', None)

if api_key is None or api_key != API_KEY:
return jsonify(
status=False,
message='Invalid Access Token!'
), 403

if uli is None:
return jsonify(
status=False,
message='uli is required when making this request!'
), 400

licensee = remove_licensee(uli)

if licensee:
return jsonify(
status=True,
message='uli: ' + uli + ' deleted!'
), 200
else:
return jsonify(
status=False,
message='uli: ' + uli + ' not found!'
), 404


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion app/models.py
Expand Up @@ -6,7 +6,7 @@ class Member(Document):
MemberLastName = StringField(required=True)
MemberEmail = StringField(required=True)
LicenseInfo = ListField()
date_created = DateTimeField(default=datetime.datetime.utcnow)
ModificationTimestamp = DateTimeField(default=datetime.datetime.utcnow)

def to_json(self):
return {
Expand Down