Skip to main content

Automated Testing and Documentation for your REST API

Project description

ScanAPI

A library for your API that provides:

  • Automated Testing
  • Automated Documentation

How to install

$ pip install scanapi

How to use

Create an API spec file api.yaml file in the root of your project and list the API's endpoints.

api:
  base_url: https://jsonplaceholder.typicode.com/
  endpoints:
    - namespace: posts
      path: /posts
      requests:
        - name: list_all
          method: get
        - name: details
          method: get
          path: /1

To run the requests and create the doc, run:

$ scanapi --help
Usage: scanapi [OPTIONS]

  Automated Testing and Documentation for your REST API.

Options:
  -s, --spec-path PATH
  -d, --docs-path TEXT
  --log-level [DEBUG|INFO|WARNING|ERROR|CRITICAL]
  --help                          Show this message and exit.

By default, the doc will be available in the docs.md file.

Available Methods

You can run these methods:

  • GET
  • POST
  • PUT
  • DELETE

Configuration

If you want to configure scanapi, you can do it by creating a file .scanapi.yaml in the root of your project.

spec_path: api.yaml
docs_path: docs.md

Headers

api:
  base_url: https://api.thecatapi.com/v1
  headers:
    x-api-key: DEMO-API-KEY
    Content-Type: application/json
  endpoints:
    - namespace: votes
      path: /votes
      requests:
        - name: list_all
          method: get

Query Parameters

api:
  base_url: http://api.openweathermap.org/data/2.5
  params:
    APPID: <INSERT_YOUR_API_KEY_HERE>
  endpoints:
    - namespace: weather
      path: /weather
      requests:
        - name: city
          method: get
          params:
            q: Rio de Janeiro

Body

In a post request you can add a body:

api:
  base_url: https://api.thecatapi.com/v1
  headers:
    x-api-key: DEMO-API-KEY
    Content-Type: application/json
  endpoints:
    - namespace: votes
      path: /votes
      requests:
        - name: vote
          method: post
          body:
            image_id: asf2
            value: 1
            sub_id: demo-d4332e

Environment Variables

You can use environment variables in your API spec file:

api:
  base_url: ${BASE_URL}
  headers:
    Content-Type: application/json
  endpoints:
    - namespace: posts
      path: /posts
      requests:
        - name: list_all
          method: get
        - name: details
          method: get
          path: /1

And in the config file .scanapi.yaml set their values:

env_vars:
  BASE_URL: https://jsonplaceholder.typicode.com/

Chaining Requests: Custom Vars + Python Code

api:
  base_url: https://jsonplaceholder.typicode.com/
  headers:
    Content-Type: application/json
  endpoints:
    - namespace: posts
      path: /posts
      requests:
        - name: list_all # posts_list_all
          method: get
          vars:
            post_id: ${{responses['posts_list_all'].json()[1]['id']}} # should return id 2
        - name: details # posts_details
          method: get
          path: ${post_id}

Nested Endpoints

api:
  base_url: https://jsonplaceholder.typicode.com/
  headers:
    Content-Type: application/json
  endpoints:
    - namespace: posts
      path: /posts
      requests:
        - name: list_all # posts_list_all
          method: get
      endpoints:
        - namespace: comments
          path: /1/comments
          requests:
            - name: comments # posts_details_comments
              method: get # https://jsonplaceholder.typicode.com/posts/1/comments

Hiding sensitive information

If you want to ommit sensitive information in your generated documentation, you can configure it in the .scanapi.yaml file. For now, it is only working for headers.

docs:
  hide:
    headers:
      - Authorization

You can find more examples here.

Available Keys

KEY Description Type Scopes
api It is reserver word that marks the root of the specification and must not appear in any other place dict root
body The HTTP body of the request dict request
base_url The API’s base URL string api
endpoints It represents a list of API endpoints list api, endpoint
headers The HTTP headers dict api, endpoint, request
method The HTTP method of the request (GET, POST, PUT or DELETE) string request
name An identifier string endpoint, request
path A part of the URL path that will be concatenated with the base URL and possible other paths string endpoint, request
requests It represents a list of HTTP requests list api, endpoint
vars Key used to define your custom variables to be used along the specification dict request
${custom var} A syntax to get the value of the custom variables defined at key vars string request - after vars definition
${ENV_VAR} A syntax to get the value of the environment variables defined at .scanapi file string api, endpoint, request
${{python_code}} A syntax to get the value of a Python code expression string requests

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

scanapi-0.0.9.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scanapi-0.0.9-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file scanapi-0.0.9.tar.gz.

File metadata

  • Download URL: scanapi-0.0.9.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for scanapi-0.0.9.tar.gz
Algorithm Hash digest
SHA256 e7756ff6ed28146e6ad27a779c9c4834c8b21d860a6cb7feced9b8dfd0cb88a6
MD5 83344cc39e2de8d7ee96872870aec821
BLAKE2b-256 2e71af6fc1902a38fc65129098978e7a8800876795a2e1f964c985315effa1a0

See more details on using hashes here.

File details

Details for the file scanapi-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: scanapi-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for scanapi-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0d6ad344234d0a214e401133e2dfe2cbae9967c5549fa64eeb6b5549c0248789
MD5 14d44bd630dc67bca6cb6568f30eb6cb
BLAKE2b-256 181a4d50fef3868ab6ede52e649445c4ccf5be939e12eab7867123586d20aef2

See more details on using hashes here.

Supported by

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