lundi 4 octobre 2021

REST API coding exercise using Python, Flask/Postman

I just started an internship at a software company and I need help understanding and completing an assignment. I need to create an API with 4 endpoints with each method (Get, Delete, Pull, Put) using SOLID Principles. the song file has to have the info:

Song file fields: - ID – (mandatory, integer, unique) - Name of the song – (mandatory, string, cannot be larger than 100 characters) - Duration in number of seconds – (mandatory, integer, positive) - Uploaded time – (mandatory, Datetime, cannot be in the past.

The Endpoints are:

-(GET) Get API: - The route "<audioFileType/audioFileID>” will return the specific audio file The route “” will return all the audio files of that type

-(POST) Create API: The request will have the following fields: - audioFileType – mandatory, one of the 3 audio types possible - audioFileMetadata – mandatory, dictionary, contains the metadata for one of the three audio files (song, podcast, audiobook)

-(DELETE) Delete API:The route will be in the following format: “<audioFileType/<audioFileID”

-(PUT) Update API: The route be in the following format: “<audioFileType/audioFileID>” The request body will be the same as the upload

So far I have created a template based on some videos I've watched, but I need help completing each method with the song and using Postman.

The code goes like this so far:

SONGAPP.PY

from flask import Flask
import logging as logger
logger.basicConfig(level="DEBUG")

songapp = Flask(__name__)

if "__main__" == __name__:
    logger.debug("Starting App")
    from api import *
    songapp.run(debug=True, use_reloader=True)

INIT.PY

from flask_restful import Api
from songapp import songapp
from .Task import Task,

server = Api(songapp)

server.add_resource(Task, "/api/task")
server.add_resource()

TASK.PY

from flask_restful import Resource
import logging as logger

song_par = {"audioFileType" : "mp3"},{"audioMetadata" : {
"ID" : "0110",
"name":"my dreams",
"duration" : "210",
"upload time" : "sept 20th 2022"}}

class Task(Resource):

    def get(self):
        logger.debug("Inside get method")
        return {"message" : "inside get method"}, 200

def post(self, audioFileType, audioFileMetadata):
    logger.debug("Inside post method")
    return {"message": "inside post method"}, 200

def put(self, audioFileType, audioFileID):
    logger.debug("Inside put method")
    return {"message": "inside put method"}, 200

def delete(self):
    logger.debug("Inside delete method")
    return {"message": "inside delete method"}, 200

So far I've created the API template but I don't know how to proceed, so any help is appreciated :')




Aucun commentaire:

Enregistrer un commentaire