Skip to main content

Generate dlt Python clients from OpenAPI

Project description

dlt-init-openapi

dlt-init-openapi generates dlt pipelines from OpenAPI 3.x documents/specs using the dlt rest_api verified source. If you do not know dlt or our verified sources, please read:

This generator does not support OpenAPI 2.x FKA Swagger. If you need to use an older document, try upgrading it to version 3 first with one of many available converters.

Prior work

This project started as a fork of openapi-python-client. Pretty much all parts are heavily changed or completely replaced, but some lines of code still exist and we like to acknowledge the many good ideas we got from the original project :)

Features

The dlt-init-openapi generates code from an OpenAPI spec that you can use to extract data from a rest_api into any destination (e.g. Postgres, BigQuery, Redshift...) dlt supports.

Features include

Setup

You will need Python 3.9 installed, as well as poetry to install dependencies.

# 1. Checkout this repository locally
$ git clone git@github.com:dlt-hub/dlt-init-openapi.git

# 2. Install required poetry dependencies
$ poetry install

# 3. Start the poetry shell
$ poetry shell

Basic Usage

Let's create an example pipeline from the PokeAPI spec. You can point to any other OpenAPI Spec instead if you like.

# 1. Run the generator with the dlt-init-openapi init command:
$ dlt-init-openapi init pokemon --url https://raw.githubusercontent.com/cliffano/pokeapi-clients/ec9a2707ef2a85f41b747d8df013e272ef650ec5/specification/pokeapi.yml

# 2. You can now pick the endpoints you need from the popup

# 3. After selecting your pokemon endpoints and hitting Enter, your pipeline will be rendered

# 4. Go to the created pipeline folder and run your pipeline
$ cd pokemon-pipeline
$ PROGRESS=enlighten python pipeline.py # we use enlighten for a nice progress bar :)

# 5. Print the pipeline info to console to see what got loaded
$ dlt pipeline pokemon_pipeline info

# 6. You can now also install streamlit to see a preview of the data
$ pip install pandas streamlit
$ dlt pipeline pokemon_pipeline show

What will be created?

When you run the init command above, the following files will be generated:

  • ./pokemon-pipeline - a folder containing the full project.
  • ./pokemon-pipeline/pipeline.py - a file which you can execute to run your pipeline.
  • ./pokemon-pipeline/pokemon/__init__.py - a file that contains the generated code to connect to the PokeApi, you can inspect this file and manually change it to your liking or to fix incorrectly generated results.
  • ./pokemon-pipeline/.dlt - a folder with the config.toml. You can add your secrets.toml with credentials here.
  • ./pokemon-pipeline/rest_api - a folder that contains the rest_api source from our verified sources.

If you re-generate your pipeline, you will be prompted to continue if this folder exists. If you select yes, all generated files will be overwritten. All other files you may have created will remain in this folder.

CLI commands

$ dlt-init [OPTIONS] COMMAND [ARGS]...
# example:
$ dlt-init init pokemon --path ./path/to/my_spec.yml

Options:

  • --version: Print the version and exit [default: False]
  • --help: Show this message and exit.

Commands:

  • init: Generate a new dlt rest_api source

dlt-init-openapi init

Generate a new dlt rest_api source

Usage:

$ dlt-init-openapi init pokemon --path ./path/to/my_spec.yml

Options:

  • --url TEXT: A url to read the OpenAPI JSON or YAML file from
  • --path PATH: A path to read the OpenAPI JSON or YAML file from locally
  • --output-path PATH: A path to render the output to
  • --config PATH: Path to the config file to use (see below)
  • --no-interactive: Skip endpoint selection and render all paths of the OpenAPI spec.
  • --loglevel: Set logging level for stdout output, defaults to 20 (INFO).
  • --help: Show this message and exit.

Config options

You can pass a path to a config file with the --config PATH argument. To see available config values, go to https://github.com/dlt-hub/dlt-init-openapi/blob/master/dlt_init_openapi/config.py and read the information below each field on the Config class.

The config file can be supplied as json or yaml dictionary. For example to change the package name, you can create a yaml file:

# config.yml
package_name: "other_package_name"

And use it with the config argument:

$ dlt-init-openapi init pokemon --url ... --config config.yml

Implementation notes

  • OAuth Authentication currently is not natively supported, you can supply your own
  • Per endpoint authentication currently is not supported by the generator, only the first globally set securityScheme will be applied. You can add your own per endpoint if you need to.

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

dlt_init_openapi-0.0.5a0.tar.gz (35.7 kB view hashes)

Uploaded Source

Built Distribution

dlt_init_openapi-0.0.5a0-py3-none-any.whl (46.4 kB view hashes)

Uploaded Python 3

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