Skip to main content

"Fonctions d'utilisation d'API ReST."

Project description

Build Status License Language

Scrippy, my scrangourou friend

scrippy_api

REST API client for the Scrippy framework (https://codeberg.org/scrippy).

Requirements

Python modules

Required modules list

The modules listed below will be automatically installed.

  • requests
  • PyYAML
  • jsonschema

Installation

Manual

git clone https://codeberg.org/scrippy/scrippy-api.git
cd scrippy-api
python -m pip install -r requirements.txt
make install

With pip

pip install scrippy-api

Usage

The scrippy_api.api module provides the Client object that allows querying any REST API uniformly using the unique method Client.request().

The Client object has a single method Client.request() that accepts many parameters, most of which are optional. However, some optional parameters can become mandatory depending on the HTTP method used for the request and the use case. YMMV.

The Client.request() method always returns a requests.Response object (see documentation) that must be handled by the script.

Both parameter keys and values will be automatically encoded when necessary.

In case of error (HTTP code != 200), the client exits with an 1 return code and the error is logged as critical. This behavior can be inhibited during the client instantiation by setting the exit_on_error parameter to False:

from scrippy_api.api import Client
client = Client(exit_on_error=False)

In this case, any encountered errors will appear in the log file as warning.

The verification of the remote server SSL certificate can be disabled by passing the optional verify parameter of the Client object to False:

from scrippy_api.api import Client
client = Client(exit_on_error=True, verify=False)

Note: Disabling certificate verification is discouraged because it presents real security risks.

Parameters

Parameters of the request method of the Client object:

Parameter Type Description Default value
params Dictionary Applicable to all HTTP methods. Each key/value pair will be concatenated to the URL. None
cookies Dictionary Cookies to be sent with the request None
timeout integer Waiting time before interrupting the connection None
headers Dictionary Headers to be sent with the request None
proxies List List of proxy servers to use for the connection None
auth Tuple Username and password for BASIC AUTH authentication
data Dictionary Data to be sent with the request. Not applicable with GET method None
json Dictionary Data in JSON format to be sent with the request. Not applicable with GET method. Use when data and file are not specified None
files Dictionary Files to be uploaded in multipart. The dictionary takes the form {<file name>: <file>} None

Implemented HTTP methods:

HTTP method Description
GET Retrieve a resource or list of resource URIs
POST Create a resource
PUT Replace or create a resource
PATCH Update a resource or create it if non-existent
DELETE Delete a resource

Examples

URL with Parameters
from scrippy_api.api import Client
params = {"name": "Luiggi Vercotti", "password": "dead/parrot"}
client = Client()
response = client.request(method="GET", url="https://montypython.org/user", params=params)

The called URL will be:

https://montypython.org/user?name=Luiggi+Vercotti&password=dead%2Fparrot
Basic Authentication (BASIC AUTH)

Basic authentication with the following credentials:

  • Username: Luiggi Vercotti
  • Password: dead/parrot
from scrippy_api.api import Client
auth = ("Luiggi Vercotti", "dead/parrot")
client = Client()
response = client.request(method="POST", url="https://montypython.org", auth=auth)
Sending Data

Creating the Luiggi Vercotti user with the password dead/parrot:

from scrippy_api.api import Client
data = {"name": "Luiggi Vercotti", "password": "dead/parrot"}
client = Client()
response = client.request(method="POST", url="https://montypython.org/user", data=data)
File Upload

Upload of the two files ./images/dead_parrot.png and ./images/flying_circus.mp4:

from scrippy_api.api import Client
files = {"dead_parrot.png": open("./images/dead_parrot.png", "rb"), "flying_circus.mp4": open("./images/flying_circus.mp4", "rb")}
client = Client()
response = client.request(method="POST", url="https://montypython.org/upload", data=data)
Resource Modification

Replaces the password of the Luiggi Vercotti user

from scrippy_api.api import Client
auth = ("Luiggi Vercotti", "dead/parrot")
data = {"password": "live/parrot"}
params = {"name": "Luiggi Vercotti"}
client = Client()
response = client.request(method="PATCH",
                          url="https://montypython.org/user",
                          params=params,
                          data=data)
File Download
from scrippy_api.api import Client
url = "https://monthy.python/inquisition.zip"
local_dir = "/home/luiggi.vercotti"
local_filename = "spanish_inquisition.zip"
client = Client()
if client.download(url, local_dir, local_filename):
  print("No one expects the Spanish inquisition")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scrippy-api-1.1.83.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

scrippy_api-1.1.83-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file scrippy-api-1.1.83.tar.gz.

File metadata

  • Download URL: scrippy-api-1.1.83.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for scrippy-api-1.1.83.tar.gz
Algorithm Hash digest
SHA256 c98e8a88b19457df71642bd6b8fb4bc63ddf0c57845a9e4fb34cbecf273f5b6c
MD5 5670973b5cef6ed363c3743710a60cfd
BLAKE2b-256 400780ff7e84b98cec4c452cce30b5577d34ac7e235a3a35d0721e58f16f58e5

See more details on using hashes here.

File details

Details for the file scrippy_api-1.1.83-py3-none-any.whl.

File metadata

File hashes

Hashes for scrippy_api-1.1.83-py3-none-any.whl
Algorithm Hash digest
SHA256 ae52d890080d278deb7ad01119ed55865a357a00731b7289596a340bc0c5be73
MD5 bc8be4e26d63e9ccf06e860f3417608e
BLAKE2b-256 3b31f13d317063aa6ed7c5aaad5f4109a3455cfb2f42c01316464d4e8489a7e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page