alisinabh

alisinabh

Hi everyone.

In last month i was dealing with REST Api services mostly from client perspective and i realized how much people don’t care about documentation of their APIs. That’s Crazy! There was even one company who hadn’t any docs at all! They just sent me their real-time generated docs (seriously?) as we were talking on messenger and almost all of it were WRONG!

But even when there was a cool and fair documentation, it was still a time consuming task developing every single action in those documents.

So there is something called ApiBlueprint.
It is simply:

A powerful high-level API description language for web APIs.

It is so nice. If you are familiar with apiary you hopefully know what an ApiBlueprint is.

Since ApiBlueprint is fairly rich, I came up with the idea of writing a code generator based on ApiBlueprint that takes .apib files and convert them to Elixir code for usage.

I’ve named it Prex

Simply Prex can take .apib file and generate Elixir modules like this:

# Created by Prex
defmodule Example.Posts do
  @moduledoc """
  This section groups App.net post resources.
  """

  @base_url "https://alpha-api.app.net"

  ###
  # API Calls
  ###

  # Post

  @doc """
  Returns a specific Post.

  ## Parameters
    - postid: The id of the Post.
  """
  def retrieve_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}"
    HTTPoison.request(:get, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def retrieve_a_post!(postid \\ "") do
    {:ok, result} = retrieve_a_post(postid)
    result
  end

  @doc """
  Delete a Post. The current user must be the same user who created the Post. It
returns the deleted Post on success.

  ## Parameters
    - postid: The id of the Post.
  """
  def delete_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}"
    HTTPoison.request(:delete, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def delete_a_post!(postid \\ "") do
    {:ok, result} = delete_a_post(postid)
    result
  end

  # Posts Collection

  @doc """
  Create a new Post object. Mentions and hashtags will be parsed out of the post
text, as will bare URLs...
  """
  def create_a_post do
    req_url = Path.join @base_url, "/stream/0/posts"
    HTTPoison.request(:post, req_url)
  end

  def create_a_post! do
    {:ok, result} = create_a_post()
    result
  end

  @doc """
  Retrieves all posts.
  """
  def retrieve_all_posts do
    req_url = Path.join @base_url, "/stream/0/posts"
    HTTPoison.request(:get, req_url)
  end

  def retrieve_all_posts! do
    {:ok, result} = retrieve_all_posts()
    result
  end

  # Stars

  @doc """
  Save a given Post to the current User’s stars. This is just a “save” action,
not a sharing action.

*Note: A repost cannot be starred. Please star the parent Post.*

  ## Parameters
    - postid: The id of the Post.
  """
  def star_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}/star"
    HTTPoison.request(:post, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def star_a_post!(postid \\ "") do
    {:ok, result} = star_a_post(postid)
    result
  end

  @doc """
  Remove a Star from a Post.

  ## Parameters
    - postid: The id of the Post.
  """
  def unstar_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}/star"
    HTTPoison.request(:delete, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def unstar_a_post!(postid \\ "") do
    {:ok, result} = unstar_a_post(postid)
    result
  end

end

I’ve been working on this for only about 48 hours now. It’s super buggy. :cold_sweat:
It is going to support SOAP with WSDL too.

I just want to know how much do you think this is useful?

Please share your opinion with me. Even if you think this is very useless.

Thank’s for reading this. :slight_smile:

https://github.com/alisinabh/prex

Showing Posts 7 to 1

alisinabh

alisinabh OP

The ? (Question mark) before a variable name is a standard for ApiBlueprint which indicates that parameter is not mandatory. So this is not possible to have it in ApiBlueprint.

This is also applied to HTTP, you cannot have a variable in HTTP Query strings (Which is same as application/x-www-form-urlencoded in post body) that contains a ?.

However you can achieve ? in variable name by using %3F instead of ? in both ApiBluprint and HTTP requests.

Thank you @OvermindDL1 :blush:

OvermindDL1

OvermindDL1

Heh, cool idea. Any way to override in case a ? really is part of the name (unlikely and rare though it is)? :slight_smile:

alisinabh

alisinabh OP

Prex 0.0.3

Prex 0.0.3 is out for testing. In this version prex supports URL parameters (AKA Query strings)

If a query string is provided in ApiBlueprint, prex will convert it to a function like below:

def list_all_users(since \\ nil, limit \\ nil) do
  req_url = Path.join @base_url, "/users?limit=#{limit |> URI.encode_www_form}" <>
   (if since != nil, do: "since=#{since |> URI.encode_www_form}", else: "")

  HTTPoison.request(:get, req_url, body: Poison.encode!(%{"since" => since, "limit" => limit}), headers: ["Content-Type": "application/json"])
end

Parameters can be optional or required. if a parameter is optional (indicated with ? before its name in ApiBlueprint) then a single line if will get placed.

Parameters in Parameters section of the url will also be in JSON body of request. (Which should not happen always) I will fix that soon.

I admit it’s a bit messy for optional params. Since currently i don’t want prex to be a runtime dependency for scaffolded code, i didn’t use a function in my lib to achieve optional parameters.

Thanks,
Your comments means a lot to me :slight_smile:

alisinabh

alisinabh OP

Thank you @OvermindDL1

I’m working on some updates. SOAP will be supported soon. :innocent:

I will :sweat_smile:

OvermindDL1

OvermindDL1

That looks quite cool. :slight_smile:

Hold on to your sanity! ^.^;

alisinabh

alisinabh OP

Thank you @mbuhot :slight_smile:

I need to make prex check project’s dependencies for HTTPoison (and Poison when i start processing responses)

Since generated code may and should be altered by the developer, I need to know two things about previously in generated actions.

  1. Version of Prex which generated the code
  2. Version of implemented ApiBlueprint

I should research around this topic and gather useful information about code generators (since this is my first code generator)

I hope i find a good solution for this matter.

mbuhot

mbuhot

This is really neat!

My current workflow uses phoenix_swagger to generate a swagger spec, then bureaucrat to generate markdown documentation.

With good tools available, there’s no excuse to not have a well documented REST API :smiley:

One idea I haven’t tied out yet is to replace custom mix tasks that generate json/markdown/html files with a more integrated mix compiler, this post makes it look pretty strait forward.

For prex, you might be able to use macros to generate the module body from the api blueprint, using @external_resource to cause a recompile when the blueprint changes.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews