Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zendesk/api_client
HTTP API Client Builder
https://github.com/zendesk/api_client
gem ruby sell
Last synced: 13 days ago
JSON representation
HTTP API Client Builder
- Host: GitHub
- URL: https://github.com/zendesk/api_client
- Owner: zendesk
- License: apache-2.0
- Created: 2011-12-05T16:18:15.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T17:44:21.000Z (almost 2 years ago)
- Last Synced: 2024-10-13T15:52:13.616Z (about 1 month ago)
- Topics: gem, ruby, sell
- Language: Ruby
- Homepage:
- Size: 145 KB
- Stars: 16
- Watchers: 30
- Forks: 13
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
ApiClient ![Tests](https://github.com/zendesk/api_client/workflows/Tests/badge.svg)
=========ApiClient is an experimental builder for HTTP API clients. The goal is to provide a easy to use engine for constructing queries and map the responses to instances of Hashie::Mash subclasses.
Basically you should be able to build a client for any HTTP based API, without the need for handling connections, parsing responses or instantiating objects. All you need to do is choose where the request should go and enhance your client classes. See the examples dir for usage examples.
Current state is alpha - it works, but the query interface is not final and is subject to change at any time. Hell, even the can even change without prior notice. You were warned.
## How API Client works
ApiClient gives you two classes that you can inherit from:
* ApiClient::Base
This class gives you the most basic access. It is a Hashie::Mash
subclass. On the class level, it exposes a variety of request
methods (like get, post, put, delete)* ApiClient::Resource::Base
This class extends ApiClient::Base giving it ActiveRecord-like class methods
(find, find_all, create, update, destroy) as well as instance methods
(save, destroy).## Making requests
By design, all request methods are singleton methods. They either return Ruby
objects or objects of the class they are called on.### `ApiClient::Base.get(path, params = {})`
Make a GET request to a specified path. You can pass params as the second
argument. It will parse the representation and return a Ruby object.#### Example
```ruby
ApiClient::Base.get('/apples/1.json')
# => returns a parsed Hash
```### `ApiClient::Base.post(path, params = {})`
Make a POST request to a specified path. You can pass params as the second
argument. It will parse the representation and return a Ruby object.#### Example
```ruby
ApiClient::Base.post('/apples/1.json', :name => 'Lobo')
# => returns a parsed Hash
```### `ApiClient::Base.put(path, params = {})`
Make a PUT request to a specified path. You can pass params as the second
argument. It will parse the representation and return a Ruby object.#### Example
```ruby
ApiClient::Base.put('/apples/1.json', :name => 'Lobo')
# => returns a parsed Hash
```### `ApiClient::Base.delete(path, params = {})`
Make a DELETE request to a specified path. You can pass params as the second
argument. It will parse the representation and return a Ruby object.#### Example
```ruby
ApiClient::Base.delete('/apples/1.json')
# => returns a parsed Hash
```### `ApiClient::Base.fetch(path, params = {})`
Make a GET request to a specified path. You can pass params as the second
argument. It will parse the representation, pass it to the build method and
return and object of the class it was called on.#### Example
```ruby
ApiClient::Base.fetch('/apples/1.json')
# => returns a ApiClient::Base objectclass Apple < ApiClient::Base
endApple.fetch('/apples/1.json')
# => returns an Apple object
```## Scoping and Chaining
ApiClient allows you to apply scoping to your request using 3 methods:
* ApiClient::Base.params(pars = {})
This method allows you to add parameters to a request. If the request is a
GET request, it will be added in the query. Otherwise, it will be sent in
the request body.It returns a ApiClient::Scope object attached to a class you started with.
* ApiClient::Base.options(opts = {})
Allows passing options to the connection object behind the ApiClient. Useful
when working with OAuth for passing the token.It returns a ApiClient::Scope object attached to a class you started with.
* ApiClient::Base.headers(heads = {})
Allows setting headers in the request. Useful when you need to add a token
as the headerIt returns a ApiClient::Scope object attached to a class you started with.
* ApiClient::Base.raw_body(body = nil)
Allows setting non-hash body in the request. Useful for binary payloads.
Notice: it overrides all parameters set via params method!It returns a ApiClient::Scope object attached to a class you started with.
All of these methods return a ApiClient::Scope object. When you call any request
methods on this object (get, post, put, delete), the request will apply the
options, params and headers.### Examples
#### Params, headers and a GET request
```ruby
ApiClient::Base.
params({ :page => 1 }).
headers('Auth-Token', 'mytoken').
get('/stuff.json') # => returns a parsed Array object
```## Logging
To log requests set the `ApiClient.logger`. To log request payloads and headers set level to `Logger::DEBUG`
```ruby
require 'logger'
ApiClient.logger = Logger.new('api_client.log')
ApiClient.logger.level = Logger::INFO```
## Releasing new version of gem
1. Update version in [lib/api_client/version.rb](lib/api_client/version.rb) and push to `master`
2. Create new GitHub release with tag name starting with `v` and the version, for example `v1.0.0`
3. Gem will be automatically built and pushed to rubygems.org with GitHub Action## Copyright and license
Copyright 2011 Zendesk
Licensed under the [Apache License, Version 2.0](LICENSE)