einvoicing-connect 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cce75b4168fa4248df3041674f78b6ef6b78d2a0bc7e709c9605f155a9f45f0a
4
- data.tar.gz: 1518c2b142d792049a7333afea8cf997fb72dd4f542de1664bf454a4ebf66bb5
3
+ metadata.gz: 13a511ead5bfecf4da045e1889c01e3a736b02fe487958be1535e0aa2b1dc727
4
+ data.tar.gz: 1c46ac87ca308378ba2278f87e058452c0d863f5089d5834c237f702626ae8db
5
5
  SHA512:
6
- metadata.gz: 80ff9931f78d3120442608d09e5b2a21b19f28001c503bc1ee82951d648b160fe622f840bd375f33eab9fc1aba6254a91176e28fc72ccc499a554225992ce488
7
- data.tar.gz: c0b0da87b89ff7335f7d6a35162e1d9bbc0a89600885b85a1774b56bde91a0bc7ec789f8ea322e0e57c3554813fba9181486fc28a89e54dec5ae8e385e4db532
6
+ metadata.gz: 35eace814e8bd81f57b6e93ec1868d92413af878c8a2a212d5699ac71ae65757cd8ab11a75c526ba6c9e904d5635fe4e3be93b4cf4b452f431ce2019d6318889
7
+ data.tar.gz: 531420b8ac1aeca48db815f4fa5fbf8cd78eaa6bc40eabbe4295e31625b0253f65900b1ba285a05b1a36185afd184f97e92aea3175b38b81b87a129b98de97f0
data/README.md CHANGED
@@ -44,6 +44,18 @@ creds = Einvoicing::Connect::FR::Pennylane::Credentials.oauth(
44
44
 
45
45
  OAuth credentials are mutable — when an expired token is refreshed automatically, `creds.access_token`, `creds.refresh_token`, and `creds.expires_at` are updated in place so you can persist the new values.
46
46
 
47
+ #### Timeouts
48
+
49
+ Every request is bounded: 5 s to connect, 30 s to read and 30 s to send, never retried. Pass your own to the submitter or the client:
50
+
51
+ ```ruby
52
+ submitter = Einvoicing::Connect::FR::Pennylane::EInvoiceSubmitter.new(
53
+ credentials: creds, open_timeout: 3, read_timeout: 15, write_timeout: 20
54
+ )
55
+ ```
56
+
57
+ A request that runs out of time raises `Einvoicing::Connect::FR::Pennylane::TimeoutError`, a `Pennylane::Error`.
58
+
47
59
  #### Submitting an e-invoice
48
60
 
49
61
  ```ruby
@@ -12,3 +12,4 @@ en:
12
12
  auth_failed: "Pennylane authentication failed — check your API key"
13
13
  oauth_failed: "Pennylane OAuth token refresh failed (HTTP %{code}): %{body}"
14
14
  submission_failed: "Pennylane invoice submission failed: %{code} %{body}"
15
+ timeout: "Pennylane did not answer in time (%{error})"
@@ -12,3 +12,4 @@ fr:
12
12
  auth_failed: "Échec de l'authentification Pennylane — vérifiez votre clé API"
13
13
  oauth_failed: "Échec du renouvellement du jeton OAuth Pennylane (HTTP %{code}) : %{body}"
14
14
  submission_failed: "Échec de la soumission de la facture Pennylane : %{code} %{body}"
15
+ timeout: "Pennylane n'a pas répondu à temps (%{error})"
@@ -18,10 +18,23 @@ module Einvoicing
18
18
  OAUTH_BASE_URL = "https://app.pennylane.com"
19
19
  OAUTH_SANDBOX_URL = "https://sandbox.pennylane.com"
20
20
 
21
- def initialize(credentials:, sandbox: false)
22
- @credentials = credentials
23
- @base_url = sandbox ? SANDBOX_URL : BASE_URL
24
- @oauth_base = sandbox ? OAUTH_SANDBOX_URL : OAUTH_BASE_URL
21
+ # Seconds per phase. Net::HTTP's defaults are 60 s each, and it sends
22
+ # an idempotent request again after a timeout: a caller bounded by a
23
+ # web request or a job would wait minutes on a silent Pennylane.
24
+ OPEN_TIMEOUT = 5
25
+ READ_TIMEOUT = 30
26
+ WRITE_TIMEOUT = 30
27
+
28
+ NETWORK_TIMEOUTS = [ Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout ].freeze
29
+
30
+ def initialize(credentials:, sandbox: false, open_timeout: OPEN_TIMEOUT,
31
+ read_timeout: READ_TIMEOUT, write_timeout: WRITE_TIMEOUT)
32
+ @credentials = credentials
33
+ @base_url = sandbox ? SANDBOX_URL : BASE_URL
34
+ @oauth_base = sandbox ? OAUTH_SANDBOX_URL : OAUTH_BASE_URL
35
+ @open_timeout = open_timeout
36
+ @read_timeout = read_timeout
37
+ @write_timeout = write_timeout
25
38
  end
26
39
 
27
40
  # Submit a Factur-X PDF binary to the e-invoice import endpoint.
@@ -99,7 +112,7 @@ module Einvoicing
99
112
  client_id: @credentials.client_id,
100
113
  client_secret: @credentials.client_secret
101
114
  )
102
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
115
+ res = perform(uri, req)
103
116
  unless res.is_a?(Net::HTTPSuccess)
104
117
  raise OAuthError, ::I18n.t("einvoicing.connect.pennylane.oauth_failed",
105
118
  code: res.code, body: res.body.to_s[0, 200])
@@ -112,27 +125,23 @@ module Einvoicing
112
125
  end
113
126
 
114
127
  def post(path, body)
115
- uri = URI("#{@base_url}#{path}")
116
- http = Net::HTTP.new(uri.host, uri.port)
117
- http.use_ssl = true
128
+ uri = URI("#{@base_url}#{path}")
118
129
  req = Net::HTTP::Post.new(uri)
119
130
  req["Authorization"] = "Bearer #{current_token}"
120
131
  req["Content-Type"] = "application/json"
121
132
  req.body = JSON.generate(body)
122
- handle_response(http.request(req))
133
+ handle_response(perform(uri, req))
123
134
  end
124
135
 
125
136
  def post_multipart(path, file:, filename:, invoice_options: nil)
126
137
  boundary = "RubyBoundary#{SecureRandom.hex(16)}"
127
138
  uri = URI("#{@base_url}#{path}")
128
- http = Net::HTTP.new(uri.host, uri.port)
129
- http.use_ssl = true
130
139
  req = Net::HTTP::Post.new(uri)
131
140
  req["Authorization"] = "Bearer #{current_token}"
132
141
  req["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
133
142
  req.body = build_multipart_body(boundary, file: file, filename: filename,
134
143
  invoice_options: invoice_options)
135
- handle_response(http.request(req))
144
+ handle_response(perform(uri, req))
136
145
  end
137
146
 
138
147
  def build_multipart_body(boundary, file:, filename:, invoice_options:)
@@ -154,12 +163,25 @@ module Einvoicing
154
163
  end
155
164
 
156
165
  def get(path)
157
- uri = URI("#{@base_url}#{path}")
158
- http = Net::HTTP.new(uri.host, uri.port)
159
- http.use_ssl = true
166
+ uri = URI("#{@base_url}#{path}")
160
167
  req = Net::HTTP::Get.new(uri)
161
168
  req["Authorization"] = "Bearer #{current_token}"
162
- handle_response(http.request(req))
169
+ handle_response(perform(uri, req))
170
+ end
171
+
172
+ # One connection per request, every phase bounded, never retried: a
173
+ # POST that timed out may have reached Pennylane, and sending it
174
+ # again would import the invoice twice.
175
+ def perform(uri, req)
176
+ http = Net::HTTP.new(uri.host, uri.port)
177
+ http.use_ssl = true
178
+ http.open_timeout = @open_timeout
179
+ http.read_timeout = @read_timeout
180
+ http.write_timeout = @write_timeout
181
+ http.max_retries = 0
182
+ http.request(req)
183
+ rescue *NETWORK_TIMEOUTS => e
184
+ raise TimeoutError, ::I18n.t("einvoicing.connect.pennylane.timeout", error: e.class.name)
163
185
  end
164
186
 
165
187
  def handle_response(response)
@@ -24,8 +24,10 @@ module Einvoicing
24
24
  # submitter.submit(invoice, pdf: pdf_bytes)
25
25
  # # creds.access_token / creds.refresh_token are updated after a refresh
26
26
  class EInvoiceSubmitter
27
- def initialize(credentials:, sandbox: false)
28
- @client = Client.new(credentials: credentials, sandbox: sandbox)
27
+ # +timeouts+: +open_timeout+, +read_timeout+, +write_timeout+ in
28
+ # seconds, handed to the Client (its constants by default).
29
+ def initialize(credentials:, sandbox: false, **timeouts)
30
+ @client = Client.new(credentials: credentials, sandbox: sandbox, **timeouts)
29
31
  end
30
32
 
31
33
  # Generates a Factur-X PDF from +invoice+ and +pdf+, then submits it.
@@ -8,6 +8,10 @@ module Einvoicing
8
8
  AuthError = Class.new(Error) unless defined?(AuthError)
9
9
  OAuthError = Class.new(Error) unless defined?(OAuthError)
10
10
  SubmissionError = Class.new(Error) unless defined?(SubmissionError)
11
+ # Asked of this module alone: `defined?` would also find a top-level
12
+ # ::TimeoutError an application or another gem defines, and this one
13
+ # would then never exist under Pennylane::Error.
14
+ TimeoutError = Class.new(Error) unless const_defined?(:TimeoutError, false)
11
15
  end
12
16
  end
13
17
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Einvoicing
4
4
  module Connect
5
- VERSION = "0.4.0"
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: einvoicing-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Le Ray
@@ -230,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
230
230
  - !ruby/object:Gem::Version
231
231
  version: '0'
232
232
  requirements: []
233
- rubygems_version: 4.0.19
233
+ rubygems_version: 4.0.20
234
234
  specification_version: 4
235
235
  summary: Platform connectors for the einvoicing gem — Pennylane, PPF/Chorus Pro, SIRET
236
236
  lookup