All Projects → rubygarage → Api_struct

rubygarage / Api_struct

Licence: mit
API wrapper builder with response serialization

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Api struct

Colore
A powerful C# library for Razer Chroma's SDK
Stars: ✭ 121 (-45.98%)
Mutual labels:  api, wrapper
Mailjet Apiv3 Nodejs
[API v3] Official Mailjet API v3 NodeJS wrapper
Stars: ✭ 137 (-38.84%)
Mutual labels:  api, wrapper
Jstp
Fast RPC for browser and Node.js based on TCP, WebSocket, and MDSF
Stars: ✭ 132 (-41.07%)
Mutual labels:  api, serialization
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (-49.11%)
Mutual labels:  api, serialization
Jsonapi Rb
Efficiently produce and consume JSON API documents.
Stars: ✭ 219 (-2.23%)
Mutual labels:  api, serialization
Ovoid
Un-Official OVO API Wrapper
Stars: ✭ 121 (-45.98%)
Mutual labels:  api, wrapper
Python Twitch Client
Python wrapper for Twitch API
Stars: ✭ 137 (-38.84%)
Mutual labels:  api, wrapper
Spotify Web Api Js
A client-side JS wrapper for the Spotify Web API
Stars: ✭ 1,313 (+486.16%)
Mutual labels:  api, wrapper
Wiki
Wikipedia Interface for Node.js
Stars: ✭ 180 (-19.64%)
Mutual labels:  api, wrapper
Spaces Api
An API wrapper for DigitalOcean's Spaces object storage designed for easy use.
Stars: ✭ 166 (-25.89%)
Mutual labels:  api, wrapper
Tlaw
The Last API Wrapper: Pragmatic API wrapper framework
Stars: ✭ 112 (-50%)
Mutual labels:  api, wrapper
Uniswap Python
🦄 The unofficial Python client for the Uniswap exchange.
Stars: ✭ 191 (-14.73%)
Mutual labels:  api, wrapper
Binance.api.csharp.client
C#.NET client for Binance Exchange API.
Stars: ✭ 98 (-56.25%)
Mutual labels:  api, wrapper
Kayn
superagent-inspired Node.js lib (w/ **some** TypeScript support) for accessing Riot's League of Legend's API (discord: cnguy#3614)
Stars: ✭ 122 (-45.54%)
Mutual labels:  api, wrapper
Discord.jl
The Julia Discord API Wrapper
Stars: ✭ 93 (-58.48%)
Mutual labels:  api, wrapper
Mastodonkit
MastodonKit is a Swift Framework that wraps Mastodon's API
Stars: ✭ 134 (-40.18%)
Mutual labels:  api, wrapper
Termux
Node.js module for Termux-API
Stars: ✭ 87 (-61.16%)
Mutual labels:  api, wrapper
Apipeline
Feature-rich and pluggable offline-first API wrapper for all your javascript environements ! Easily wire-up your API and make your app work offline in minutes.
Stars: ✭ 92 (-58.93%)
Mutual labels:  api, wrapper
Coinbasepro Csharp
The unofficial .NET/C# client library for the Coinbase Pro/GDAX API
Stars: ✭ 143 (-36.16%)
Mutual labels:  api, wrapper
Fbrecog
An unofficial python wrapper for the Facebook face recognition endpoint
Stars: ✭ 184 (-17.86%)
Mutual labels:  api, wrapper

ApiStruct

ApiStruct consists of two main interfaces: ApiStruct::Client and ApiStruct::Entity. The ApiStruct::Client class is aimed at using the same interface for describing requests to different APIs. The ApiStruct::Entity enables you to use ApiStruct clients in ORM-like style.

Gem Version Maintainability CircleCI

Installation

Add this line to your application's Gemfile:

gem 'api_struct'

And then execute:

$ bundle

Or install it yourself as:

$ gem install api_struct

Usage

Initialize APIs routes:

ApiStruct::Settings.configure do |config|
  config.endpoints = {
    first_api: {
      root: 'http://localhost:3000/api/v1',
      headers: {
        'content-type': 'application/json',
        'Authorization': 'Bearer TOKEN'
      }
    },
    second_api: {
      root: 'http://localhost:3001/api/v1',
      params: { token: 'Default token' }
    }
  }
end

Client

Endpoint wrapper

class PostsClient < ApiStruct::Client
  first_api :posts

  def show(id)
    get(id)
  end

  def index
    get
  end

  def user_posts(user_id, post_id = nil)
    get(post_id, prefix: [:users, user_id])
    # alias:
    # get(post_id, prefix: '/users/:id', id: user_id)
  end

  def custom_path(user_id)
    get(path: 'users_posts/:user_id', user_id: user_id)
  end
end

Usage:

PostsClient.new.get(1) # -> /posts/1

Returns Result monad

# => Success({:id=>1, :title=>"Post"})

Other methods from sample:

post_client = PostsClient.new

post_client.index            # -> /posts
post_client.user_posts(1)    # -> /users/1/posts
post_client.user_posts(1, 2) # -> /users/1/posts/2
post_client.custom_path(1)   # -> /users_posts/1/

Entity

Response serializer

class User < ApiStruct::Entity
  client_service UsersClient

  client_service AuthorsClient, prefix: true, only: :index
  # alias:
  # client_service AuthorsClient, prefix: :prefix, except: :index

  attr_entity :name, :id
end
class UsersClient < ApiStruct::Client
  first_api :users

  def show(id)
    get(id)
  end
end
class AuthorsClient < ApiStruct::Client
  first_api :authors

  def index
    get
  end
end

Usage:

user = User.show(1)
# => {"id"=>1, "name"=>"John"}

Call methods from prefixed clients:

users = User.authors_client_index
# or
# users = User.prefix_index

Response serializers with related entities:

class Network < ApiStruct::Entity
  client_service NetworkClient

  attr_entity :name, :id
  attr_entity :state, &:to_sym

  has_entity :super_admin, as: User
end
class NetworkClient < ApiStruct::Client
  first_api :networks

  def show(id)
    get(id)
  end
end

Usage:

network = Network.show(1)
# => {"id"=>1, "name"=>"Main network", "state"=>"active", "super_admin"=>{"id"=>1, "name"=>"John"}}

network.state
# => :active

Dynamic headers:

class Auth
  def self.call
    # Get a token..
  end
end
class AuthHeaderValue
  def self.call
    { "Authorization": "Bearer #{Auth.call}" }
  end
end
class PostClient < ApiStruct::Client
  first_api :posts

  def update(id, post_data)
    put(id, json: post_data, headers: AuthHeaderValue.call)
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rubygarage/api_struct.

License

The gem is available as open source under the terms of the MIT License.


RubyGarage Logo

RubyGarage is a leading software development and consulting company in Eastern Europe. Our main expertise includes Ruby and Ruby on Rails, but we successfully employ other technologies to deliver the best results to our clients. Check out our portfolio for even more exciting works!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].