simple_api_client 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -22
- data/lib/simple_api_client.rb +91 -0
- data/lib/simple_api_client/version.rb +1 -1
- data/spec/simple_api_client_spec.rb +24 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbaa7daaea726e1e71511f35cf5fc9496529b16d
|
4
|
+
data.tar.gz: 361842e92d99a384508af44552cec9c64265c196
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17d035a89fcf28f82e5fb11cad80cd68459385e43ee32b6b3e2ba116270663e800809aaca4eeba811d147373d8d51394e2a5b5e666b74da4292cd472895537b5
|
7
|
+
data.tar.gz: 91e7f9f9506078ff332b3d1bc9413376904b906e5df2d4112cbc7ff3e1d848cec7c7aec96dd5de1176c6505d4bdc68b9c1f5f3224b10362bf26d3bb8d70500b2
|
data/README.md
CHANGED
@@ -18,28 +18,7 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
```ruby
|
24
|
-
require 'simple_api_client'
|
25
|
-
|
26
|
-
class YourClientClass
|
27
|
-
|
28
|
-
include SimpleApiClient
|
29
|
-
|
30
|
-
#including SimpleApiClient gives you a base initialize method that accepts two parameters.
|
31
|
-
#param1: excepts a URI object (which is used to set the Scheme, Host, Port etc, of your api.
|
32
|
-
#param2: is an object that responses to a call method which needs to accept a hash of options.
|
33
|
-
|
34
|
-
#Define your own endpoints
|
35
|
-
|
36
|
-
def client_info(client_id, payload)
|
37
|
-
call(method: :get, uri: uri('/client_info')
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
```
|
21
|
+
See: [simple_api_client.rb documentation](https://github.com/dvallance/simple_api_client/blob/master/lib/simple_api_client.rb).
|
43
22
|
|
44
23
|
## Contributing
|
45
24
|
|
data/lib/simple_api_client.rb
CHANGED
@@ -4,6 +4,88 @@ require 'simple_api_client/http_caller/net_http'
|
|
4
4
|
require 'simple_api_client/http_caller/response'
|
5
5
|
require 'active_support/core_ext/module/delegation'
|
6
6
|
|
7
|
+
# This module provides some simple basic methods to help create an HTTP based
|
8
|
+
# API client with minimum effort.
|
9
|
+
#
|
10
|
+
# If you have an HTTP based API endpoint that accepts and delivers its payloads
|
11
|
+
# via JSON or XML this gem is designed to quickly create the calling client. It
|
12
|
+
# does this by providing an #initialize method that handles setting your HTTP
|
13
|
+
# servers HOST, PORT, and SCHEME. These are set with a URI object or a URI
|
14
|
+
# parse-able string. This allows you to declare any endpoints paths relative to
|
15
|
+
# the provided URI.
|
16
|
+
#
|
17
|
+
# The second part of the equation is the Http client that actually makes the
|
18
|
+
# call to the server. One is provided in this gem, +HttpCaller::NetHttp+, which
|
19
|
+
# is just a wrapper around rubys Net::HTTP client. If your looking for something
|
20
|
+
# more performant you can use the +http_caller-curb+ gem which provides a
|
21
|
+
# wrapper around Curb (Libcurl bindings for Ruby). The HttpCaller is simply a
|
22
|
+
# class that implements a +#call+ method that accepts an options hash.
|
23
|
+
#
|
24
|
+
# ==HttpCallers
|
25
|
+
#
|
26
|
+
# +HttpCaller::NetHttp+ (provided with this gem) and +HttpCaller::Curb+
|
27
|
+
# (requires the http_caller-curb gem) are both very simple wrappers around
|
28
|
+
# existing HTTP clients and they both understand the following options.
|
29
|
+
# * :method - [:get, :post, :put] allows the caller to determine the type of
|
30
|
+
# call to perform.
|
31
|
+
# * :uri - the *absolute* url address for the endpoint. There is a convienence
|
32
|
+
# method #uri which will turn a relative path into the absolute path based on
|
33
|
+
# the initial URI passed to the initializer.
|
34
|
+
# * :payload - a :post or :put can accept a payload.
|
35
|
+
# * :accept - tells the server what format the payload will be [:json (default)
|
36
|
+
# or :xml].
|
37
|
+
# * :content_type - if your server api can respond in :json or :xml you can tell
|
38
|
+
# it which you prefer (:json is the default).
|
39
|
+
#
|
40
|
+
# The HttpCallers I've provided both wrap exceptions in a common format.
|
41
|
+
# * Errno::ECONNREFUSED - if the server is unresponsive
|
42
|
+
# * Errno::ECONNRESET - the remote host reset the connection request
|
43
|
+
# * Errno::ETIMEDOUT - timed out
|
44
|
+
#
|
45
|
+
# You can of course roll your own HttpCaller and if thats the case you can
|
46
|
+
# change up the option names and respond to anything you want.
|
47
|
+
#
|
48
|
+
# ==Usage
|
49
|
+
# This simplest usage would be to include the *SimpleApiClient* module in your
|
50
|
+
# intended client and provide some basic endpoints.
|
51
|
+
# require 'simple_api_client'
|
52
|
+
#
|
53
|
+
# class MyApiClient
|
54
|
+
# include SimpleApiClient
|
55
|
+
#
|
56
|
+
# def get_address(user_id)
|
57
|
+
# # note the use of the #uri method to create an absolute uri
|
58
|
+
# path = uri("/users/#{user_id}/address")
|
59
|
+
#
|
60
|
+
# # call is delegated to the provided HttpCaller object.
|
61
|
+
# call(uri: path, method: :get)
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# # a post example could look like this
|
65
|
+
# def post_address(user_id, payload)
|
66
|
+
# path = uri("/users/#{user_id}/address")
|
67
|
+
# call(uri: path, method: :post, payload: payload)
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# my_client = MyApiClient.new('https://myserver:7788', HttpCaller::NetHttp.new)
|
72
|
+
#
|
73
|
+
# #result is an HttpCaller::Response object which has a response code and body.
|
74
|
+
# result = my_client.get_address(1)
|
75
|
+
#
|
76
|
+
# puts result.code
|
77
|
+
# # => 200
|
78
|
+
#
|
79
|
+
# puts result.body #server specific, but the request will ask for json by default.
|
80
|
+
# # => "{\"street address\":\"897 someroad\"}"
|
81
|
+
#
|
82
|
+
# The code behind the scenes is vary simple and there is just an un-enforced
|
83
|
+
# contract between the client class and the provided HttpCaller class. Your
|
84
|
+
# defined endpoints simply pass the options to the HttpCaller and expect it to
|
85
|
+
# respond. In my provided code that response is an HttpCaller::Response object
|
86
|
+
# but this can be anything you want if you provide your own.
|
87
|
+
#
|
88
|
+
# @author David Vallance
|
7
89
|
module SimpleApiClient
|
8
90
|
|
9
91
|
ARGUMENT_ERROR_MESSAGE_HOST_MISSING = 'First argument must be a URI object or a URI string that provides a host. (i.e //myhost or http://myhost). Note: Scheme defaults to http if not provided.'
|
@@ -13,6 +95,15 @@ module SimpleApiClient
|
|
13
95
|
delegate :call, to: :@caller
|
14
96
|
attr_reader :caller
|
15
97
|
|
98
|
+
# Provides an default initializer for your class along with validation
|
99
|
+
# checking on the accepted params. An ArgumentError will be raised if the
|
100
|
+
# parameters do not satisfy expectations.
|
101
|
+
#
|
102
|
+
# @param uri [URI::HTTP, String] a URI object or a URI parse-able string.
|
103
|
+
# @param api_caller [#call] an object that responds to a #call method.
|
104
|
+
# HttpCaller::NetHttp is provided with this simple_api_client gem. See gem
|
105
|
+
# [http_caller-curb] for an optional HttpCaller::Curb implimentation using
|
106
|
+
# curb.
|
16
107
|
def initialize uri, api_caller
|
17
108
|
@uri = URI(uri)
|
18
109
|
raise ArgumentError.new(ARGUMENT_ERROR_MESSAGE_HOST_MISSING) if @uri.host.nil?
|
@@ -53,3 +53,27 @@ describe TestClient do
|
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
56
|
+
|
57
|
+
class TestClientWithOwnInitializer
|
58
|
+
include SimpleApiClient
|
59
|
+
|
60
|
+
attr_accessor :my_string
|
61
|
+
|
62
|
+
def initialize string
|
63
|
+
super('http://127.0.0.1:9393', TestCaller.new)
|
64
|
+
self.my_string = string
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
describe TestClientWithOwnInitializer do
|
70
|
+
it '#initializes properly' do
|
71
|
+
client = TestClientWithOwnInitializer.new('something')
|
72
|
+
client.scheme.must_equal 'http'
|
73
|
+
client.host.must_equal '127.0.0.1'
|
74
|
+
client.port.must_equal 9393
|
75
|
+
client.caller.class.must_equal TestCaller
|
76
|
+
client.my_string.must_equal 'something'
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dave Vallance
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|