basic_api_client 1.0.0
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +9 -0
- data/lib/basic_api_client.rb +65 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: acdf78b43f35bba13bb9ae63b1760a53762f23ba
|
4
|
+
data.tar.gz: 9d2310d251369e250fdae844711754fb2ce9b146
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2f6c7c5d800525f86931daf826983ea9f84533dd96f1c6a06e2d55b286d97a43ee7fe73b6ffcfd0182b8d484f99b3159b22012f2d38122ebc45fe611699c03d
|
7
|
+
data.tar.gz: 71ca21a3a9c82616b786aa4c6416fa45b5b6a6063e1378829533d55bf235f1bf1a0667e04979f684c1355717e196c7b12e936716030313ee97f81c149b93e5c4
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 John Hager
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
```ruby
|
2
|
+
client = BasicApiClient.new("user.cn", "userpassword", "http://example.com", "/api/v1/auth/sign_in")
|
3
|
+
|
4
|
+
client.authorize
|
5
|
+
#=> Returns a Hash with user data if success, else nil
|
6
|
+
|
7
|
+
client.get('/api/v1/users')
|
8
|
+
#=> Returns a Hash with user data if success, else nil
|
9
|
+
```
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class BasicApiClient
|
5
|
+
AUTH_HEADERS = %w{ access-token token-type uid expiry client }.sort
|
6
|
+
|
7
|
+
attr_reader :headers
|
8
|
+
|
9
|
+
def initialize(cn, password, site, auth_url)
|
10
|
+
@cn = cn
|
11
|
+
@password = password
|
12
|
+
@site = site
|
13
|
+
@auth_url = auth_url
|
14
|
+
end
|
15
|
+
|
16
|
+
def conn
|
17
|
+
@conn ||= begin
|
18
|
+
Faraday.new(url: @site) do |faraday|
|
19
|
+
faraday.request :url_encoded
|
20
|
+
faraday.response :logger
|
21
|
+
faraday.adapter Faraday.default_adapter
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def authorize
|
27
|
+
res = conn.post do |req|
|
28
|
+
req.url @auth_url
|
29
|
+
req.params['cn'] = @cn
|
30
|
+
req.params['password'] = @password
|
31
|
+
end
|
32
|
+
|
33
|
+
handle_response(res)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get(url, params = {})
|
37
|
+
res = conn.get do |req|
|
38
|
+
req.url url
|
39
|
+
req.params = params.merge(req.params)
|
40
|
+
req.headers = @headers
|
41
|
+
end
|
42
|
+
|
43
|
+
handle_response(res)
|
44
|
+
end
|
45
|
+
|
46
|
+
def post(url, params = {})
|
47
|
+
res = conn.post do |req|
|
48
|
+
req.url url
|
49
|
+
req.params = params.merge(req.params)
|
50
|
+
req.headers = @headers
|
51
|
+
end
|
52
|
+
|
53
|
+
handle_response(res)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
def handle_response(res)
|
58
|
+
if res.success?
|
59
|
+
@headers = res.headers.select { |k, v| AUTH_HEADERS.include?(k) }
|
60
|
+
else
|
61
|
+
@headers = {}
|
62
|
+
end
|
63
|
+
JSON.parse(res.body) if @headers.keys.sort == AUTH_HEADERS
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: basic_api_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jphager2
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Example of a basic api client that keeps access-token headers
|
42
|
+
email: jphager2@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- lib/basic_api_client.rb
|
50
|
+
homepage: https://github.com/jphager2/basic_api_client
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.4.6
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Example of a basic api client that keeps access-token headers
|
74
|
+
test_files: []
|