Formation

Formation

  • Docs

›Introduction

Introduction

  • About Formation
  • Using Formation with Requests
  • Structured Queries with Attrs

Examples

  • Posting Data
  • Production Ready Clients
  • Scraping Web Sites

Middleware

  • Accept
  • Circuit Breaker
  • Context Logger
  • Context
  • Duration
  • Request ID
  • Retry
  • Timeout

Building Middleware

  • A Simple Middleware
  • Thread Safety and Concurrency

Structured Queries with Attrs

Attrs is a popular and lightweight data class implementation for Python.

With Formation and its sister-project attrs-serde you can build an HTTP client that treats all queries as structured, strongly typed, queries. Here's an example:

from formation.for_requests import client, html_response
from formation.middleware import request_logger
from attr import attrib, attrs
from attrs_serde import serde
import structlog

@serde
@attrs
class Query(object):
    query = attrib(metadata={"to": ["q"]})

@client
class Google(object):
    base_uri = "https://google.com"
    middleware = [request_logger(structlog.getLogger())]
    response_as = html_response

    def search(self, text):
        return self.request.get("/", params=Query(text))

if __name__ == "__main__":
    google = Google()
    (xml, _code, _headers) = google.search("larry page")
    print(xml.xpath("//title/text()"))

Formation client will detect Query being an attrs-serde friendly class instance and will serialize it into a query string automatically.

To see how you can build even more sophisticated clients, learn more about attrs-serde.

← Using Formation with RequestsPosting Data →
Formation
Docs
Getting StartedExamplesMiddleware
Community
Stack OverflowTwitter
More
GitHubStar
Copyright © 2019 Dotan Nahum