Giter VIP home page Giter VIP logo

apical's Introduction

Apical

test status

Elixir Routers from OpenAPI schemas

Installation

This package can be installed by adding apical to your list of dependencies in mix.exs:

Exonerate is a compile-time dependency. If you don't include this in your Mix.exs, there will be unwanted compiler warnings.

def deps do
  [
     {:apical, "~> 0.2.1"},
     {:exonerate, "~> 1.1.2", runtime: Mix.env() != :prod}
  ]
end

If you think you might need to recompile your router in production, remove the runtime option on Exonerate.

Basic use

For the following router module:

defmodule MyProjectWeb.ApiRouter do
    require Apical

    Apical.router_from_string(
      """
      openapi: 3.1.0
      info:
        title: My API
        version: 1.0.0
      paths:
        "/":
          get:
            operationId: getOperation
            responses:
              "200":
                description: OK
      """,
      controller: MyProjectWeb.ApiController,
      encoding: "application/yaml"
    )
end

You would connect this to your endpoint as follows:

defmodule MyProjectWeb.ApiEndpoint do
  use Phoenix.Endpoint, otp_app: :my_project

  plug(MyProjectWeb.ApiRouter)
end

And compose a controller as follows:

defmodule MyProjectWeb.ApiController do
  use Phoenix.Controller

  alias Plug.Conn

  # NOTE THE CASING BELOW:
  def getOperation(conn, _params) do
    Conn.send_resp(conn, 200, "OK")
  end
end

From file:

You may also generate a router from a file:

defmodule MyProjectWeb.ApiRouter do
    require Apical

    Apical.router_from_file("priv/assets/api/openapi.v1.yaml",
      controller: MyProjectWeb.ApiController
    )
end

Embedding inside of an existing router

You may also embed apical inside of an existing phoenix router:

scope "/api", MyProjectWeb.Api do
  require Apical

  Apical.router_from_file("priv/assets/api/openapi.v1.yaml",
    controller: MyProjectWeb.ApiController)
end

If you are embedding Apical in an existing router, be sure to delete the following lines from the phoenix endpoint:

plug Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json],
  pass: ["*/*"],
  json_decoder: Phoenix.json_library()

As Apical will do its own parsing.

Using it in tests:

If you're using OpenAPI as a client, you can use Apical to test that your request wrapper conforms to the client schema:

https://hexdocs.pm/apical/apical-for-testing.html

Advanced usage

For more advanced usage, consult the tests in the test directory. Guides will be provided in the next version of apical

Documentation

Documentation can be found at https://hexdocs.pm/apical.

apical's People

Contributors

ityonemo avatar sleipnir avatar kianmeng avatar

Stargazers

Keegan Carlo Falcao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.