http-client-generator 0.1.10 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/http_client_generator/plugs/camelize_body.rb +2 -0
- data/lib/http_client_generator/plugs/encode_json_response.rb +17 -0
- data/lib/http_client_generator/plugs/enforce_json_response.rb +16 -0
- data/lib/http_client_generator/plugs/set_header.rb +19 -0
- data/lib/http_client_generator/plugs/set_request_id.rb +7 -1
- data/lib/http_client_generator/plugs/underscore_response.rb +3 -1
- data/lib/http_client_generator/request.rb +9 -2
- data/lib/http_client_generator/resource.rb +6 -12
- data/lib/http_client_generator/resources_definition.rb +21 -19
- data/lib/http_client_generator/version.rb +1 -1
- data/lib/http_client_generator.rb +3 -5
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9174a1018297dcaa3f4fbbc55006f6fcd70a98c24845fa6d51b795f27551b08
|
4
|
+
data.tar.gz: 8c86b4bade2414fca0302b0292834a17c4caaad932d5e7e9c95c7ae9d4b48726
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9174db830895a0ed7d7cc35ea85e8b409e1d12382e9d2ce0d26c7570fe2b3a5f099128bbd7c4d9bf3f9811c7dfcca9e5c4527a399250bbe11ab5ea68e4a87fe6
|
7
|
+
data.tar.gz: fd87157116505334a027ab6350e4decd72064a3c0b9055f18c51f34e0baace7a8db8f30ab8c6350089248733d93bea7c9fad29107de057fe4fafc4ac96b740de
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HttpClientGenerator
|
4
|
+
class Plugs
|
5
|
+
class EncodeJsonResponse
|
6
|
+
Plugs.register :encode_json_response, self
|
7
|
+
|
8
|
+
def call(request)
|
9
|
+
request.response_body = JSON.parse(request.response_body, symbolize_names: true)
|
10
|
+
request
|
11
|
+
rescue JSON::ParserError
|
12
|
+
request
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HttpClientGenerator
|
4
|
+
class Plugs
|
5
|
+
class EnforceJsonResponse
|
6
|
+
Plugs.register :enforce_json_response, self
|
7
|
+
|
8
|
+
def call(request)
|
9
|
+
request.response_body = JSON.parse(request.response_body, symbolize_names: true)
|
10
|
+
request
|
11
|
+
rescue JSON::ParserError => e
|
12
|
+
request.raise_error(e)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HttpClientGenerator
|
4
|
+
class Plugs
|
5
|
+
class SetHeader
|
6
|
+
Plugs.register :set_header, self
|
7
|
+
|
8
|
+
def initialize(arg:, header:)
|
9
|
+
@arg = arg
|
10
|
+
@header = header
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(req)
|
14
|
+
req.headers[header] = req.rest_args[arg]
|
15
|
+
req
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -5,12 +5,18 @@ require 'securerandom'
|
|
5
5
|
module HttpClientGenerator
|
6
6
|
class Plugs
|
7
7
|
class SetRequestId
|
8
|
+
Plugs.register :set_request_id, self
|
9
|
+
|
8
10
|
def initialize(header_name)
|
9
11
|
@header_name = header_name
|
10
12
|
end
|
11
13
|
|
12
14
|
def call(req)
|
13
|
-
|
15
|
+
request_id = req.rest_args[:request_id] || SecureRandom.uuid
|
16
|
+
|
17
|
+
req.headers[@header_name] = request_id
|
18
|
+
req.extra[:request_id] = request_id
|
19
|
+
|
14
20
|
req
|
15
21
|
end
|
16
22
|
end
|
@@ -3,10 +3,12 @@
|
|
3
3
|
module HttpClientGenerator
|
4
4
|
class Plugs
|
5
5
|
class UnderscoreResponse
|
6
|
+
Plugs.register :underscore_response, self
|
7
|
+
|
6
8
|
def call(req)
|
7
9
|
return req unless req.response_body.is_a?(Hash)
|
8
10
|
|
9
|
-
req.response_body = req.response_body.deep_transform_keys
|
11
|
+
req.response_body = req.response_body.deep_transform_keys { |k| k.to_s.underscore.to_sym }
|
10
12
|
|
11
13
|
req
|
12
14
|
end
|
@@ -15,21 +15,28 @@ module HttpClientGenerator
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
attr_accessor :verb, :content_type, :url, :headers, :body, :rest_args, :response_body
|
18
|
+
attr_accessor :verb, :content_type, :url, :headers, :body, :rest_args, :response_body, :base, :extra
|
19
19
|
|
20
|
-
def initialize(verb:, content_type:, url:, body:, rest_args:)
|
20
|
+
def initialize(base:, verb:, content_type:, url:, body:, rest_args:)
|
21
|
+
@base = base
|
21
22
|
@verb = verb
|
22
23
|
@content_type = content_type
|
23
24
|
@url = url
|
24
25
|
@rest_args = rest_args
|
25
26
|
@body = body
|
26
27
|
@headers = {}
|
28
|
+
@extra = {}
|
27
29
|
end
|
28
30
|
|
29
31
|
def current_headers
|
30
32
|
default_headers.merge(headers)
|
31
33
|
end
|
32
34
|
|
35
|
+
def raise_error(e)
|
36
|
+
Sentry.capture_exception(e, extra: { request_id: extra[:request_id] }) if Object.const_defined?(:Sentry)
|
37
|
+
raise base::RequestError, e.message, e.backtrace, cause: nil
|
38
|
+
end
|
39
|
+
|
33
40
|
private
|
34
41
|
|
35
42
|
def default_headers
|
@@ -4,17 +4,18 @@ require 'http'
|
|
4
4
|
|
5
5
|
module HttpClientGenerator
|
6
6
|
class Resource
|
7
|
-
attr_reader :verb, :content_type, :name, :req_plugs, :resp_plugs
|
7
|
+
attr_reader :verb, :content_type, :name, :req_plugs, :resp_plugs, :base
|
8
8
|
|
9
|
-
def initialize(verb:, content_type:, name:, req_plugs:, resp_plugs:)
|
9
|
+
def initialize(verb:, content_type:, name:, base:, req_plugs:, resp_plugs:)
|
10
10
|
@verb = verb
|
11
|
+
@base = base
|
11
12
|
@content_type = content_type
|
12
13
|
@name = name
|
13
14
|
@req_plugs = req_plugs
|
14
15
|
@resp_plugs = resp_plugs
|
15
16
|
end
|
16
17
|
|
17
|
-
def perform_request(url_helper,
|
18
|
+
def perform_request(url_helper, url_options, body, rest_args)
|
18
19
|
request = prepare_request(url_helper, url_options, body, rest_args)
|
19
20
|
|
20
21
|
request_attributes = [request.verb, request.url]
|
@@ -28,7 +29,7 @@ module HttpClientGenerator
|
|
28
29
|
|
29
30
|
process_response(request)
|
30
31
|
rescue HTTP::Error => e
|
31
|
-
|
32
|
+
request.raise_error(e)
|
32
33
|
end
|
33
34
|
|
34
35
|
private
|
@@ -37,20 +38,13 @@ module HttpClientGenerator
|
|
37
38
|
url = url_helper.public_send(:"#{name}", url_options)
|
38
39
|
|
39
40
|
request = Request.new(
|
40
|
-
verb: verb, url: url, content_type: content_type, body: body, rest_args: rest_args
|
41
|
+
verb: verb, url: url, content_type: content_type, body: body, rest_args: rest_args, base: base
|
41
42
|
)
|
42
43
|
|
43
44
|
req_plugs.reduce(request) { |req, plug| plug.call(req) }
|
44
45
|
end
|
45
46
|
|
46
47
|
def process_response(request)
|
47
|
-
request.response_body =
|
48
|
-
begin
|
49
|
-
request.response_body = JSON.parse(request.response_body, symbolize_names: true)
|
50
|
-
rescue JSON::ParserError
|
51
|
-
request.response_body
|
52
|
-
end
|
53
|
-
|
54
48
|
resp_plugs.reduce(request) { |req, plug| plug.call(req) }
|
55
49
|
|
56
50
|
request.response_body
|
@@ -10,7 +10,8 @@ module HttpClientGenerator
|
|
10
10
|
|
11
11
|
HTTP_VERBS = %i[get post put patch].freeze
|
12
12
|
|
13
|
-
def initialize(req_plugs = [], resp_plugs = [])
|
13
|
+
def initialize(base, req_plugs = [], resp_plugs = [])
|
14
|
+
@base = base
|
14
15
|
@resources = []
|
15
16
|
@req_plugs = req_plugs
|
16
17
|
@resp_plugs = resp_plugs
|
@@ -23,38 +24,39 @@ module HttpClientGenerator
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def req_plug(plug, *args, **kwargs)
|
26
|
-
@req_plugs <<
|
27
|
-
if plug.respond_to?(:call)
|
28
|
-
plug
|
29
|
-
elsif plug.respond_to?(:new)
|
30
|
-
plug.new(*args, **kwargs)
|
31
|
-
else
|
32
|
-
Plugs.read(plug).new(*args, **kwargs)
|
33
|
-
end
|
27
|
+
@req_plugs << build_plug(plug, *args, **kwargs)
|
34
28
|
end
|
35
29
|
|
36
30
|
def resp_plug(plug, *args, **kwargs)
|
37
|
-
@resp_plugs <<
|
38
|
-
if plug.respond_to?(:call)
|
39
|
-
plug
|
40
|
-
elsif plug.respond_to?(:new)
|
41
|
-
plug.new(*args, **kwargs)
|
42
|
-
else
|
43
|
-
Plugs.read(plug).new(*args, **kwargs)
|
44
|
-
end
|
31
|
+
@resp_plugs << build_plug(plug, *args, **kwargs)
|
45
32
|
end
|
46
33
|
|
47
34
|
def namespace(_name, &block)
|
48
|
-
namespaced_definition = ResourcesDefinition.new(req_plugs, resp_plugs)
|
35
|
+
namespaced_definition = ResourcesDefinition.new(@req_plugs, resp_plugs)
|
49
36
|
namespaced_definition.instance_eval(&block)
|
50
37
|
@resources << namespaced_definition.resources
|
51
38
|
end
|
52
39
|
|
53
40
|
private
|
54
41
|
|
42
|
+
def build_plug(plug, *args, **kwargs)
|
43
|
+
if plug.respond_to?(:call)
|
44
|
+
plug
|
45
|
+
elsif plug.respond_to?(:new)
|
46
|
+
plug.new(*args, **kwargs)
|
47
|
+
else
|
48
|
+
Plugs.read(plug).new(*args, **kwargs)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
55
52
|
def build_resource(verb, name, options)
|
56
53
|
Resource.new(
|
57
|
-
verb: verb,
|
54
|
+
verb: verb,
|
55
|
+
name: name,
|
56
|
+
base: @base,
|
57
|
+
req_plugs: @req_plugs,
|
58
|
+
resp_plugs: @resp_plugs,
|
59
|
+
**with_defaults(options)
|
58
60
|
)
|
59
61
|
end
|
60
62
|
|
@@ -4,11 +4,9 @@ require 'zeitwerk'
|
|
4
4
|
|
5
5
|
Zeitwerk::Loader.for_gem.setup
|
6
6
|
|
7
|
-
|
8
|
-
Plugs.register :camelize_body, HttpClientGenerator::Plugs::CamelizeBody
|
9
|
-
Plugs.register :underscore_response, HttpClientGenerator::Plugs::UnderscoreResponse
|
10
|
-
Plugs.register :set_request_id, HttpClientGenerator::Plugs::SetRequestId
|
7
|
+
Dir["#{__dir__}/http_client_generator/plugs/*.rb"].each { |f| require f }
|
11
8
|
|
9
|
+
module HttpClientGenerator
|
12
10
|
def self.included(base)
|
13
11
|
super
|
14
12
|
|
@@ -30,7 +28,7 @@ module HttpClientGenerator
|
|
30
28
|
method_name = :"#{resource.name}_#{resource.verb}"
|
31
29
|
|
32
30
|
define_singleton_method(method_name) do |url_options = {}, body: nil, **rest_args|
|
33
|
-
resource.perform_request(@url_helper,
|
31
|
+
resource.perform_request(@url_helper, url_options, body, rest_args)
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http-client-generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Egorov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -49,6 +49,9 @@ files:
|
|
49
49
|
- lib/http_client_generator.rb
|
50
50
|
- lib/http_client_generator/plugs.rb
|
51
51
|
- lib/http_client_generator/plugs/camelize_body.rb
|
52
|
+
- lib/http_client_generator/plugs/encode_json_response.rb
|
53
|
+
- lib/http_client_generator/plugs/enforce_json_response.rb
|
54
|
+
- lib/http_client_generator/plugs/set_header.rb
|
52
55
|
- lib/http_client_generator/plugs/set_request_id.rb
|
53
56
|
- lib/http_client_generator/plugs/underscore_response.rb
|
54
57
|
- lib/http_client_generator/request.rb
|