3

I have data stored in the file system (normalized across multiple small files) and I have written python functions to read/write data from the file system. Read API returns object of type Job. Write API expects an object of type Job to be passed as an argument.

def get_jobs(starttime, endtime):
  ''' Reads and returns jobs that ran between starttime and endtime interval '''


def put_job(job):
  ''' Persists Job object to a file system '''


class Job:
    def __init__(self, name, key, starttime, endtime):
        self.name = name 
        self.key = key
        self.starttime = starttime
        self.endtime = endtime

Now I want to expose these functions via a web server. I would prefer exposing GraphQL APIs with Django.

Questions:

  • Is Django/Django REST framework a right choice for this? I am new to Django and GraphQL.
  • Django models seem to be tightly coupled with Databases. Will I have to create another Job model class, and create it using Job returned by read_jobs function? If yes, how can I create a simple web application from here?

Note:

  • Job object is a three-level nested object and has many attributes/properties. For the demonstration purpose, I kept only four attributes/properties in the question here.
  • I would prefer Django because my application at this stage might look a bit small but over time it will grow and I have lots of features that I need to add. I am specifically looking for solutions with django-graphene based application without using SQLite DB or without assuming that the data is getting fetched from some database. I want to leverage my persistence API methods for querying data from the file system.
ThinkGeek
  • 4,749
  • 13
  • 44
  • 91

1 Answers1

3

Django could be a (good but heavier) solution, but here is simpler solution using Flask:

from flask import Flask, jsonify

app = Flask(__name__)

class Job:
    def __init__(self, name, key, starttime, endtime):
        self.name = name 
        self.key = key
        self.starttime = starttime
        self.endtime = endtime

@app.route("/get", methods=['GET'])
def get_jobs(starttime, endtime):
    ''' Reads and returns jobs that ran between starttime and endtime interval '''
    jobs = read_data(starttime, endtime) # your read_data() method
    return jsonify({'jobs': jobs})

@app.route("/put", methods=['POST'])   # or methods=['PUT']
def put_job(request):

    # access your data trough the request object:
    job_name = request.args.get('name', '')
    job_key = request.args.get('key', '')

    # or get it in json
    job_data = request.json

    write_data(Job.from_json(job_data))

I used Json here because I'm more confortable with it, but if GraphQL is important for you, I recommend to you the Graphene-Python library.

There is also a project of integration of Graphene with Flask

olinox14
  • 6,177
  • 2
  • 22
  • 39
  • Thanks for the help. I would still prefer Django because my application at this stage might look a bit small but over time it will grow and I have lots of features that I need to add. I am specifically looking for solutions that create a django-graphene based application without using SQLite DB or without assuming that the data is getting fetched from some database. I want to leverage my persistence API methods for querying data from the file system. – ThinkGeek Jun 20 '19 at 18:58
  • Here it is: . With Django you won't avoid a database, at least for the User and Groups... But nothing prevent you of writing views and handling requests without using this database. – olinox14 Jun 21 '19 at 09:10