ant-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4869c734537d9dda9bce74cd78c24e57d7749a969357fb632dcdd56c6419d227
4
+ data.tar.gz: 1a821dca419796fc6ccb332deb7b99cc11c5f03c975c491c5c5edf53d222ae8b
5
+ SHA512:
6
+ metadata.gz: 9a20670667495948a62509c028b32971aec95fda635e3abfcb77d7bd89522779db4f9dc0c4189c2cc8e8ca00db7d34cf6e9b6d0acc8bf5afe2907ddaf76662cf
7
+ data.tar.gz: 8d248eab65088a0d0edd88c456c514fa2fada5c1012b8ecb05e6dbf7e374e687ac43f4c57f6ca8ed4aa0770f592824887c681930f52ae5736f90bd81da7b6979
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'client/rest_client'
4
+ require_relative 'client/version'
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'json_format'
4
+ require_relative 'url_encoded'
5
+
6
+ module Ant
7
+ module Client
8
+ # Singleton storing all the formats implemented for http clients.
9
+ module Format
10
+ extend Ant::DRY::ResourceInjector
11
+ class << self
12
+ def build(config)
13
+ resource(:formats, config[:format] || 'json').new
14
+ end
15
+
16
+ def register_format(name, klass)
17
+ register(:formats, name, klass)
18
+ end
19
+ end
20
+ register_format('json', JSONFormat)
21
+ register_format('url_encoded', URLEncodedFormat)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Ant
6
+ module Client
7
+ module Format
8
+ # Implements JSON format for http clients.
9
+ class JSONFormat
10
+ def pack(data)
11
+ {
12
+ body: data.to_json,
13
+ headers: { 'Content-type' => 'application/json; charset=UTF-8',
14
+ 'User-Agent' => 'Ruby Ant Client' }
15
+ }
16
+ end
17
+
18
+ def unpack(data)
19
+ unformat(data)
20
+ end
21
+
22
+ def unformat(msg)
23
+ JSON.parse(msg, symbolize_names: true)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'json_format'
4
+
5
+ module Ant
6
+ module Client
7
+ module Format
8
+ # Allows to send data url encoded as a regular http client.
9
+ class URLEncodedFormat < JSONFormat
10
+ def pack(data)
11
+ {
12
+ body: encode(data),
13
+ headers: { 'Content-type' =>
14
+ 'application/x-www-form-urlencoded; charset=UTF-8' }
15
+ }
16
+ end
17
+
18
+ def encode(data)
19
+ data.map { |k, v| "#{k}=#{v}" }.join('&')
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
File without changes
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty'
4
+ require 'ant/core'
5
+ require 'ant/logger'
6
+
7
+ require_relative 'session/base'
8
+ require_relative 'format/format'
9
+ require_relative 'validator'
10
+
11
+ module Ant
12
+ module Client
13
+ # HTTP client using HTTParty as support. This implementation makes it easier
14
+ # to integrate with other backends.
15
+ class RESTClient
16
+ include Ant::Logger
17
+
18
+ def initialize(configs)
19
+ @session = Session.build(configs)
20
+ @config = configs
21
+ @format = Format.build(configs)
22
+ @endpoint = configs[:endpoint]
23
+ @validator = Validator.build(configs)
24
+ end
25
+
26
+ def get(path, data = {})
27
+ perform_request(:get, path, data)
28
+ end
29
+
30
+ def post(path, data = {})
31
+ perform_request(:post, path, data)
32
+ end
33
+
34
+ def put(path, data = {})
35
+ perform_request(:put, path, data)
36
+ end
37
+
38
+ def delete(path, data = {})
39
+ perform_request(:delete, path, data)
40
+ end
41
+
42
+ def patch(path, data = {})
43
+ perform_request(:patch, path, data)
44
+ end
45
+
46
+ def raw_get(path, data = {})
47
+ perform_raw_request(:get, path, data)
48
+ end
49
+
50
+ def raw_post(path, data = {})
51
+ perform_raw_request(:post, path, data)
52
+ end
53
+
54
+ private
55
+
56
+ def perform_raw_request(method, path, data)
57
+ log_debug('Performing request', method: method, path: path, data: data)
58
+ init_time = Time.now
59
+ uri = (path.start_with?('http') ? path : "#{@endpoint}#{path}")
60
+ result = @session.perform_request(method, uri,
61
+ @format.pack(data))
62
+ log_info('Request perfomed',
63
+ path: path,
64
+ server: @endpoint,
65
+ verb: method,
66
+ processing_time: (Time.now - init_time).to_f * 1000)
67
+ result
68
+ end
69
+
70
+ def perform_request(method, path, data)
71
+ result = perform_raw_request(method, path, data).body
72
+ unpacked = @format.unpack(result)
73
+ @validator.validate(unpacked)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty'
4
+ require_relative 'basic_auth'
5
+
6
+ module Ant
7
+ module Client
8
+ # Provides session handling on http requests
9
+ module Session
10
+ def self.build(config)
11
+ Base.new(config)
12
+ end
13
+
14
+ # Used on http clients. It modifies the requests in order to implement
15
+ # credentials and other mechanism for session.
16
+ class Base
17
+ include HTTParty
18
+ include BasicAuth
19
+
20
+ def initialize(config)
21
+ @config = config
22
+ register_certificate
23
+ register_ca
24
+ end
25
+
26
+ # :nocov: #
27
+ def register_certificate
28
+ return unless @config[:client_certificate]
29
+
30
+ cert = File.read(@config[:client_certificate])
31
+ self.class.pkcs12(cert, @config[:client_certificate_pass])
32
+ end
33
+ # :nocov: #
34
+
35
+ # :nocov: #
36
+ def register_ca
37
+ return unless @config[:ca_validate]
38
+
39
+ self.class.ssl_ca_file(@config[:ca_validate])
40
+ end
41
+ # :nocov: #
42
+
43
+ def configure_request(request)
44
+ basic_auth(request, @config[:basic_auth]) if @config[:basic_auth]
45
+ request[:verify] = @config[:verify] if @config.key?(:verify)
46
+ end
47
+
48
+ def perform_request(method, endpoint, data)
49
+ configure_request(data)
50
+ self.class.send(method, endpoint, data)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
4
+ module Client
5
+ module Session
6
+ # Implements basic auth functionality for http client
7
+ module BasicAuth
8
+ def basic_auth(request, user:, password:)
9
+ request[:basic_auth] ||= { username: user, password: password }
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'validator/jsend'
4
+ require_relative 'validator/no_validator'
5
+
6
+ module Ant
7
+ module Client
8
+ # Allows to validate the responses from another service.
9
+ # Use it to rise exceptions when you detect there were an error.
10
+ module Validator
11
+ extend Ant::DRY::ResourceInjector
12
+
13
+ class << self
14
+ def build(config)
15
+ resource(:validators, config[:validator] || 'empty').new
16
+ end
17
+
18
+ def register_validator(name, klass)
19
+ register(:validators, name, klass)
20
+ end
21
+
22
+ end
23
+ register_validator('jsend', JSend)
24
+ register_validator('empty', NoValidator)
25
+ register_validator('none', NoValidator)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
4
+ module Client
5
+ module Validator
6
+ # Implements JSend specification on another backend calls
7
+ class JSend
8
+ include Ant::Exceptions
9
+ EXCEPTION_LIST = {
10
+ 'fail' => AntFail,
11
+ 'error' => AntError,
12
+ 'fatal' => AntError
13
+ }.freeze
14
+
15
+ def validate(response)
16
+ case response[:status]
17
+ when 'success'
18
+ response[:data]
19
+ when 'fail', 'error', 'fatal'
20
+ exception_klass = EXCEPTION_LIST[response[:status]]
21
+ raise exception_klass.new(response[:message],
22
+ response[:code],
23
+ response[:data])
24
+ else
25
+ raise(AntError, 'Unknown Error')
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Ant
6
+ module Client
7
+ module Validator
8
+ # Implement the empty validator for http client responses.
9
+ class NoValidator
10
+ def validate(response)
11
+ response
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
4
+ module Client
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,209 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ant-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gilberto Vargas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ant-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ant-logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.12'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.12'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '12.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '12.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rdoc
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '6.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '6.1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.16'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.16'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.5'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.5'
167
+ description: Use this gem with a duo with the ant-server gem
168
+ email:
169
+ - tachoguitar@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - lib/ant/client.rb
175
+ - lib/ant/client/format/format.rb
176
+ - lib/ant/client/format/json_format.rb
177
+ - lib/ant/client/format/url_encoded.rb
178
+ - lib/ant/client/format/xml_format.rb
179
+ - lib/ant/client/rest_client.rb
180
+ - lib/ant/client/session/base.rb
181
+ - lib/ant/client/session/basic_auth.rb
182
+ - lib/ant/client/validator.rb
183
+ - lib/ant/client/validator/jsend.rb
184
+ - lib/ant/client/validator/no_validator.rb
185
+ - lib/ant/client/version.rb
186
+ homepage: https://github.com/tachomex/ant-client
187
+ licenses:
188
+ - MIT
189
+ metadata: {}
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ requirements: []
205
+ rubygems_version: 3.0.3
206
+ signing_key:
207
+ specification_version: 4
208
+ summary: Implements a HTTP Client that can be configured a lot
209
+ test_files: []