Home

Awesome

MicroRESTCli is a micro HTTP JSON REST Web client based on MicroWebCli for MicroPython (used on ESP32 and Pycom modules)

HC²

Very easy to integrate and very light with 2 files only :

Simple but effective :

Using microRESTCli main class :

NameFunction
ConstructorrCli = MicroRESTCli(baseUrl, user=None, password=None, token=None)
Makes a GET requestrCli.GET(resUrl, fileToSave=None, progressCallback=None)
Makes a POST requestrCli.POST(resUrl, o, fileToSave=None, progressCallback=None)
Makes a PUT requestrCli.PUT(resUrl, o, fileToSave=None, progressCallback=None)
Makes a PATCH requestrCli.PATCH(resUrl, o, fileToSave=None, progressCallback=None)
Makes a DELETE requestrCli.DELETE(resUrl, fileToSave=None, progressCallback=None)
Gets status code of last callrCli.GetLastStatusCode()
Gets status message of last callrCli.GetLastStatusMessage()
Gets json response object of last callrCli.GetLastJSONResponse()
NameProperty
Gets or sets the connection timeout in secondesrCli.ConnTimeoutSec
Gets or sets the dict headers of http requestsrCli.Headers

Simple example :

from microRESTCli import MicroRESTCli

rCli = MicroRESTCli('https://my-web-api.io/rest', user='toto', password='blah123')

invoice = rCli.GET('invoice/%d' % 42)

try :
  u = { 'name':'geek', 'age':18 }
  resp = rCli.POST('user', u)
  print('User added')
except :
  err = rCli.GetLastJSONResponse()
  if err :
    print("%s API request failed (%s)" % (err.name, err.reason))

Example of saving response to a file :

from microRESTCli import MicroRESTCli

def progressCallback(microWebCli, progressSize, totalSize) :
  if totalSize :
    print('Progress: %d bytes of %d downloaded...' % (progressSize, totalSize))
  else :
    print('Progress: %d bytes downloaded...' % progressSize)

rCli        = MicroRESTCli('https://my-web-api.io/rest', token='jIud87jzIsUzj3=')
filename    = '/flash/invoice.pdf'
contentType = rCli.GET('invoice/%d/pdf' % 42, filename, progressCallback)
print('File of type "%s" saved to "%s"' % (contentType, filename))

Using microRESTCli helpers :

NameFunction
Converts timestamp to json datetimeMicroRESTCli.Timestamp2JsonDT(timestamp=None)
Converts json datetime to timestampMicroRESTCli.JsonDT2Timestamp(jsonDT)

By JC`zic for HC² ;')

Keep it simple, stupid :+1: