Package: pantam
pantamPantam is an extensible, ultra lightweight, Python framework for creating RESTful microservices.Features include:built on top ofStarletteserved usingUvicornsimple REST routingsegregated business logicconvention over configurationparadigmexpressive logging (decent error reporting!)live reloading in developer modeNeed to build a Javascript microservice?Check out BantamGetting startedOur goal with Pantam is reduce the work bootstrapping microservices.With Pantam you can create a basic REST API in 5 minutes or less.ExamplesDomain segregated exampleEvent based exampleInstallationWe recommend usingPoetrywithPyenvfor dependency and environment management (this documentation will use Poetry throughout) but if you preferpiporcondathen crack on!If you haven't done so already, setup your python environment and Poetry project:% pyenv install 3.8.2
% pyenv local 3.8.2
% poetry initNB: if you specify pantam as a package during the setup process, runpoetry installand skip the next step.Now install the Pantam package:% poetry add pantamOnce you have installed Pantam you can initialise your app.% poetry shell
% pantam initFollow the CLI instructions and then start building your microservice!SetupPantam expects the following folder structure:| main.py       // can have any name, it's where you run your app
| actions        // where your domain logic sits
|  |  index.py   // primary logic lives here (might be all you need)
|  |  other.py   // add as many other "domains" as you like (optional)In the root levelmain.pyfile add the following to run Pantam:from pantam import Pantam

pantam = Pantam()

app = pantam.build()

if __name__ == "__main__":
    pantam.run("main", "app")In theactionsfolder create the following files.actions/index.pyclass Index:
  def fetch_all(self):
    pass

  def fetch_single(self, request):
    pass

  def create(self, request):
    pass

  def update(self, request):
    pass

  def delete(self, request):
    passactions/other.pyclass Other:
  def fetch_single(self, request):
    pass

  def create(self, request):
    passThe setup above will make the following routes available:GET      /            // Index.fetch_all()
GET      /{id}         // Index.fetch_single()
POST     /            // Index.create()
PATCH    /{id}         // Index.update()
DELETE   /{id}         // Index.delete()

GET      /other/{id}   // Other.fetch_single()
POST     /other       // Other.create()And that's you ready to go!DevelopmentConfigure the application to live reload on a given port as follows:from pantam import Pantam

pantam = Pantam(debug=True)

app = pantam.build()Then serve the app:% pantam serve --devYour application will be served onhttp://localhost:5000NB: Pantam looks for aPANTAM_ENVenvironment variable when serving the application and defaults todevelopmentif no variable is set.ProductionTo serve your microservice in production use:% pantam serveYour application is served athttp://your-host:5000You can change the port number via the configuration options.NB: in production you should set aPANTAM_ENV=productionenvironment variable..pantamrc.jsonAfter runningpantam inityou will have a.pantamrc.jsonfile in your directory with some CLI config options like this:{
  "actions_folder": "actions",
  "entrypoint": "example.py",
  "port": 5000,
  "dev_port": 5000
}The.pantamrc.jsonfile provides configuration options for the CLI. You only need to change it if you change your main file (entrypoint) or rename your actions folder.Add New RoutesTo add a new action (resource) you can either create a new file in the actions folder or use the CLI to make the file for you:% pantam action index.pyYou can add the standard methods (fetch_all,fetch_single,create,update,delete) to an action class which will automatically create the standard routes.If you'd like to create custom methods for your action class you can create custom getters like this:// GET -> /custom-method/
def get_custom_method(self):
  # your code hereAnd custom setters like this:// POST -> /custom-method/
def set_custom_method(self, request):
  print(request)
  # your code hereFrom version 0.1.0 onwards you can also create adomethod, which is a custom post method that also expects an identifier as part of the url.// POST -> /custom-method/{id}
def do_custom_method(self, request):
  print(request)
  # your code herePantam will ignore methods that are not "standard" methods or do not start withgetorset. However if you want toensurethat your method will be ignored you can prefix the method with a double underscore, like this:def __my_hidden_method(self):
  // do something secretCreating ResponsesTo create a response, make use of theStarlette response API, you can import all responses from starlette or import common responses from Pantam directly, including:JSONResponse,HTMLResponse,PlainTextResponse,FileResponse,RedirectResponse.Here are a few examples:from pantam import PlainTextResponse

class YourClass:
  def fetch_all():
    return PlainTextResponse("This is fetch all!")from pantam import JSONResponse

class YourClass:
  def fetch_all():
    return JSONResponse([{ "content": "example" }])Changing status code is also simple.def fetch_all(self):
  return PlainTextResponse("This is fetch all!", status_code=404)Adjusting headers can also be achieved.def fetch_all(self):
  headers = {
    "Cache-Control": "no-cache"
  }
  return PlainTextResponse("This is fetch all!", headers=headers)Configuration OptionsFor advanced configuration pass options in when instantiating Pantam.from pantam import Pantam

pantam = Pantam(debug=True) # add options as below

app = pantam.build()

if __name__ == "__main__":
    pantam.run("main", "app")You can set the following options:debug:boolTurns debugging mode on for Pantam (and underlying Starlette). Provides more verbose error messages. Should be off in production.Default: Falseactions_folder:stringThe folder that contains your action files.Default: "actions"actions_index:stringThe primary action file in your action folder.Default: "index"on_shutdown:functionFunction called prior to shutdown.Default: NoneDebuggingIf you're struggling to debug and issue and unsure what routes Pantam has created for you, set thedebugoption to True.from pantam import Pantam

pantam = Pantam(debug=True)

app = pantam.build()Also check trailing slashes in your urls, these are important.In the example below the urltest/1andtest/custom-methodboth triggerfetch_single()but the urltest/custom-method/(with the trailing slash) triggersget_custom_method().// actions/test.py

// GET -> test/custom-method
// GET -> test/{id}
fetch_single()

// GET -> test/custom-method/
get_custom_method()ContributionWe welcome feedback, suggestions and contributions.If you have an idea you want to discuss pleaseopen an issue.LicensesFree for personal and commerical use under theMIT License