foobara-http-api-command 0.0.6 → 0.0.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e260c28660b274db261a8cb7516ba3763ab534f7155b5f3dff42f6e14d735c2
4
- data.tar.gz: 126c8c76cd2a14d54d5a3da3f44e79f2da110d253b8e7d387d002eb3b5c17e59
3
+ metadata.gz: 8b335997bb51113f94152e97d3c826f83e10a287fff2393f7cd74743e16b8646
4
+ data.tar.gz: dc639cf31c66346b20a3e0d644a6a0eff577620efbb370e714f2f71b231e0c8b
5
5
  SHA512:
6
- metadata.gz: 1f5afa79b0cc20bbd8e1a056cac98e62e9490af23f085f3387a68f139eb30fc5ff31d24900ce249baa6910c7ad73dfd8e229aa7afcce9673c394dad4522cd77d
7
- data.tar.gz: 2d892b329fa23240b6da945db808eac5f518d1a0cca1ff68f9be084d95e4fde0699bbbbb0529897bb9a6925ce6ec27faad40df4ebc986745671d067ead60d0ee
6
+ metadata.gz: 6d3fdd94f82b6acec6580ecfb880747257e1c942438925cdc53bebe42bc29536275302f2e25ff93741e197069dda9a8d51236b4a1ceb09b3c170fafdc0b93788
7
+ data.tar.gz: 98210d758a2d47d469594310789ddd919f7244fd5948d03e9a6ecd7067a07b0aa787052a4cb5c803d51086a408a9586f27a201324810dcd8bcf95f77b891c4e3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.0.8] - 2025-03-02
2
+
3
+ - Fix bug caused by reusing Net::HTTP objects for ssl connections
4
+
5
+ ## [0.0.7] - 2025-02-27
6
+
7
+ - Add http_timeout option
8
+ - Bump Ruby to 3.4.2
9
+
1
10
  ## [0.0.6] - 2025-02-17
2
11
 
3
12
  - Support separate base_url and path helpers
@@ -4,16 +4,29 @@ module Foobara
4
4
  module Url
5
5
  include Concern
6
6
 
7
+ def net_http
8
+ # unclear why, but, seems like if we're doing SSL we have to be single-use, hmmm...
9
+ return @net_http if @net_http && !@net_http.use_ssl?
10
+
11
+ @net_http ||= self.class.compute_http(self)
12
+ end
13
+
7
14
  def api_url
8
15
  @api_url ||= self.class.compute_api_url(self)
9
16
  end
10
17
 
18
+ def api_uri_object
19
+ @api_uri_object ||= self.class.compute_uri_object(self)
20
+ end
21
+
11
22
  inherited_overridable_class_attr_accessor :foobara_base_url_block,
12
23
  :foobara_base_url,
13
24
  :foobara_path,
25
+ :foobara_path_block,
14
26
  :foobara_url_block,
15
27
  :foobara_url,
16
- :foobara_http_method
28
+ :foobara_http_method,
29
+ :foobara_http_timeout
17
30
 
18
31
  module ClassMethods
19
32
  def http_method(method = nil)
@@ -42,9 +55,21 @@ module Foobara
42
55
  end
43
56
  end
44
57
 
45
- def path(path = nil)
46
- if path
58
+ def path(path = nil, &block)
59
+ if block_given?
60
+ unless path.nil?
61
+ # :nocov:
62
+ raise ArgumentError, "Cannot specify both path and block"
63
+ # :nocov:
64
+ end
65
+
66
+ self.foobara_path_block = block
67
+ elsif path
47
68
  self.foobara_path = path
69
+ else
70
+ # :nocov:
71
+ raise ArgumentError, "No path specified"
72
+ # :nocov:
48
73
  end
49
74
  end
50
75
 
@@ -66,29 +91,79 @@ module Foobara
66
91
  end
67
92
  end
68
93
 
69
- def compute_api_url(command)
70
- if foobara_url
71
- foobara_url
72
- elsif foobara_url_block
73
- command.instance_eval(&foobara_url_block)
74
- elsif foobara_path
75
- base = if foobara_base_url
76
- foobara_base_url
77
- elsif foobara_base_url_block
78
- command.instance_eval(&foobara_base_url_block)
79
- else
80
- # :nocov:
81
- raise "Not able to determine the api url. Did you remember to call .url or .path and .base_url?"
82
- # :nocov:
83
- end
84
-
85
- "#{base}#{foobara_path}"
94
+ def http_timeout(timeout = nil)
95
+ if timeout
96
+ self.foobara_http_timeout = timeout
86
97
  else
