<file_sep>""" Main entry point for service """

from flask import Flask, jsonify, request
from Util import database_upload, local_upload, print_to_screen

app = Flask(__name__)
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True

@app.route("/")
def root():
    return jsonify({
                "message": "welcome to my file microservice",
                "author": "<NAME>",
                "company": "availity"
            }), 200

@app.route("/add-file", methods=["POST"])
def file_intake():
    data = dict()
    provider_codes = {
        "provider1" : "database",
        "provider2" : "local",
        "provider3" : "print"
    }
    files = request.files
    provider_type = request.args.get("provider")

    if len(files) == 0:
        return jsonify({"error": "files not found"}), 404

    print()
    print("Thank you for the files.")
    print("Provider type of", provider_type)

    for f in files:
        data[f] = request.files[f]

    if provider_type == "provider1": database_upload(data)
    if provider_type == "provider2": local_upload(data)
    if provider_type == "provider3": print_to_screen(data)

    return jsonify({"type": provider_codes[provider_type]}), 200


if __name__ == "__main__":
    app.run(debug=False, use_reloader=True)<file_sep>""" Utility for saving files """

import os
from tinydb import TinyDB, Query
from flask import jsonify

def database_upload(data):
    db = TinyDB("db.json")

    for d in data:
        db.insert({
            d: data[d].read().decode("latin-1")
        })

    print()
    print("Files uploaded to database.")

def local_upload(data):
    current_path = os.path.dirname(os.path.abspath(__file__))
    location = current_path + "/local_storage/"

    for d in data:
        save_location = location + d
        with open(save_location, 'w') as f:
            f.write(data[d].read().decode("latin-1"))

    print()
    print("Files saved to local storage.")

def print_to_screen(data):
    print()
    print("Printing data to screen...")

    for d in data:
        print("File:", d)
        print("Data:\n", data[d].read().decode("latin-1"))<file_sep>## File microservice

Python 3. Used Flask for the API and TinyDB for the database - for a lightweight architecture.

"Write an app in your programming language of choice that takes as input a list of files and a provider parameter. Depending on the provider parameter either write the files to a database, the file system, or print the files to console or popup window. The parameter values are “provider1” for database, “provider2” for file system, “provider3” for console/popup."

#### Install

To install dependecies, make sure pip is installed and run the following command.
  ```pip install requirements.txt```

#### Run the app
To run, use the following command.
  ```
  $ python app.py

   * Serving Flask app "app" (lazy loading)
   * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
   * Debug mode: off
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
 ```

Use the url to test locally. http://127.0.0.1:5000/

To test, use the provider parameters below in the request URL. 

Use a header of Content-Type=multipart/form-data

Attach files to the body of your request as formdata. Example of the files in the body of request, in Postman.

<img src="postman.png" />

- Database
  ``` http://1172.16.31.10:5000/add-file?provider=provider1 ```


- Local
  ``` http://127.0.0.1:5000/add-file?provider=provider2 ```

- Print
  ``` http://127.0.0.1:5000/add-file?provider=provider3 ```

