Backend created with TypeScript, Express, and MongoDB.
CLI Application created with Python standard library, Requests, and Click.
Pre-requisites: Need Python and Node/NPM installed.
Clone the repo
git clone https://github.com/EricGip/APIChallenge.git
Cd into the root directory of the repo (where Package.json is located) and install dependencies:
npm install
pip install -U click requests
IMPORTANT
Please fill in the fields inside .env.example
with the credentials Paula has sent you.
Rename .env.example
to .env
In the same root directory, run the CLI application and start the connection to the database with (Option 1).
python main.py
Open another terminal instance in the same directory and run python main.py
again to invoke the CALL / GET APIs through menu choices.
Menu Choices:
- Connect to database
- Access the GET API
- Access the POST API
q. Quit
If you're using windows and you're getting an error of FileNotFoundErorr: [WinError2]
, in the same root directory, please enter:
npm run start
then in another terminal instance, run python main.py
, options 2 and 3 should function properly.
Please use the CLI application to access the APIs, but here is how it works underneath the hood.
Base URL: localhost:8080/logs/
localhost:8080/logs/
- returns all logs available/
localhost:8080/logs/:id
- returns specific log with matching ID.
Parameters: id - _id field autogenerated by MongoDb.
localhost:8080/logs/
- uploads an array of objects / logs into the database.
Headers: Content-type: application-json.
Expects array of objects/dictionaries.
- Chose to create a REST API because it is the most common and I should focus on the fundamentals before I try deviating.
- I am aware of SOAP, GraphQL, gRPC, Websockets, and Webhooks.
- Chose to do this in Python with Requests, could have chosen language agnostic tool such as cURL and wrote CLI app in another language.
- JS/NPM and Axios/Fetch, Bash + cURL.
- Can think of 3 places we could have sorted the data:
- In the backend during MongoDB API call, there is most likely an ORM function for this.
- This limits us if we are extending the application, it is better to manipulate the data after this step.
- In the backend after MongoDB API call with TypeScript.
- In the CLI app after MongoDB API call with Python.
- Chose this because I am most comfortable with data structures & algorithms in Python.
- Unless there is something more efficient happening under the hood of either language, they should be around the same complexity of O(n log n).
- In the backend during MongoDB API call, there is most likely an ORM function for this.
Since we are posting data, we need a persistent store and credentials for the database. I can think of doing this 5 ways.
- Sending db information in an email that's hopefully encrypted.
- I ended up using this because it's the most practical for our test app and done in the industry.
- Hard coding credentials in .env and leaving it in repository.
- This is irresponsible to do while interviewing for a security role
- Set up database with local MongoDb instance.
- User may be on limited connection and need to download MongoDB, would require more overhead from the user.
- Dockerize entire app, instantiate MongoDb within the container.
- User might need to have Docker Desktop installed (> 1gb) and may be on a slower / limited connection. Also, docker desktop takes a ton of RAM even just processing in the background.
- Dockerize app, host on serverless backend cloud app (AWS lambda or GCP cloud run should be enough), then call API with the CLI tool.
- Requires more overhead than the project itself.
- First thing we need to do is to connect to the database, have a script that does
npm run start
to initialize connection to server. - prompt user "what do you want to do?" get or post request
- if get, "sort objects descending by date and display results". Need way to present data nicer than just objects too.
- probably for loop, set into array / some kind of nicer text
- if post, should ask how many objects they want to enter
if one, just prompt 3 questions of: name, type, description
- Redundant, can just always ask if user wants to add more.- if multiple: just ask 4 questions: name, type, descrption, and add another? (Y/n)
- if y, repeat questions, append object to array
- need to make sure it appends with { } in front to tell db it's an object
- if n, send post request with the array as the body.
- if y, repeat questions, append object to array
- if get, "sort objects descending by date and display results". Need way to present data nicer than just objects too.
1. DELETE and PUT routes to edit and delete data. Extended App to ensure I remember how the app works
2. Data validation
3. Api key
4. Docker so user doesn’t have to npm install and pip
Resources:
https://www.mongodb.com/compatibility/using-typescript-with-mongodb-tutorial
https://click.palletsprojects.com/en/8.1.x/api/
https://requests.readthedocs.io/en/latest/user/quickstart/
https://docs.python.org/3/library/subprocess.html
https://realpython.com/sort-python-dictionary/
https://www.w3schools.com/python/ref_requests_post.asp