Skip to content

Backend Utils Advanced Usage

ZABBIX edited this page Mar 24, 2024 · 1 revision

Backend Advanced Usage πŸ’‘

Pypulse is equipped with the capability to seamlessly connect to a backend API and facilitate integration in the most straightforward manner possible.

Backend Configuration πŸ”§

Example of configuration:

import os

from pypulse import Window, Aplication
from pypulse.Template import Template
from pypulse.Controller import Controller, BackendInstance

...

# Active Backend Controller
Controller.BACKEND_CONTROLLER = True

# Setting Backend Type
Controller.BACKEND_TYPE = "api-restful"

# Backend Controller
Controller.BACKEND_AUTH = {
    "type": "basic",
    "bearer": True,
    "bearer_header": "Authorization",
    "bearer_key": "Bearer",
    "url": "https://api.openai.com/v1/",
}

Controller.CHECK_AUTH_URL = "completions"

# Instancing backend
BackendInstance()

...

Backend Types πŸ› οΈ

  • api-restful

Backend Auth πŸ”

  • type
    • basic
      • Basic authentication is a simple authentication scheme built into the HTTP protocol
  • bearer
    • False or True
      • Bearer authentication is a method used to secure API endpoints by requiring a valid access token in the request header. In this approach, the client includes a bearer token in the "Authorization" header of the HTTP request to authenticate itself with the server.
  • bearer_header
    • String denoting the header for the bearer key.
      • {bearer_header}:{bearer_key} Token
  • bearer_key
    • String indicating the token prefix.
      • {bearer_key} Token
  • url
    • Fundamental API Base URL

Tuning parameters πŸŽ›οΈ

These parameters are employed to customize the backend according to the API requirements, all of this parameters are optional.

  • LOGIN_URL
  • REGISTER_URL
  • LOGOUT_URL
  • LOGIN_TOKEN_KEY
  • CHECK_AUTH_URL
  • AUTH_FAIL_STATUS_CODE

Auth methods πŸ”’

Token πŸ”‘

The CHECK_AUTH_URL configuration must be set in YourApp.py.

from pypulse.View import view
from pypulse.Template import RenderTemplate, Redirect
from pypulse.Controller import set_manual_bearer, authenticated


@view(name="home", path_trigger="/")
def home(request):
    if not authenticated():
       return Redirect("/login")
    return RenderTemplate("index.html")


@view(name="login", path_trigger="/login")
def login(request):
    token = "auth token"
    set_manual_bearer(token)
    if authenticated():
       return Redirect("/")
    return RenderTemplate("login.html")
  • set_manual_bearer
    • This function allows you to set an access token for the API.
  • authenticated
    • This function enables you to verify your access to the API.

Username & Password πŸ‘€πŸ”

The LOGIN_URL configuration is essential and should be set in YourApp.py. Optionally, you may configure REGISTER_URL and LOGOUT_URL based on the methods you are utilizing.

from pypulse.View import view
from pypulse.Template import RenderTemplate, Redirect
from pypulse.Controller import authenticated, login, logout


@view(name="home", path_trigger="/")
def home(request):
    if not authenticated():
       return Redirect("/login")
    return RenderTemplate("index.html")


@view(name="login", path_trigger="/login")
def login(request):
    is_login = login('username', 'pass')
    if is_login():
       return Redirect("/")
    return RenderTemplate("login.html")


@view(name="logout", path_trigger="/logout")
def logout(request):
    if authenticated():
        logout()
    return Redirect("/login")
  • login
  • logout
  • ***register

Models πŸš€

Models serve as the interface for interacting with data.

Get Data from API

...
from pypulse.Model import Model, String

class Menu(Model):
    name = String(max_length=100)
    icon = String(max_length=255)

    class Meta:
        target = 'manage/menus/'


@view(name="home", path_trigger="/")
def home(request):
    if not authenticated():
       return Redirect("/login")

    menus = Menu.view.all()
    return RenderTemplate("index.html", {"menus": menus})
...

POST Data from API

...
from pypulse.Model import Model, String

class Menu(Model):
    name = String(max_length=100)
    icon = String(max_length=255)

    class Meta:
        target = 'manage/menus/'
        method = "post"
        body = None


@view(name="home", path_trigger="/")
def home(request):
    if not authenticated():
       return Redirect("/login")
   
    new_menu = {"name": "new", "ico": "new_ico"}
    menus = Menu.body(new_menu)
    menus = menus.view.all()

    return RenderTemplate("index.html", {"menus": menus})
...

Model Meta πŸ”

  • target
    • Path to the method you are calling
  • method
    • By default, the method is set to 'get' but you have the flexibility to configure the appropriate method for the endpoint.
  • body
    • This is employed to transmit data to the API.

Model methods πŸ”§πŸ”

  • view
    • all
      • Retrieve all data without applying any filters.
    • ***filter:
      • Retrieve data with filtering.

*** : Not implemented.