87
- # :nocov:
88
- raise "Not able to determine the api url. Did you remember to call .url or .path and .base_url?"
89
- # :nocov:
98
+ foobara_http_timeout
90
99
  end
91
100
  end
101
+
102
+ def compute_uri_object(command)
103
+ return @compute_uri_object if @compute_uri_object
104
+
105
+ uri = URI(compute_api_url(command))
106
+
107
+ unless foobara_base_url_block || foobara_path_block || foobara_url_block
108
+ @compute_uri_object = uri
109
+ end
110
+
111
+ uri
112
+ end
113
+
114
+ def compute_api_url(command)
115
+ return @compute_api_url if @compute_api_url
116
+
117
+ url = if foobara_url
118
+ foobara_url
119
+ elsif foobara_url_block
120
+ command.instance_eval(&foobara_url_block)
121
+ else
122
+ path = if foobara_path
123
+ foobara_path
124
+ elsif foobara_path_block
125
+ command.instance_eval(&foobara_path_block)
126
+ end
127
+
128
+ base = if foobara_base_url
129
+ foobara_base_url
130
+ elsif foobara_base_url_block
131
+ command.instance_eval(&foobara_base_url_block)
132
+ else
133
+ # :nocov:
134
+ raise "Not able to determine the api url. " \
135
+ "Did you remember to call .url or .path and .base_url?"
136
+ # :nocov:
137
+ end
138
+
139
+ "#{base}#{path}"
140
+ end
141
+
142
+ unless foobara_base_url_block || foobara_path_block || foobara_url_block
143
+ @compute_api_url = url
144
+ end
145
+
146
+ url
147
+ end
148
+
149
+ def compute_http(command)
150
+ # unclear why, but, seems like if we're doing SSL we have to be single-use, hmmm...
151
+ return @net_http if @net_http && !@net_http.use_ssl?
152
+
153
+ uri = URI(command.api_url)
154
+ computed_http = Net::HTTP.new(uri.host, uri.port).tap do |http|
155
+ http.use_ssl = uri.scheme == "https"
156
+ if http_timeout
157
+ http.read_timeout = http_timeout
158
+ end
159
+ end
160
+
161
+ unless foobara_base_url_block || foobara_path_block || foobara_url_block
162
+ @net_http = computed_http
163
+ end
164
+
165
+ computed_http
166
+ end
92
167
  end
93
168
  end
94
169
  end
@@ -27,19 +27,28 @@ module Foobara
27
27
  end
28
28
 
29
29
  def issue_http_request
30
- case self.class.http_method
31
- when :get
32
- uri = URI(api_url)
33
- uri.query = URI.encode_www_form(request_body)
34
- self.response = Net::HTTP.get_response(uri, request_headers)
35
- when :post
36
- uri = URI.parse(api_url)
37
- self.response = Net::HTTP.post(uri, JSON.generate(request_body), request_headers)
38
- else
39
- # :nocov:
40
- raise "Unknown http method #{self.class.http_method}"
41
- # :nocov:
42
- end
30
+ request = case self.class.http_method
31
+ when :get
32
+ uri = if request_body.empty?
33
+ api_uri_object
34
+ else
35
+ api_uri_object.dup.tap do |new_uri|
36
+ new_uri.query = URI.encode_www_form(request_body)
37
+ end
38
+ end
39
+
40
+ Net::HTTP::Get.new(uri.request_uri, request_headers)
41
+ when :post
42
+ Net::HTTP::Post.new(api_uri_object.request_uri, request_headers).tap do |post|
43
+ post.body = JSON.generate(request_body)
44
+ end
45
+ else
46
+ # :nocov:
47
+ raise "Unknown http method #{self.class.http_method}"
48
+ # :nocov:
49
+ end
50
+
51
+ self.response = net_http.request(request)
43
52
  end
44
53
 
45
54
  def build_result
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-http-api-command
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-17 00:00:00.000000000 Z
10
+ date: 2025-03-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: foobara