einvoicing-connect 0.3.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: c1d5afb37eec4ff165c07ca7f35ca30470385c3c9820fe012f69bdb2b663d347
4
- data.tar.gz: 79882e661a1683484e4a5ffdcc53640916e70b0b5e997a85b0a58ee14e74dfb2
3
+ metadata.gz: 13a511ead5bfecf4da045e1889c01e3a736b02fe487958be1535e0aa2b1dc727
4
+ data.tar.gz: 1c46ac87ca308378ba2278f87e058452c0d863f5089d5834c237f702626ae8db
5
5
  SHA512:
6
- metadata.gz: c8f426765e61378bbbb5a8e7c7ea0860b93c9e73d0be38287b7d690731a095277ee62ed4d97570c4181f3086f6eae5ceacc1e706b15cbb470a1be779b524c71d
7
- data.tar.gz: cec80f177749ff036fb921d011fa2f4b909647c1dfa29af808e49984a334cfe1ca4cb1ae0308eb20be6e0a0516212eb2bdb11f03d1f44b914e79ef94dcf34b9c
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
@@ -8,12 +8,8 @@ en:
8
8
  not_found: "Not found: %{body}"
9
9
  api_error: "API error %{code}: %{body}"
10
10
  structure_not_found: "Buyer SIRET %{siret} not found in Chorus Pro"
11
- missing_technical_account: "Chorus Pro technical account (login/password) is required for API submission"
12
11
  pennylane:
13
12
  auth_failed: "Pennylane authentication failed — check your API key"
14
13
  oauth_failed: "Pennylane OAuth token refresh failed (HTTP %{code}): %{body}"
15
14
  submission_failed: "Pennylane invoice submission failed: %{code} %{body}"
16
- invalid_invoice: "Invoice is missing required fields for Pennylane submission"
17
- fr:
18
- siret_api_error: "SIRET lookup failed: %{message}"
19
- siret_not_found: "No company found for SIREN: %{siren}"
15
+ timeout: "Pennylane did not answer in time (%{error})"
@@ -8,12 +8,8 @@ fr:
8
8
  not_found: "Non trouvé : %{body}"
9
9
  api_error: "Erreur API %{code} : %{body}"
10
10
  structure_not_found: "SIRET acheteur %{siret} non trouvé dans Chorus Pro"
11
- missing_technical_account: "Le compte technique Chorus Pro (identifiant/mot de passe) est requis pour la soumission API"
12
11
  pennylane:
13
12
  auth_failed: "Échec de l'authentification Pennylane — vérifiez votre clé API"
14
13
  oauth_failed: "Échec du renouvellement du jeton OAuth Pennylane (HTTP %{code}) : %{body}"
15
14
  submission_failed: "Échec de la soumission de la facture Pennylane : %{code} %{body}"
16
- invalid_invoice: "La facture est incomplète pour la soumission Pennylane"
17
- fr:
18
- siret_api_error: "Échec de la recherche SIRET : %{message}"
19
- siret_not_found: "Aucune entreprise trouvée pour le SIREN : %{siren}"
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.3.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.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Le Ray
@@ -11,18 +11,52 @@ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: einvoicing
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.9.3
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: 0.9.3
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '1.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: i18n
14
34
  requirement: !ruby/object:Gem::Requirement
15
35
  requirements:
16
36
  - - "~>"
17
37
  - !ruby/object:Gem::Version
18
- version: '0.5'
38
+ version: '1.0'
19
39
  type: :runtime
20
40
  prerelease: false
21
41
  version_requirements: !ruby/object:Gem::Requirement
22
42
  requirements:
23
43
  - - "~>"
24
44
  - !ruby/object:Gem::Version
25
- version: '0.5'
45
+ version: '1.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: base64
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ type: :runtime
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
26
60
  - !ruby/object:Gem::Dependency
27
61
  name: rspec
28
62
  requirement: !ruby/object:Gem::Requirement
@@ -121,6 +155,34 @@ dependencies:
121
155
  - - "~>"
122
156
  - !ruby/object:Gem::Version
123
157
  version: '3.0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: simplecov
160
+ requirement: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - "~>"
163
+ - !ruby/object:Gem::Version
164
+ version: '0.22'
165
+ type: :development
166
+ prerelease: false
167
+ version_requirements: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - "~>"
170
+ - !ruby/object:Gem::Version
171
+ version: '0.22'
172
+ - !ruby/object:Gem::Dependency
173
+ name: bundler-audit
174
+ requirement: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - "~>"
177
+ - !ruby/object:Gem::Version
178
+ version: '0.9'
179
+ type: :development
180
+ prerelease: false
181
+ version_requirements: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - "~>"
184
+ - !ruby/object:Gem::Version
185
+ version: '0.9'
124
186
  description: 'Adds French e-invoicing platform connectors to the einvoicing gem: Pennylane
125
187
  (Factur-X import), PPF/Chorus Pro submission, and SIRET lookup.'
126
188
  email:
@@ -168,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
230
  - !ruby/object:Gem::Version
169
231
  version: '0'
170
232
  requirements: []
171
- rubygems_version: 4.0.17
233
+ rubygems_version: 4.0.20
172
234
  specification_version: 4
173
235
  summary: Platform connectors for the einvoicing gem — Pennylane, PPF/Chorus Pro, SIRET
174
236
  lookup