mundosms 1.2.0 → 2.0.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 +4 -4
- data/README.md +2 -2
- data/lib/mundosms.rb +27 -22
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4aed4991adbb37132322dad8eeb59cb7b56514dc2c6e601db4f6455f9b63df26
|
|
4
|
+
data.tar.gz: 43f264354242b17704bce028287004e854d17e465b8aef1173cb4c521fff6530
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 502ea3b1dfa0eeb594c9142f1e8aa46d403900a9df433416f35eaaeed888183eaefc23d1d9fe6494d625609fcc304978585ea70818a18ee31a8fa73261062bff
|
|
7
|
+
data.tar.gz: 4d53781041980d22f67b220cdef425514a17e2c0ab34bb9bba583a1c4690c787411410c43bd8c4e920b2a101e08336e92b786dd3b86847e3c8a7a5a6dd44a101
|
data/README.md
CHANGED
|
@@ -30,7 +30,7 @@ end
|
|
|
30
30
|
begin
|
|
31
31
|
api.patch("/extensions/recepcion", { "record_calls" => true })
|
|
32
32
|
rescue MundoSMS::Error => e
|
|
33
|
-
puts [e.http, e.
|
|
33
|
+
puts [e.http, e.code, e.message, e.detail, e.request_id].join(" ")
|
|
34
34
|
end
|
|
35
35
|
```
|
|
36
36
|
|
|
@@ -39,7 +39,7 @@ end
|
|
|
39
39
|
- `all(path, query)` recorre todas las páginas de un listado (un `Enumerator::Lazy`: pide cada página al llegar a ella).
|
|
40
40
|
Al acabar, el último `next_cursor` queda en `last_cursor`; `last_rate_limit` es el cupo (`X-RateLimit-*`) de la última
|
|
41
41
|
respuesta.
|
|
42
|
-
- Cualquier error es un `MundoSMS::Error` con `http`, `
|
|
42
|
+
- Cualquier error es un `MundoSMS::Error` con `http`, `code`, `message`, `detail`, `data`, `request_id` y `response`.
|
|
43
43
|
Un WhatsApp sin confirmar (`202`, `data["state"]` `"unknown"`: puede haber salido) no es un error: vuelve la respuesta.
|
|
44
44
|
- Cada `POST` lleva una `Idempotency-Key`, la misma en todos sus intentos; la tuya, con
|
|
45
45
|
`api.post(path, data, idempotency_key: "pedido-881")`.
|
data/lib/mundosms.rb
CHANGED
|
@@ -10,18 +10,19 @@
|
|
|
10
10
|
# puts r["data"]["id"]
|
|
11
11
|
# api.all("/calls", { "from_datetime" => "2026-11-01", "stop_at_ongoing" => true }).each { |call| ... }
|
|
12
12
|
#
|
|
13
|
-
# Errores: excepción MundoSMS::Error con #http (código HTTP), #
|
|
14
|
-
# (la respuesta entera) y #request_id (la cabecera X-Request-Id). También cuando la API contesta con "status": "error",
|
|
13
|
+
# Errores: excepción MundoSMS::Error con #http (código HTTP), #code (la palabra de la API: "not_found", "invalid_parameter"…),
|
|
14
|
+
# #message, #detail, #data, #response (la respuesta entera) y #request_id (la cabecera X-Request-Id). También cuando la API contesta con "status": "error",
|
|
15
15
|
# venga con el código HTTP que venga. Un WhatsApp sin confirmar (202) no es un error: vuelve la respuesta, con
|
|
16
16
|
# data["state"] "unknown". Lo que no se puede mandar (una Idempotency-Key mal formada, un cuerpo que no es UTF-8) es un
|
|
17
17
|
# ArgumentError, y no se manda nada.
|
|
18
18
|
# Cada POST lleva una Idempotency-Key (un UUID nuevo en cada llamada, la misma en todos sus intentos; la tuya, con
|
|
19
|
-
# api.post(path, data, idempotency_key: "pedido-881")). Por eso un POST que falla por la red, o con 409
|
|
20
|
-
#
|
|
19
|
+
# api.post(path, data, idempotency_key: "pedido-881")). Por eso un POST que falla por la red, o con 409
|
|
20
|
+
# request_in_progress, se repite sin miedo: la API no lo hace dos veces. Si la respuesta es la guardada de la primera vez, trae
|
|
21
21
|
# "idempotent_replayed" => true.
|
|
22
22
|
# Reintenta: un 429, en cualquier método (no se hizo nada); un fallo de red, en cualquier método; un 5xx, solo en GET (un
|
|
23
|
-
# POST con 5xx no se repite:
|
|
24
|
-
#
|
|
23
|
+
# POST con 5xx no se repite: un 500 puede llegar con la operación hecha, y la API lo guarda con la Idempotency-Key, así que
|
|
24
|
+
# repetirlo con ella devolvería el mismo 500; un 503 no hizo nada, y puedes repetirlo tú con la misma clave). Espera lo que
|
|
25
|
+
# diga Retry-After (como mucho 60 s) o, si no lo trae, 1, 2, 4… s. Un DELETE que se repite tras quedarse sin respuesta y da 404 ya estaba hecho:
|
|
25
26
|
# vuelve como éxito, con data["changed"] false.
|
|
26
27
|
# Una redirección (3xx) no se sigue nunca: es un MundoSMS::Error (la API no redirige). Un 2xx que no es JSON (el HTML de un
|
|
27
28
|
# proxy o de un portal cautivo) también es un error; los ficheros que da la API (audio, imagen, PDF) vuelven por su
|
|
@@ -37,7 +38,7 @@ require "timeout"
|
|
|
37
38
|
require "uri"
|
|
38
39
|
|
|
39
40
|
module MundoSMS
|
|
40
|
-
VERSION = "
|
|
41
|
+
VERSION = "2.0.0"
|
|
41
42
|
DEFAULT_BASE_URL = "https://api.mundosms.es/v3"
|
|
42
43
|
MAX_RETRY_WAIT = 60 # segundos: lo más que se espera antes de un reintento, aunque Retry-After pida más
|
|
43
44
|
WEBHOOK_TOLERANCE = 300 # segundos: lo más que puede alejarse de tu hora la «t» de la firma de un aviso
|
|
@@ -48,16 +49,16 @@ module MundoSMS
|
|
|
48
49
|
# plazo agotado), http 0 y un mensaje que empieza por «No response from the API:».
|
|
49
50
|
class Error < StandardError
|
|
50
51
|
attr_reader :http, # el código HTTP; 0 si no hubo respuesta
|
|
51
|
-
:
|
|
52
|
+
:code, # el code de la API ("not_found", "invalid_parameter"…); nil si la respuesta no lo trae
|
|
52
53
|
:detail, # el detail de la respuesta, si lo trae
|
|
53
54
|
:data, # el data de la respuesta (en WhatsApp, el id del mensaje que puede haber salido)
|
|
54
55
|
:response, # la respuesta entera, decodificada; nil si no era JSON
|
|
55
56
|
:request_id # la cabecera X-Request-Id: con ella encuentras la petición en Logs API
|
|
56
57
|
|
|
57
|
-
def initialize(message, http: 0,
|
|
58
|
+
def initialize(message, http: 0, code: nil, detail: nil, data: nil, response: nil, request_id: nil)
|
|
58
59
|
super(message)
|
|
59
60
|
@http = http
|
|
60
|
-
@
|
|
61
|
+
@code = code
|
|
61
62
|
@detail = detail
|
|
62
63
|
@data = data
|
|
63
64
|
@response = response
|
|
@@ -70,7 +71,7 @@ module MundoSMS
|
|
|
70
71
|
|
|
71
72
|
# El X-Timestamp de una petición: milisegundos, y siempre mayor que el de la anterior de este proceso (también desde
|
|
72
73
|
# varios hilos). Dos peticiones iguales en el mismo milisegundo (un reintento tras un 429 con Retry-After: 0, o dos a la
|
|
73
|
-
# vez) firmarían igual, y la API rechazaría la segunda por repetida («409
|
|
74
|
+
# vez) firmarían igual, y la API rechazaría la segunda por repetida («409 duplicated_request»).
|
|
74
75
|
def self.timestamp
|
|
75
76
|
@timestamp_lock.synchronize do
|
|
76
77
|
@last_timestamp = [Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond), @last_timestamp + 1].max
|
|
@@ -199,7 +200,7 @@ module MundoSMS
|
|
|
199
200
|
if truthy(data["has_more"]) && truthy(data["results"]) && !truthy(cursor)
|
|
200
201
|
message = "MundoSMS: all() cannot go through #{path}: it has more (has_more) but no next_cursor, so the list is not " \
|
|
201
202
|
"paginated: raise limit or narrow the filters, and read it with get"
|
|
202
|
-
raise Error.new(message, http: 200,
|
|
203
|
+
raise Error.new(message, http: 200, data: data, response: r)
|
|
203
204
|
end
|
|
204
205
|
list_key ||= data.keys.find { |k| data[k].is_a?(Array) } # la lista es el primer campo que es una lista
|
|
205
206
|
(list_key && data[list_key].is_a?(Array) ? data[list_key] : []).each { |item| y << item }
|
|
@@ -281,14 +282,13 @@ module MundoSMS
|
|
|
281
282
|
end
|
|
282
283
|
return j unless j["status"] == "error"
|
|
283
284
|
end
|
|
284
|
-
if method == "DELETE" && after_network && status == 404 && j.is_a?(Hash) &&
|
|
285
|
+
if method == "DELETE" && after_network && status == 404 && j.is_a?(Hash) && api_code(j) != "route_not_found"
|
|
285
286
|
# lo borró el intento que se quedó sin respuesta
|
|
286
|
-
return { "status" => "success", "
|
|
287
|
-
"data" => { "state" => "deleted", "changed" => false } }
|
|
287
|
+
return { "status" => "success", "message" => "Already deleted", "data" => { "state" => "deleted", "changed" => false } }
|
|
288
288
|
end
|
|
289
289
|
if j.is_a?(Hash)
|
|
290
290
|
message = j["message"].is_a?(String) ? j["message"] : "Error"
|
|
291
|
-
raise Error.new(message, http: status,
|
|
291
|
+
raise Error.new(message, http: status, code: api_code(j), detail: text(j["detail"]), data: j["data"],
|
|
292
292
|
response: j, request_id: rid)
|
|
293
293
|
end
|
|
294
294
|
raise Error.new("HTTP #{status}", http: status, request_id: rid)
|
|
@@ -352,24 +352,29 @@ module MundoSMS
|
|
|
352
352
|
end
|
|
353
353
|
|
|
354
354
|
# Si se puede repetir: un 429 en cualquier método; un GET, ante un fallo de red o un 5xx; un PATCH o un DELETE, ante un
|
|
355
|
-
# fallo de red; un POST (con su Idempotency-Key), ante un fallo de red o un 409
|
|
356
|
-
# no:
|
|
355
|
+
# fallo de red; un POST (con su Idempotency-Key), ante un fallo de red o un 409 request_in_progress. Un 5xx de un POST
|
|
356
|
+
# no: un 500 puede haberse hecho y la API lo guarda con la clave (repetirlo devolvería el mismo 500), y un 503 no hizo
|
|
357
|
+
# nada, pero repetirlo con la misma clave se deja a tu código. Un 409 duplicated_request (la MISMA petición firmada llegó
|
|
357
358
|
# dos veces: la repitió algo entre medias) es un fallo de red más.
|
|
358
359
|
def retryable?(method, network, status, j, idem_key)
|
|
359
360
|
return true if status == 429
|
|
360
361
|
|
|
361
|
-
message = j.is_a?(Hash) && j["message"].is_a?(String) ? j["message"] : nil
|
|
362
362
|
network ||= duplicated?(status, j)
|
|
363
363
|
return network || status >= 500 if method == "GET"
|
|
364
364
|
return network if %w[PATCH DELETE].include?(method)
|
|
365
|
-
return network || (status == 409 &&
|
|
365
|
+
return network || (status == 409 && api_code(j) == "request_in_progress") if idem_key
|
|
366
366
|
|
|
367
367
|
false
|
|
368
368
|
end
|
|
369
369
|
|
|
370
|
-
# 409
|
|
370
|
+
# 409 duplicated_request: la MISMA petición firmada llegó dos veces (la repitió algo entre medias).
|
|
371
371
|
def duplicated?(status, j)
|
|
372
|
-
status == 409 &&
|
|
372
|
+
status == 409 && api_code(j) == "duplicated_request"
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# El code de una respuesta de error ("duplicated_request"…) como texto, o nil si no lo trae.
|
|
376
|
+
def api_code(j)
|
|
377
|
+
j.is_a?(Hash) ? text(j["code"]) : nil
|
|
373
378
|
end
|
|
374
379
|
|
|
375
380
|
# El cupo de la respuesta (X-RateLimit-Limit, -Remaining y -Reset, en enteros), o nil si no lo trae.
|