watchlogdev 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +152 -0
- data/examples/README.md +10 -0
- data/examples/json.rb +8 -0
- data/examples/request.rb +11 -0
- data/lib/watchlog/client.rb +241 -0
- data/lib/watchlog/endpoints.rb +72 -0
- data/lib/watchlog/errors.rb +47 -0
- data/lib/watchlog/operations.rb +134 -0
- data/lib/watchlog/responses.rb +45 -0
- data/lib/watchlog/serialization.rb +69 -0
- data/lib/watchlog/transport.rb +27 -0
- data/lib/watchlog/version.rb +5 -0
- data/lib/watchlog.rb +14 -0
- data/lib/watchlogdev.rb +4 -0
- data/sig/watchlog.rbs +84 -0
- metadata +65 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c715e71e98c5a23f1e19f192b3dd064028966301c07da8b3d9baae8890e34385
|
|
4
|
+
data.tar.gz: 338ad27f1afdf0ad8c473b9a967b5e39cff76cbe5c11c44f5e3a684a1614bee4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 47cbf18470c3ae13cfa01348f10d97b5bf96ad3c883cb5b4f57fe192191ed5f9f71fb1887d1047b868ed06122005eb318a0208c19adfe72b8ad4f5e5df6fa208
|
|
7
|
+
data.tar.gz: efe300cb904f125f13c7d2ca2dbf333d86b223d47c8d6b6115bfb6382251ca91e8a0754fa9f23e41700f77c69b724fabff0fef2d3fef9482bb5d1e6aa5c8f333
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Watchlog
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Watchlog Ruby SDK
|
|
2
|
+
|
|
3
|
+
The official Ruby SDK for the [Watchlog](https://www.watchlog.dev) watch price tracking API.
|
|
4
|
+
|
|
5
|
+
Track watch prices, by reference number or single listing, with a scheduled watchlist, price history and alerts.
|
|
6
|
+
|
|
7
|
+
The SDK exposes **9 methods**, one per documented endpoint, plus `clear_target_price` and a generic `request`. It only uses the Ruby standard library.
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
- Ruby 3.1 or newer
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
gem install watchlogdev
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or in your `Gemfile`:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem "watchlogdev"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Get your API key
|
|
26
|
+
|
|
27
|
+
[Sign up for Watchlog](https://www.watchlog.dev/signup) or sign in, then copy your API key from the [dashboard](https://www.watchlog.dev/app). The client reads `ENV["WATCHLOG_API_KEY"]` when no key is passed.
|
|
28
|
+
|
|
29
|
+
## Quick start
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
require "watchlog"
|
|
33
|
+
|
|
34
|
+
client = Watchlog::Client.new(api_key: ENV.fetch("WATCHLOG_API_KEY"))
|
|
35
|
+
response = client.search_watches(query: "submariner")
|
|
36
|
+
puts response.data
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Keyword arguments use the parameter names of the [API reference](https://www.watchlog.dev/docs). Unknown names raise `Watchlog::SerializationError`; use `request` for undocumented parameters.
|
|
40
|
+
|
|
41
|
+
## HTTP methods
|
|
42
|
+
|
|
43
|
+
Each method uses the HTTP verb of its endpoint. `GET` and `DELETE` send parameters in the query string; `POST` and `PATCH` send them as a JSON body, except `wait` on `/products/{id}/check`, which stays in the query string. `nil` values are never sent, so `update_product(target_price: nil)` leaves the target price as it is; `client.clear_target_price(id: product_id)` removes it (it sends `PATCH /products/{id}` with `{"target_price": null}`).
|
|
44
|
+
|
|
45
|
+
`nil` values are omitted, `false` and `0` are sent, and arrays repeat the parameter name.
|
|
46
|
+
|
|
47
|
+
## Any endpoint
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
response = client.request("GET", "/account")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Errors
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
begin
|
|
57
|
+
client.search_watches(query: "submariner")
|
|
58
|
+
rescue Watchlog::APIError => e
|
|
59
|
+
warn "HTTP #{e.status}: #{e.message}"
|
|
60
|
+
end
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
| Error | When |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `Watchlog::ConfigurationError` | Missing API key, invalid options or unsupported method. |
|
|
66
|
+
| `Watchlog::SerializationError` | A required parameter is missing, unknown or can't be sent. |
|
|
67
|
+
| `Watchlog::TransportError` | Network failure or timeout. |
|
|
68
|
+
| `Watchlog::APIError` | Non-2xx status (`status`, `error_code`, `error_message`, `response_body`, `raw_response`). `error_code` is the API's `error.code`, such as `"insufficient_credits"` or `"product_limit"`, or `nil` when the body has none. |
|
|
69
|
+
| `Watchlog::ResponseDecodingError` | A success response could not be decoded. |
|
|
70
|
+
|
|
71
|
+
All of them inherit from `Watchlog::Error`.
|
|
72
|
+
|
|
73
|
+
## Retries
|
|
74
|
+
|
|
75
|
+
Requests are retried up to 3 times after a network error, a `408`, `500`, `502`, `503` or `504`. Other statuses, such as `400`, `401`, `402`, `404` or `429`, and client-side timeouts are not retried: the API's rate limits last until the hour (or the day) is over, and on the free plan every retry would count as a request. The delay starts at 1 second and grows 1.5× per retry (capped at 30 seconds); a `Retry-After` header wins.
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
client = Watchlog::Client.new(max_retries: 5, retry_delay: 0.5)
|
|
79
|
+
client.search_watches(query: "submariner", request_options: { max_retries: 0 }) # one request without retries
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Batch requests
|
|
83
|
+
|
|
84
|
+
`batch` runs the block for every item on 10 threads (`concurrency:`) and returns one `BatchResult` per item, in input order. A failing item never stops the others.
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
inputs = ["submariner", "speedmaster", "royal oak"]
|
|
88
|
+
results = client.batch(inputs, concurrency: 5) { |value| client.search_watches(query: value) }
|
|
89
|
+
results.each do |result|
|
|
90
|
+
if result.ok?
|
|
91
|
+
puts result.value.data
|
|
92
|
+
else
|
|
93
|
+
warn "#{inputs[result.index]}: #{result.error.message}"
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
| Constant | Default | Description |
|
|
99
|
+
| --- | --- | --- |
|
|
100
|
+
| `Watchlog::Client::DEFAULT_CONCURRENCY` | `10` | Tasks run at the same time by batch |
|
|
101
|
+
| `Watchlog::Client::DEFAULT_TIMEOUT` | `120` | Request timeout (seconds) |
|
|
102
|
+
| `Watchlog::Client::MAX_RETRIES` | `3` | Retry attempts after a network error, a 408/5xx |
|
|
103
|
+
| `Watchlog::Client::RETRY_BACKOFF_FACTOR` | `1.5` | Exponential backoff multiplier |
|
|
104
|
+
| `Watchlog::Client::DEFAULT_RETRY_DELAY` | `1.0` | Delay before the first retry (seconds) |
|
|
105
|
+
|
|
106
|
+
## Timeouts and custom transports
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
client = Watchlog::Client.new(timeout: 60) # seconds, default 120
|
|
110
|
+
client.search_watches(query: "submariner", request_options: { timeout: 30 })
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
A transport is any object with `call(method:, url:, headers:, body:, timeout:)` returning `[status, headers, body]`; the default is `Watchlog::NetHTTPTransport`.
|
|
114
|
+
|
|
115
|
+
## Endpoints
|
|
116
|
+
|
|
117
|
+
9 methods, generated from the [OpenAPI document](https://www.watchlog.dev/openapi.json).
|
|
118
|
+
|
|
119
|
+
| Method | HTTP | Description |
|
|
120
|
+
| --- | --- | --- |
|
|
121
|
+
| `check_product` | POST `/products/{id}/check` | Asks for a check right away (1 credit), at most once per 5 minutes; earlier calls return the product unchanged. A paused product has to be… |
|
|
122
|
+
| `clear_target_price` | PATCH `/products/{id}` | Removes the target price: sends `{"target_price": null}`, which `update_product` cannot since `nil` values are omitted. |
|
|
123
|
+
| `delete_product` | DELETE `/products/{id}` | Removes the product and its price history. |
|
|
124
|
+
| `get_account` | GET `/account` | Your plan, the credits left (monthly allowance and packs), how many products you track and the credits they use per month, the credits each… |
|
|
125
|
+
| `get_product` | GET `/products/{id}` | The product's latest state and every price read of the period, oldest first. The history covers every read of this reference on this market… |
|
|
126
|
+
| `get_watch_history` | GET `/history` | Every price Watchlog read for a reference or listing id in a currency over the period, whoever tracks it, oldest first, with the lowest, hi… |
|
|
127
|
+
| `list_products` | GET `/products` | Your watchlist, newest first, with each product's latest state and its recent prices (trend). Filter with watch_id (and marketplace) to kno… |
|
|
128
|
+
| `search_watches` | GET `/search` | Watch models matching the keywords first (watch_id is their reference number, price the lowest asking price last seen across the whole mark… |
|
|
129
|
+
| `track_product` | POST `/products` | Adds a watch to your watchlist: a model by its reference number (126610LN), priced at the lowest asking price among the current listings th… |
|
|
130
|
+
| `update_product` | PATCH `/products/{id}` | Target price, drop alerts, check interval, or pause. Send only what changes. On the free plan, alerts (price drop, back on the market, targ… |
|
|
131
|
+
|
|
132
|
+
## All Watchlog SDKs
|
|
133
|
+
|
|
134
|
+
| SDK | Package | Install |
|
|
135
|
+
| --- | --- | --- |
|
|
136
|
+
| Node.js | [`@watchlogdev/sdk`](https://www.npmjs.com/package/@watchlogdev/sdk) | `npm install @watchlogdev/sdk` |
|
|
137
|
+
| Python | [`watchlogdev`](https://pypi.org/project/watchlogdev/) | `pip install watchlogdev` |
|
|
138
|
+
| Go | [`watchlog-go`](https://pkg.go.dev/github.com/watchlogdev/watchlog-go) | `go get github.com/watchlogdev/watchlog-go` |
|
|
139
|
+
| .NET | [`Watchlog`](https://www.nuget.org/packages/Watchlog) | `dotnet add package Watchlog` |
|
|
140
|
+
| Ruby | [`watchlogdev`](https://rubygems.org/gems/watchlogdev) | `gem install watchlogdev` |
|
|
141
|
+
| PHP | [`watchlogdev/sdk`](https://packagist.org/packages/watchlogdev/sdk) | `composer require watchlogdev/sdk` |
|
|
142
|
+
| Java | [`watchlog-java`](https://github.com/watchlogdev/watchlog-java) | build from source, see its README |
|
|
143
|
+
| CLI | [`@watchlogdev/cli`](https://www.npmjs.com/package/@watchlogdev/cli) | `npm install -g @watchlogdev/cli` |
|
|
144
|
+
| MCP server | [`@watchlogdev/mcp`](https://www.npmjs.com/package/@watchlogdev/mcp) | `npx -y @watchlogdev/mcp` |
|
|
145
|
+
|
|
146
|
+
## Trademarks
|
|
147
|
+
|
|
148
|
+
Watch brand and model names are trademarks of their respective owners. Watchlog is an independent service, not affiliated with any watch brand, dealer or marketplace.
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT — see [LICENSE](LICENSE).
|
data/examples/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
```sh
|
|
4
|
+
gem install watchlogdev
|
|
5
|
+
export WATCHLOG_API_KEY="YOUR_API_KEY"
|
|
6
|
+
ruby examples/json.rb
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
- `json.rb` — call the featured endpoint and print the JSON result.
|
|
10
|
+
- `request.rb` — call any path with `request` and handle API errors.
|
data/examples/json.rb
ADDED
data/examples/request.rb
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "watchlog"
|
|
4
|
+
|
|
5
|
+
client = Watchlog::Client.new # reads WATCHLOG_API_KEY
|
|
6
|
+
begin
|
|
7
|
+
response = client.request("GET", "/account")
|
|
8
|
+
puts response.data if response.json?
|
|
9
|
+
rescue Watchlog::APIError => e
|
|
10
|
+
warn "HTTP #{e.status}: #{e.message}"
|
|
11
|
+
end
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Watchlog
|
|
6
|
+
# Official Watchlog API client. Create one instance and reuse it.
|
|
7
|
+
class Client
|
|
8
|
+
include Operations
|
|
9
|
+
|
|
10
|
+
DEFAULT_BASE_URL = "https://www.watchlog.dev/api/v1"
|
|
11
|
+
DEFAULT_TIMEOUT = 120
|
|
12
|
+
# Tasks run at the same time by #batch.
|
|
13
|
+
DEFAULT_CONCURRENCY = 10
|
|
14
|
+
# Retries after a network error or a 408, 500, 502, 503 or 504 response (never a 429).
|
|
15
|
+
MAX_RETRIES = 3
|
|
16
|
+
# Each retry waits this many times longer than the previous one (a Retry-After header wins).
|
|
17
|
+
RETRY_BACKOFF_FACTOR = 1.5
|
|
18
|
+
# Seconds before the first retry.
|
|
19
|
+
DEFAULT_RETRY_DELAY = 1.0
|
|
20
|
+
MAX_RETRY_DELAY = 30.0
|
|
21
|
+
RETRYABLE_STATUSES = [408, 500, 502, 503, 504].freeze
|
|
22
|
+
API_KEY_ENVIRONMENT_VARIABLE = "WATCHLOG_API_KEY"
|
|
23
|
+
ERROR_MESSAGE_KEYS = %w[error message error_message detail].freeze
|
|
24
|
+
|
|
25
|
+
# @param api_key [String, nil] defaults to ENV["WATCHLOG_API_KEY"]
|
|
26
|
+
# @param base_url [String] API origin
|
|
27
|
+
# @param timeout [Numeric, nil] seconds per request; nil disables it
|
|
28
|
+
# @param headers [Hash] extra headers for every request
|
|
29
|
+
# @param max_retries [Integer] retries after a network error or a 408/5xx response; 0 disables them
|
|
30
|
+
# @param retry_delay [Numeric] seconds before the first retry, growing 1.5x per retry
|
|
31
|
+
# @param transport [#call] custom transport, see {NetHTTPTransport}
|
|
32
|
+
def initialize(api_key: nil, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, headers: {},
|
|
33
|
+
max_retries: MAX_RETRIES, retry_delay: DEFAULT_RETRY_DELAY, transport: nil)
|
|
34
|
+
api_key = ENV.fetch(API_KEY_ENVIRONMENT_VARIABLE, nil) if api_key.nil?
|
|
35
|
+
if !api_key.is_a?(String) || api_key.strip.empty?
|
|
36
|
+
raise ConfigurationError,
|
|
37
|
+
"A non-blank Watchlog API key is required. Pass api_key: or set #{API_KEY_ENVIRONMENT_VARIABLE}."
|
|
38
|
+
end
|
|
39
|
+
raise ConfigurationError, "The Watchlog API key cannot be used in an HTTP header." if api_key.match?(/[\r\n]/)
|
|
40
|
+
|
|
41
|
+
uri = URI.parse(base_url.to_s)
|
|
42
|
+
unless %w[http https].include?(uri.scheme) && uri.host
|
|
43
|
+
raise ConfigurationError, "base_url must be an absolute http or https URL."
|
|
44
|
+
end
|
|
45
|
+
raise ConfigurationError, "timeout must be a positive number or nil." if timeout && !(timeout.is_a?(Numeric) && timeout.positive?)
|
|
46
|
+
raise ConfigurationError, "transport must respond to #call." if transport && !transport.respond_to?(:call)
|
|
47
|
+
|
|
48
|
+
@api_key = api_key
|
|
49
|
+
@base_url = base_url.to_s.sub(%r{/+\z}, "")
|
|
50
|
+
@timeout = timeout
|
|
51
|
+
@headers = headers.to_h { |name, value| [name.to_s, value.to_s] }.freeze
|
|
52
|
+
raise ConfigurationError, "max_retries must be a non-negative Integer." unless max_retries.is_a?(Integer) && max_retries >= 0
|
|
53
|
+
raise ConfigurationError, "retry_delay must be a non-negative number." unless retry_delay.is_a?(Numeric) && retry_delay >= 0
|
|
54
|
+
|
|
55
|
+
@max_retries = max_retries
|
|
56
|
+
@retry_delay = retry_delay
|
|
57
|
+
@transport = transport || NetHTTPTransport.new
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def inspect
|
|
61
|
+
"#<#{self.class.name} base_url=#{@base_url.inspect}>"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Sends any request, including endpoints released after this SDK version.
|
|
65
|
+
# GET parameters travel in the query string; POST parameters travel in a JSON body.
|
|
66
|
+
# @return [JsonResponse, BinaryResponse]
|
|
67
|
+
def request(method, path, params = nil, timeout: nil, headers: nil, max_retries: nil)
|
|
68
|
+
send_request(method.to_s.upcase, path, params, { timeout: timeout, headers: headers, max_retries: max_retries }, :any)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Runs the block for every item on +concurrency+ threads (default 10) and returns one {BatchResult} per item,
|
|
72
|
+
# in input order. A failing item never stops the others; the client is thread-safe.
|
|
73
|
+
#
|
|
74
|
+
# results = client.batch(ids, concurrency: 5) { |id| client.some_endpoint(query: id) }
|
|
75
|
+
# @return [Array<BatchResult>]
|
|
76
|
+
def batch(items, concurrency: DEFAULT_CONCURRENCY)
|
|
77
|
+
raise ConfigurationError, "concurrency must be a positive Integer." unless concurrency.is_a?(Integer) && concurrency.positive?
|
|
78
|
+
raise ArgumentError, "batch needs a block" unless block_given?
|
|
79
|
+
|
|
80
|
+
list = items.to_a
|
|
81
|
+
results = Array.new(list.size)
|
|
82
|
+
queue = Queue.new
|
|
83
|
+
list.each_with_index { |item, index| queue << [item, index] }
|
|
84
|
+
queue.close
|
|
85
|
+
workers = Array.new([concurrency, list.size].min) do
|
|
86
|
+
Thread.new do
|
|
87
|
+
while (job = queue.pop)
|
|
88
|
+
item, index = job
|
|
89
|
+
results[index] = begin
|
|
90
|
+
BatchResult.new(index: index, value: yield(item), error: nil)
|
|
91
|
+
rescue StandardError => e
|
|
92
|
+
BatchResult.new(index: index, value: nil, error: e)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
workers.each(&:join)
|
|
98
|
+
results
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
def call_operation(name, params, options)
|
|
104
|
+
endpoint = ENDPOINTS.fetch(name)
|
|
105
|
+
options = (options || {}).transform_keys(&:to_sym)
|
|
106
|
+
method = (options[:method] || endpoint[:methods].first).to_s.upcase
|
|
107
|
+
unless endpoint[:methods].include?(method)
|
|
108
|
+
raise ConfigurationError, "#{endpoint[:path]} supports #{endpoint[:methods].join(', ')}, not #{method}."
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
params = params.transform_keys(&:to_s)
|
|
112
|
+
unknown = params.keys - endpoint[:params]
|
|
113
|
+
unless unknown.empty?
|
|
114
|
+
raise SerializationError.new(
|
|
115
|
+
"Unknown parameter(s) for #{name}: #{unknown.join(', ')}. Use #request for undocumented parameters.",
|
|
116
|
+
unknown.first
|
|
117
|
+
)
|
|
118
|
+
end
|
|
119
|
+
endpoint[:required].each do |required|
|
|
120
|
+
raise SerializationError.new("The \"#{required}\" parameter is required.", required) if params[required].nil?
|
|
121
|
+
end
|
|
122
|
+
send_request(method, endpoint[:path], params, options, endpoint[:response])
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def send_request(method, path, params, options, expected)
|
|
126
|
+
raise ConfigurationError, "Unsupported HTTP method: #{method.inspect}." unless %w[GET POST PATCH DELETE].include?(method)
|
|
127
|
+
raise ConfigurationError, 'The request path must start with "/".' unless path.is_a?(String) && path.start_with?("/")
|
|
128
|
+
|
|
129
|
+
path, pairs, body = Serialization.serialize(method, path, params)
|
|
130
|
+
url = @base_url + path
|
|
131
|
+
url += "?#{Serialization.encode_query(pairs)}" unless pairs.empty?
|
|
132
|
+
headers = {
|
|
133
|
+
"Accept" => "application/json, */*;q=0.8",
|
|
134
|
+
"User-Agent" => "watchlog-ruby/#{VERSION}"
|
|
135
|
+
}.merge(@headers).merge((options[:headers] || {}).to_h { |k, v| [k.to_s, v.to_s] })
|
|
136
|
+
headers["Authorization"] = "Bearer #{@api_key}"
|
|
137
|
+
payload = nil
|
|
138
|
+
if body
|
|
139
|
+
headers["Content-Type"] = "application/json"
|
|
140
|
+
payload = JSON.generate(body)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
max_retries = options[:max_retries] || @max_retries
|
|
144
|
+
attempt = 0
|
|
145
|
+
loop do
|
|
146
|
+
begin
|
|
147
|
+
status, response_headers, response_body = @transport.call(
|
|
148
|
+
method: method, url: url, headers: headers, body: payload, timeout: options[:timeout] || @timeout
|
|
149
|
+
)
|
|
150
|
+
rescue Timeout::Error => e
|
|
151
|
+
raise TransportError, "Watchlog request timed out: #{e.message}"
|
|
152
|
+
rescue StandardError => e
|
|
153
|
+
raise TransportError, "Watchlog request failed before an HTTP response was received: #{e.message}" if attempt >= max_retries
|
|
154
|
+
|
|
155
|
+
sleep(retry_delay(attempt, nil))
|
|
156
|
+
attempt += 1
|
|
157
|
+
next
|
|
158
|
+
end
|
|
159
|
+
raw = build_raw_response(status, response_headers, response_body)
|
|
160
|
+
return decode(raw, expected) if (200..299).cover?(raw.status)
|
|
161
|
+
raise api_error(raw) unless retryable?(raw) && attempt < max_retries
|
|
162
|
+
|
|
163
|
+
sleep(retry_delay(attempt, raw.headers["retry-after"]&.first))
|
|
164
|
+
attempt += 1
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def retryable?(raw)
|
|
169
|
+
RETRYABLE_STATUSES.include?(raw.status)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def retry_delay(attempt, retry_after)
|
|
173
|
+
seconds = Float(retry_after, exception: false) if retry_after
|
|
174
|
+
return [seconds, 60].min if seconds && seconds >= 0
|
|
175
|
+
|
|
176
|
+
[@retry_delay * (RETRY_BACKOFF_FACTOR**attempt), MAX_RETRY_DELAY].min
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def build_raw_response(status, headers, body)
|
|
180
|
+
normalized = {}
|
|
181
|
+
(headers || {}).each { |name, values| normalized[name.to_s.downcase] = Array(values).map(&:to_s).freeze }
|
|
182
|
+
RawResponse.new(
|
|
183
|
+
status: status.to_i,
|
|
184
|
+
headers: normalized.freeze,
|
|
185
|
+
content_type: normalized["content-type"]&.first,
|
|
186
|
+
body: body.to_s.b
|
|
187
|
+
)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def api_error(raw)
|
|
191
|
+
parsed = parse_json(raw.body)
|
|
192
|
+
message = nil
|
|
193
|
+
code = nil
|
|
194
|
+
if parsed.is_a?(Hash)
|
|
195
|
+
# The API answers {"error": {"code", "message"}}.
|
|
196
|
+
message = parsed["error"]["message"] if parsed["error"].is_a?(Hash) && parsed["error"]["message"].is_a?(String)
|
|
197
|
+
code = parsed["error"]["code"] if parsed["error"].is_a?(Hash) && parsed["error"]["code"].is_a?(String)
|
|
198
|
+
ERROR_MESSAGE_KEYS.each do |key|
|
|
199
|
+
value = parsed[key]
|
|
200
|
+
if value.is_a?(String) && !value.empty?
|
|
201
|
+
message = value
|
|
202
|
+
break
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
APIError.new(raw_response: raw, response_body: parsed == NOT_JSON ? nil : parsed, error_message: message, error_code: code)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def decode(raw, expected)
|
|
210
|
+
unless json_media_type?(raw.content_type)
|
|
211
|
+
if expected == :json
|
|
212
|
+
raise ResponseDecodingError.new("Watchlog returned a successful non-JSON response for a JSON operation.", raw)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
return BinaryResponse.new(bytes: raw.body, content_type: raw.content_type, raw_response: raw)
|
|
216
|
+
end
|
|
217
|
+
data = parse_json(raw.body)
|
|
218
|
+
if data == NOT_JSON
|
|
219
|
+
raise ResponseDecodingError.new("Watchlog returned a successful JSON response that could not be decoded.", raw)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
JsonResponse.new(data: data, raw_response: raw)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
NOT_JSON = Object.new.freeze
|
|
226
|
+
private_constant :NOT_JSON
|
|
227
|
+
|
|
228
|
+
def parse_json(body)
|
|
229
|
+
JSON.parse(body.dup.force_encoding(Encoding::UTF_8))
|
|
230
|
+
rescue JSON::ParserError, EncodingError
|
|
231
|
+
NOT_JSON
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def json_media_type?(content_type)
|
|
235
|
+
return false if content_type.nil?
|
|
236
|
+
|
|
237
|
+
media_type = content_type.split(";", 2).first.strip.downcase
|
|
238
|
+
media_type == "application/json" || media_type.end_with?("+json")
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Generated from the Watchlog OpenAPI document. Do not edit by hand.
|
|
4
|
+
|
|
5
|
+
module Watchlog
|
|
6
|
+
# Path, methods, response kind and parameter names of every documented operation.
|
|
7
|
+
ENDPOINTS = {
|
|
8
|
+
"check_product" => {
|
|
9
|
+
path: "/products/{id}/check",
|
|
10
|
+
methods: ["POST"].freeze,
|
|
11
|
+
response: :json,
|
|
12
|
+
required: ["id"].freeze,
|
|
13
|
+
params: ["id", "wait"].freeze
|
|
14
|
+
}.freeze,
|
|
15
|
+
"delete_product" => {
|
|
16
|
+
path: "/products/{id}",
|
|
17
|
+
methods: ["DELETE"].freeze,
|
|
18
|
+
response: :json,
|
|
19
|
+
required: ["id"].freeze,
|
|
20
|
+
params: ["id"].freeze
|
|
21
|
+
}.freeze,
|
|
22
|
+
"get_account" => {
|
|
23
|
+
path: "/account",
|
|
24
|
+
methods: ["GET"].freeze,
|
|
25
|
+
response: :json,
|
|
26
|
+
required: [].freeze,
|
|
27
|
+
params: [].freeze
|
|
28
|
+
}.freeze,
|
|
29
|
+
"get_product" => {
|
|
30
|
+
path: "/products/{id}",
|
|
31
|
+
methods: ["GET"].freeze,
|
|
32
|
+
response: :json,
|
|
33
|
+
required: ["id"].freeze,
|
|
34
|
+
params: ["id", "days"].freeze
|
|
35
|
+
}.freeze,
|
|
36
|
+
"get_watch_history" => {
|
|
37
|
+
path: "/history",
|
|
38
|
+
methods: ["GET"].freeze,
|
|
39
|
+
response: :json,
|
|
40
|
+
required: ["watch_id"].freeze,
|
|
41
|
+
params: ["watch_id", "marketplace", "days"].freeze
|
|
42
|
+
}.freeze,
|
|
43
|
+
"list_products" => {
|
|
44
|
+
path: "/products",
|
|
45
|
+
methods: ["GET"].freeze,
|
|
46
|
+
response: :json,
|
|
47
|
+
required: [].freeze,
|
|
48
|
+
params: ["watch_id", "marketplace", "filter", "page", "limit"].freeze
|
|
49
|
+
}.freeze,
|
|
50
|
+
"search_watches" => {
|
|
51
|
+
path: "/search",
|
|
52
|
+
methods: ["GET"].freeze,
|
|
53
|
+
response: :json,
|
|
54
|
+
required: ["query"].freeze,
|
|
55
|
+
params: ["query", "marketplace"].freeze
|
|
56
|
+
}.freeze,
|
|
57
|
+
"track_product" => {
|
|
58
|
+
path: "/products",
|
|
59
|
+
methods: ["POST"].freeze,
|
|
60
|
+
response: :json,
|
|
61
|
+
required: ["product"].freeze,
|
|
62
|
+
params: ["product", "marketplace", "target_price", "interval_minutes", "wait"].freeze
|
|
63
|
+
}.freeze,
|
|
64
|
+
"update_product" => {
|
|
65
|
+
path: "/products/{id}",
|
|
66
|
+
methods: ["PATCH"].freeze,
|
|
67
|
+
response: :json,
|
|
68
|
+
required: ["id"].freeze,
|
|
69
|
+
params: ["id", "target_price", "interval_minutes", "alert_on_drop", "paused"].freeze
|
|
70
|
+
}.freeze
|
|
71
|
+
}.freeze
|
|
72
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Watchlog
|
|
4
|
+
# Base class for every SDK-defined error.
|
|
5
|
+
class Error < StandardError; end
|
|
6
|
+
|
|
7
|
+
# The client configuration is missing or unusable.
|
|
8
|
+
class ConfigurationError < Error; end
|
|
9
|
+
|
|
10
|
+
# A request value cannot be represented safely on the wire.
|
|
11
|
+
class SerializationError < Error
|
|
12
|
+
attr_reader :param
|
|
13
|
+
|
|
14
|
+
def initialize(message, param = nil)
|
|
15
|
+
super(message)
|
|
16
|
+
@param = param
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The request failed before a complete HTTP response was received.
|
|
21
|
+
class TransportError < Error; end
|
|
22
|
+
|
|
23
|
+
# Watchlog returned a non-success HTTP status.
|
|
24
|
+
class APIError < Error
|
|
25
|
+
# error_code is the API's error.code (e.g. "insufficient_credits"), nil when the body has none.
|
|
26
|
+
attr_reader :status, :error_code, :error_message, :response_body, :raw_response
|
|
27
|
+
|
|
28
|
+
def initialize(raw_response:, response_body: nil, error_message: nil, error_code: nil)
|
|
29
|
+
super(error_message || "Watchlog API request failed with status #{raw_response.status}.")
|
|
30
|
+
@status = raw_response.status
|
|
31
|
+
@error_code = error_code
|
|
32
|
+
@error_message = error_message
|
|
33
|
+
@response_body = response_body
|
|
34
|
+
@raw_response = raw_response
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# A successful response could not be decoded as the documented representation.
|
|
39
|
+
class ResponseDecodingError < Error
|
|
40
|
+
attr_reader :raw_response
|
|
41
|
+
|
|
42
|
+
def initialize(message, raw_response)
|
|
43
|
+
super(message)
|
|
44
|
+
@raw_response = raw_response
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Generated from the Watchlog OpenAPI document. Do not edit by hand.
|
|
4
|
+
|
|
5
|
+
module Watchlog
|
|
6
|
+
# One method per documented endpoint. Mixed into {Client}.
|
|
7
|
+
module Operations
|
|
8
|
+
# Check the price now. Asks for a check right away (1 credit), at most once per 5 minutes; earlier calls return the product unchanged. A paused product has to be resumed first. The check runs in the background; with wait=true the answer waits for it. Read last_e…
|
|
9
|
+
#
|
|
10
|
+
# +POST /products/{id}/check+
|
|
11
|
+
#
|
|
12
|
+
# @param id [String] (required) product_id returned when the product was added
|
|
13
|
+
# @param wait [Boolean] Answer once the check is done (up to 45 seconds)
|
|
14
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
15
|
+
# @return [JsonResponse]
|
|
16
|
+
def check_product(request_options: nil, **params)
|
|
17
|
+
call_operation("check_product", params, request_options)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Stop tracking a product. Removes the product and its price history.
|
|
21
|
+
#
|
|
22
|
+
# +DELETE /products/{id}+
|
|
23
|
+
#
|
|
24
|
+
# @param id [String] (required) product_id returned when the product was added
|
|
25
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
26
|
+
# @return [JsonResponse]
|
|
27
|
+
def delete_product(request_options: nil, **params)
|
|
28
|
+
call_operation("delete_product", params, request_options)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Plan and remaining credits. Your plan, the credits left (monthly allowance and packs), how many products you track and the credits they use per month, the credits each alert email costs, the days of history you can read and, on the free plan, the API and MCP requests…
|
|
32
|
+
#
|
|
33
|
+
# +GET /account+
|
|
34
|
+
#
|
|
35
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
36
|
+
# @return [JsonResponse]
|
|
37
|
+
def get_account(request_options: nil, **params)
|
|
38
|
+
call_operation("get_account", params, request_options)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Get a product and its price history. The product's latest state and every price read of the period, oldest first. The history covers every read of this reference on this marketplace, including those made before you added it, so a product added today can come with months of pr…
|
|
42
|
+
#
|
|
43
|
+
# +GET /products/{id}+
|
|
44
|
+
#
|
|
45
|
+
# @param id [String] (required) product_id returned when the product was added
|
|
46
|
+
# @param days [Integer] History period in days. The free plan reads the last 30: history_days says which period was returned.
|
|
47
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
48
|
+
# @return [JsonResponse]
|
|
49
|
+
def get_product(request_options: nil, **params)
|
|
50
|
+
call_operation("get_product", params, request_options)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Price history of any watch. Every price Watchlog read for a reference or listing id in a currency over the period, whoever tracks it, oldest first, with the lowest, highest and average of those prices. A model with filters (126610LN:used:europe) has its own history,…
|
|
54
|
+
#
|
|
55
|
+
# +GET /history+
|
|
56
|
+
#
|
|
57
|
+
# @param watch_id [String] (required) watch reference number (126610LN), optionally narrowed to a condition and sellers' region (126610LN:used:europe), or listing id (id49860660)
|
|
58
|
+
# @param marketplace [String] Currency of the prices: com (USD), eu (EUR), co.uk (GBP)
|
|
59
|
+
# @param days [Integer] History period in days. The free plan reads the last 30: history_days says which period was returned.
|
|
60
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
61
|
+
# @return [JsonResponse]
|
|
62
|
+
def get_watch_history(request_options: nil, **params)
|
|
63
|
+
call_operation("get_watch_history", params, request_options)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# List tracked products. Your watchlist, newest first, with each product's latest state and its recent prices (trend). Filter with watch_id (and marketplace) to know if a product is tracked, or with filter to get the same lists as the app's tabs.
|
|
67
|
+
#
|
|
68
|
+
# +GET /products+
|
|
69
|
+
#
|
|
70
|
+
# @param watch_id [String] Only this reference
|
|
71
|
+
# @param marketplace [String] Only watches priced in this currency
|
|
72
|
+
# @param filter [String] Only the products of one of the app's watchlist filters. all: Every tracked product (default). drops: Cheaper now than when the product was added. target: At or below its target price. out_of_stock:…
|
|
73
|
+
# @param page [Integer] Page number
|
|
74
|
+
# @param limit [Integer] Products per page
|
|
75
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
76
|
+
# @return [JsonResponse]
|
|
77
|
+
def list_products(request_options: nil, **params)
|
|
78
|
+
call_operation("list_products", params, request_options)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Search watches. Watch models matching the keywords first (watch_id is their reference number, price the lowest asking price last seen across the whole market), then current listings (watch_id is the listing id, price its asking price), to find what to tra…
|
|
82
|
+
#
|
|
83
|
+
# +GET /search+
|
|
84
|
+
#
|
|
85
|
+
# @param query [String] (required) Keywords
|
|
86
|
+
# @param marketplace [String] Currency of the prices: com (USD), eu (EUR), co.uk (GBP)
|
|
87
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
88
|
+
# @return [JsonResponse]
|
|
89
|
+
def search_watches(request_options: nil, **params)
|
|
90
|
+
call_operation("search_watches", params, request_options)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Track a watch. Adds a watch to your watchlist: a model by its reference number (126610LN), priced at the lowest asking price among the current listings that match its filters, with how many there are as stock; or one listing by its id (id49860660) or its…
|
|
94
|
+
#
|
|
95
|
+
# +POST /products+
|
|
96
|
+
#
|
|
97
|
+
# @param product [String] (required) Reference number (126610LN), with its filters if any (126610LN:used:europe), listing id (id49860660) or listing link
|
|
98
|
+
# @param marketplace [String] Currency of the prices: com (USD), eu (EUR), co.uk (GBP)
|
|
99
|
+
# @param target_price [Numeric] Email the owner when the price goes at or below this amount (on the free plan, for the first 5 products with an alert). null removes it.
|
|
100
|
+
# @param interval_minutes [Integer] Minutes between two checks (1440 = daily). Checks faster than daily need a plan or a credit pack: every 60 minutes with a pack or Starter, 30 with Pro, 5 with Business.
|
|
101
|
+
# @param wait [Boolean] Answer once the first check is done (up to 45 seconds)
|
|
102
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
103
|
+
# @return [JsonResponse]
|
|
104
|
+
def track_product(request_options: nil, **params)
|
|
105
|
+
call_operation("track_product", params, request_options)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Change alerts and checks. Target price, drop alerts, check interval, or pause. Send only what changes. On the free plan, alerts (price drop, back on the market, target price) email for the first 5 products added among the active ones with an alert set; the others s…
|
|
109
|
+
#
|
|
110
|
+
# +PATCH /products/{id}+
|
|
111
|
+
#
|
|
112
|
+
# @param id [String] (required) product_id returned when the product was added
|
|
113
|
+
# @param target_price [Numeric] Email the owner when the price goes at or below this amount (on the free plan, for the first 5 products with an alert). null removes it.
|
|
114
|
+
# @param interval_minutes [Integer] Minutes between two checks (1440 = daily). Checks faster than daily need a plan or a credit pack: every 60 minutes with a pack or Starter, 30 with Pro, 5 with Business.
|
|
115
|
+
# @param alert_on_drop [Boolean] Email when the price drops or the product is back in stock (on the free plan, for the first 5 products with an alert)
|
|
116
|
+
# @param paused [Boolean] Stop checking (and charging) for now; false resumes
|
|
117
|
+
# @param request_options [Hash, nil] +:method+ ("GET"/"POST"), +:timeout+ (seconds), +:headers+
|
|
118
|
+
# @return [JsonResponse]
|
|
119
|
+
def update_product(request_options: nil, **params)
|
|
120
|
+
call_operation("update_product", params, request_options)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Remove the target price. Sends exactly {"target_price": null}: update_product cannot, since nil values are omitted.
|
|
124
|
+
#
|
|
125
|
+
# +PATCH /products/{id}+
|
|
126
|
+
#
|
|
127
|
+
# @param id [String] (required) product_id returned when the product was added
|
|
128
|
+
# @param request_options [Hash, nil] +:timeout+ (seconds), +:headers+, +:max_retries+
|
|
129
|
+
# @return [JsonResponse] the same response as update_product
|
|
130
|
+
def clear_target_price(id:, request_options: nil)
|
|
131
|
+
call_operation("update_product", { "id" => id, "target_price" => Serialization::JSON_NULL }, request_options)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Watchlog
|
|
4
|
+
# Buffered HTTP response: status, lower-cased multi-value headers, content type and body bytes.
|
|
5
|
+
RawResponse = Struct.new(:status, :headers, :content_type, :body, keyword_init: true)
|
|
6
|
+
|
|
7
|
+
# Outcome of one Client#batch item: +value+ when +ok?+, else +error+.
|
|
8
|
+
BatchResult = Struct.new(:index, :value, :error, keyword_init: true) do
|
|
9
|
+
def ok? = error.nil?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# A successful JSON response; +data+ is the parsed body (string keys).
|
|
13
|
+
class JsonResponse
|
|
14
|
+
attr_reader :data, :raw_response
|
|
15
|
+
|
|
16
|
+
def initialize(data:, raw_response:)
|
|
17
|
+
@data = data
|
|
18
|
+
@raw_response = raw_response
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def kind = :json
|
|
22
|
+
def json? = true
|
|
23
|
+
def binary? = false
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A successful non-JSON response (image, PDF, HTML…).
|
|
27
|
+
class BinaryResponse
|
|
28
|
+
attr_reader :bytes, :content_type, :raw_response
|
|
29
|
+
|
|
30
|
+
def initialize(bytes:, content_type:, raw_response:)
|
|
31
|
+
@bytes = bytes
|
|
32
|
+
@content_type = content_type
|
|
33
|
+
@raw_response = raw_response
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def kind = :binary
|
|
37
|
+
def json? = false
|
|
38
|
+
def binary? = true
|
|
39
|
+
|
|
40
|
+
# Writes the body to +path+ and returns the number of bytes written.
|
|
41
|
+
def save(path)
|
|
42
|
+
File.binwrite(path, bytes)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module Watchlog
|
|
7
|
+
# Request serialization: path filling, query pairs and JSON bodies.
|
|
8
|
+
module Serialization
|
|
9
|
+
QUERY_STRING_PARAMS = { "/products/{id}/check" => %w[wait] }.freeze
|
|
10
|
+
# @api private
|
|
11
|
+
# A body value sent as an explicit JSON null (nil values are omitted). Used by Operations#clear_target_price.
|
|
12
|
+
JSON_NULL = Object.new.freeze
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# Fills {name} path segments, then returns [path, query_pairs, body].
|
|
17
|
+
# nil values are omitted; false and 0 are sent.
|
|
18
|
+
def serialize(method, path_template, params)
|
|
19
|
+
raise SerializationError, "Request parameters must be a Hash." unless params.nil? || params.is_a?(Hash)
|
|
20
|
+
|
|
21
|
+
remaining = {}
|
|
22
|
+
(params || {}).each { |name, value| remaining[name.to_s] = value unless value.nil? }
|
|
23
|
+
path = path_template.gsub(/\{([^}]+)\}/) do
|
|
24
|
+
name = Regexp.last_match(1)
|
|
25
|
+
raise SerializationError.new("The \"#{name}\" path parameter is required.", name) unless remaining.key?(name)
|
|
26
|
+
|
|
27
|
+
URI.encode_www_form_component(wire_string(name, remaining.delete(name))).gsub("+", "%20")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
if %w[POST PATCH].include?(method)
|
|
31
|
+
# The API reads wait from the query string on POST /products/{id}/check.
|
|
32
|
+
globals = QUERY_STRING_PARAMS.fetch(path_template, []).filter_map { |name| [name, wire_string(name, remaining.delete(name))] if remaining.key?(name) }
|
|
33
|
+
return [path, globals, remaining.transform_values { |value| value.equal?(JSON_NULL) ? nil : value }]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
pairs = []
|
|
37
|
+
remaining.each do |name, value|
|
|
38
|
+
if value.is_a?(Array)
|
|
39
|
+
value.each { |item| pairs << [name, wire_string(name, item)] unless item.nil? }
|
|
40
|
+
else
|
|
41
|
+
pairs << [name, wire_string(name, value)]
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
[path, pairs, nil]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def wire_string(name, value)
|
|
48
|
+
case value
|
|
49
|
+
when String then value
|
|
50
|
+
when true, false, Integer, Symbol then value.to_s
|
|
51
|
+
when Float
|
|
52
|
+
raise SerializationError.new("The \"#{name}\" parameter must be a finite number.", name) unless value.finite?
|
|
53
|
+
|
|
54
|
+
value == value.floor ? value.to_i.to_s : value.to_s
|
|
55
|
+
when Hash, Array then JSON.generate(value)
|
|
56
|
+
else
|
|
57
|
+
raise SerializationError.new("The \"#{name}\" parameter has an unsupported type (#{value.class}).", name)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def encode_query(pairs)
|
|
62
|
+
pairs.map { |name, value| "#{escape(name)}=#{escape(value)}" }.join("&")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def escape(value)
|
|
66
|
+
URI.encode_www_form_component(value).gsub("+", "%20")
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module Watchlog
|
|
7
|
+
# Default transport on Net::HTTP. A custom transport is any object with
|
|
8
|
+
# +call(method:, url:, headers:, body:, timeout:)+ returning +[status, headers, body]+,
|
|
9
|
+
# where headers is a Hash of lower-cased names to arrays of values.
|
|
10
|
+
class NetHTTPTransport
|
|
11
|
+
def call(method:, url:, headers:, body:, timeout:)
|
|
12
|
+
uri = URI(url)
|
|
13
|
+
request = Net::HTTP.const_get(method.capitalize).new(uri)
|
|
14
|
+
headers.each { |name, value| request[name] = value }
|
|
15
|
+
request.body = body if body
|
|
16
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
17
|
+
http.use_ssl = uri.scheme == "https"
|
|
18
|
+
if timeout
|
|
19
|
+
http.open_timeout = timeout
|
|
20
|
+
http.read_timeout = timeout
|
|
21
|
+
http.write_timeout = timeout
|
|
22
|
+
end
|
|
23
|
+
response = http.start { |connection| connection.request(request) }
|
|
24
|
+
[response.code.to_i, response.to_hash, response.body || ""]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
data/lib/watchlog.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "watchlog/version"
|
|
4
|
+
require_relative "watchlog/errors"
|
|
5
|
+
require_relative "watchlog/responses"
|
|
6
|
+
require_relative "watchlog/serialization"
|
|
7
|
+
require_relative "watchlog/transport"
|
|
8
|
+
require_relative "watchlog/endpoints"
|
|
9
|
+
require_relative "watchlog/operations"
|
|
10
|
+
require_relative "watchlog/client"
|
|
11
|
+
|
|
12
|
+
# Official Ruby SDK for the Watchlog API (https://www.watchlog.dev).
|
|
13
|
+
module Watchlog
|
|
14
|
+
end
|
data/lib/watchlogdev.rb
ADDED
data/sig/watchlog.rbs
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
module Watchlog
|
|
2
|
+
VERSION: String
|
|
3
|
+
ENDPOINTS: Hash[String, Hash[Symbol, untyped]]
|
|
4
|
+
|
|
5
|
+
class Error < StandardError
|
|
6
|
+
end
|
|
7
|
+
class ConfigurationError < Error
|
|
8
|
+
end
|
|
9
|
+
class SerializationError < Error
|
|
10
|
+
attr_reader param: String?
|
|
11
|
+
def initialize: (String message, ?String? param) -> void
|
|
12
|
+
end
|
|
13
|
+
class TransportError < Error
|
|
14
|
+
end
|
|
15
|
+
class APIError < Error
|
|
16
|
+
attr_reader status: Integer
|
|
17
|
+
attr_reader error_code: String?
|
|
18
|
+
attr_reader error_message: String?
|
|
19
|
+
attr_reader response_body: untyped
|
|
20
|
+
attr_reader raw_response: RawResponse
|
|
21
|
+
end
|
|
22
|
+
class ResponseDecodingError < Error
|
|
23
|
+
attr_reader raw_response: RawResponse
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class RawResponse < Struct[untyped]
|
|
27
|
+
attr_accessor status: Integer
|
|
28
|
+
attr_accessor headers: Hash[String, Array[String]]
|
|
29
|
+
attr_accessor content_type: String?
|
|
30
|
+
attr_accessor body: String
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class JsonResponse
|
|
34
|
+
attr_reader data: untyped
|
|
35
|
+
attr_reader raw_response: RawResponse
|
|
36
|
+
def kind: () -> :json
|
|
37
|
+
def json?: () -> true
|
|
38
|
+
def binary?: () -> false
|
|
39
|
+
end
|
|
40
|
+
class BinaryResponse
|
|
41
|
+
attr_reader bytes: String
|
|
42
|
+
attr_reader content_type: String?
|
|
43
|
+
attr_reader raw_response: RawResponse
|
|
44
|
+
def kind: () -> :binary
|
|
45
|
+
def json?: () -> false
|
|
46
|
+
def binary?: () -> true
|
|
47
|
+
def save: (String path) -> Integer
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class BatchResult < Struct[untyped]
|
|
51
|
+
attr_accessor index: Integer
|
|
52
|
+
attr_accessor value: untyped
|
|
53
|
+
attr_accessor error: StandardError?
|
|
54
|
+
def ok?: () -> bool
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
type response = JsonResponse | BinaryResponse
|
|
58
|
+
type request_options = { ?method: String, ?timeout: Numeric?, ?headers: Hash[String, String], ?max_retries: Integer }
|
|
59
|
+
|
|
60
|
+
module Operations
|
|
61
|
+
def check_product: (id: String, ?wait: bool, ?request_options: request_options?) -> JsonResponse
|
|
62
|
+
def clear_target_price: (id: String, ?request_options: request_options?) -> JsonResponse
|
|
63
|
+
def delete_product: (id: String, ?request_options: request_options?) -> JsonResponse
|
|
64
|
+
def get_account: (?request_options: request_options?) -> JsonResponse
|
|
65
|
+
def get_product: (id: String, ?days: Integer, ?request_options: request_options?) -> JsonResponse
|
|
66
|
+
def get_watch_history: (watch_id: String, ?marketplace: String, ?days: Integer, ?request_options: request_options?) -> JsonResponse
|
|
67
|
+
def list_products: (?watch_id: String, ?marketplace: String, ?filter: String, ?page: Integer, ?limit: Integer, ?request_options: request_options?) -> JsonResponse
|
|
68
|
+
def search_watches: (query: String, ?marketplace: String, ?request_options: request_options?) -> JsonResponse
|
|
69
|
+
def track_product: (product: String, ?marketplace: String, ?target_price: Numeric, ?interval_minutes: Integer, ?wait: bool, ?request_options: request_options?) -> JsonResponse
|
|
70
|
+
def update_product: (id: String, ?target_price: Numeric, ?interval_minutes: Integer, ?alert_on_drop: bool, ?paused: bool, ?request_options: request_options?) -> JsonResponse
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
class Client
|
|
74
|
+
include Operations
|
|
75
|
+
DEFAULT_CONCURRENCY: Integer
|
|
76
|
+
MAX_RETRIES: Integer
|
|
77
|
+
RETRY_BACKOFF_FACTOR: Float
|
|
78
|
+
DEFAULT_RETRY_DELAY: Float
|
|
79
|
+
DEFAULT_TIMEOUT: Integer
|
|
80
|
+
def initialize: (?api_key: String?, ?base_url: String, ?timeout: Numeric?, ?headers: Hash[untyped, untyped], ?max_retries: Integer, ?retry_delay: Numeric, ?transport: untyped) -> void
|
|
81
|
+
def request: (String | Symbol method, String path, ?Hash[untyped, untyped]? params, ?timeout: Numeric?, ?headers: Hash[untyped, untyped]?, ?max_retries: Integer?) -> response
|
|
82
|
+
def batch: [I, T] (Enumerable[I] items, ?concurrency: Integer) { (I item) -> T } -> Array[BatchResult]
|
|
83
|
+
end
|
|
84
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: watchlogdev
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Watchlog
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-01 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Track watch prices, by reference number or single listing, with a scheduled
|
|
14
|
+
watchlist, price history and alerts.
|
|
15
|
+
email:
|
|
16
|
+
- contact@watchlog.dev
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- examples/README.md
|
|
24
|
+
- examples/json.rb
|
|
25
|
+
- examples/request.rb
|
|
26
|
+
- lib/watchlog.rb
|
|
27
|
+
- lib/watchlog/client.rb
|
|
28
|
+
- lib/watchlog/endpoints.rb
|
|
29
|
+
- lib/watchlog/errors.rb
|
|
30
|
+
- lib/watchlog/operations.rb
|
|
31
|
+
- lib/watchlog/responses.rb
|
|
32
|
+
- lib/watchlog/serialization.rb
|
|
33
|
+
- lib/watchlog/transport.rb
|
|
34
|
+
- lib/watchlog/version.rb
|
|
35
|
+
- lib/watchlogdev.rb
|
|
36
|
+
- sig/watchlog.rbs
|
|
37
|
+
homepage: https://www.watchlog.dev
|
|
38
|
+
licenses:
|
|
39
|
+
- MIT
|
|
40
|
+
metadata:
|
|
41
|
+
bug_tracker_uri: https://github.com/watchlogdev/watchlog-ruby/issues
|
|
42
|
+
documentation_uri: https://www.watchlog.dev/docs
|
|
43
|
+
homepage_uri: https://www.watchlog.dev
|
|
44
|
+
rubygems_mfa_required: 'true'
|
|
45
|
+
source_code_uri: https://github.com/watchlogdev/watchlog-ruby
|
|
46
|
+
post_install_message:
|
|
47
|
+
rdoc_options: []
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.1'
|
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
requirements: []
|
|
61
|
+
rubygems_version: 3.5.22
|
|
62
|
+
signing_key:
|
|
63
|
+
specification_version: 4
|
|
64
|
+
summary: Official Ruby SDK for the Watchlog watch price tracking API.
|
|
65
|
+
test_files: []
|