trmnl-api 0.0.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ba1eee66530c227b2e43b24de28e6913753d4f627af3d979c3add5cbba3846c
4
- data.tar.gz: da5463562b79f6b883f90925cb61ffc44b357c3e64eec615f5cbb0a433f3a36a
3
+ metadata.gz: f8a754ac699d2142cb950f2bc7739616181a33f085c99b244856f9115f823b6e
4
+ data.tar.gz: 8a221272e414bed3c1c25a8517c91bbce9286655c51057d9487077abdad22d6c
5
5
  SHA512:
6
- metadata.gz: 03430a130bfa5a302b67c674ece3951b88e0629f256c7937351d562a7f52117b7b0a0704817fbda6a4d771ee0ddd59542f80e9cccd5970c6ab5a8dfe1e9be0ba
7
- data.tar.gz: 71d6d93f75797dad4ef0ae37e6b454d65adcff265149de1f6e32175ae7d5970b4aa590283eed5d95ae93ccfad89672c1a835ce4529af8e1ea5d53a4844a5f532
6
+ metadata.gz: fadb0d344109f6a8ae6b84f411ee7f7363c708daa823a68c5adcd875871dc41928526505241587fa3218a48ece69acc2142d787245c2ce41a28e0cb30e3d28b5
7
+ data.tar.gz: 3ae4afaab60d62d8a1d8e6d776175031915cea50f237af2a025686c769a02651bc9137e2e3ffd4aa95bc24c925bc2f18b6a04161e33c1b8a683c1b7adb2467c4
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -48,14 +48,28 @@ require "trmnl/api"
48
48
  This client provides access to multiple endpoints. Each endpoint will answer either a `Success` or `Failure` (as provided by {dry_monads_link}) based on result of the API call. This allows you pattern match in your own code when using each endpoint. Example:
49
49
 
50
50
  ``` ruby
51
- case endpoint.call token: "secret"
51
+ client = TRMNL::API::Client.new
52
+
53
+ case client.display token: "secret"
52
54
  in Success(payload) then puts payload
53
55
  in Failure(response) then puts response
54
- else puts "Unknown HTTP response."
56
+ else puts "Unknown response."
55
57
  end
56
58
  ```
57
59
 
58
- See each endpoint for further details.
60
+ See xref:_endpoints[Endpoints] for further details.
61
+
62
+ === Configuration
63
+
64
+ By default, you shouldn't need to change the default configuration but you can always use a block to adjust settings as desired. If you don't configure the client, then the following defaults will be used:
65
+
66
+ [source,ruby]
67
+ ----
68
+ client = TRMNL::API::Client.new do |settings|
69
+ settings.content_type = "application/json",
70
+ settings.uri = "https://trmnl.app/api"
71
+ end
72
+ ----
59
73
 
60
74
  === Environment
61
75
 
@@ -64,16 +78,18 @@ You can configure the client via the following environment variables:
64
78
  * `TRMNL_API_CONTENT_TYPE`: Defines the HTTP `Content-Type` header. You shouldn't need to change this but is here if you need it. Default: "application/json".
65
79
  * `TRMNL_API_URI`: Defines the API URI. Default: "https://trmnl.app/api".
66
80
 
67
- Any/all environment changes will be applied to all endpoint objects.
81
+ Any/all environment changes will be applied unless you override these settings via the client configuration block shown above.
82
+
83
+ === Endpoints
68
84
 
69
- === Current Screen
85
+ ==== Current Screen
70
86
 
71
87
  Allows you to obtain current screen being displayed for your device. You must supply your device's API token as the `token`. Example:
72
88
 
73
89
  [source,ruby]
74
90
  ----
75
- endpoint = TRMNL::API::Endpoints::CurrentScreen.new
76
- endpoint.call token: "secret"
91
+ client = TRMNL::API::Client.new
92
+ client.current_screen token: "secret"
77
93
 
78
94
  # Success(
79
95
  # #<data TRMNL::API::Models::CurrentScreen
@@ -84,14 +100,14 @@ endpoint.call token: "secret"
84
100
  # )
85
101
  ----
86
102
 
87
- === Display
103
+ ==== Display
88
104
 
89
- Allows you to obtain current screen being displayed for your device. You must supply your device's API token as the `token`. Example:
105
+ Allows you to obtain current screen being displayed for your device with additional information not provided by the xref:_current_screen[Current Screen] endpoint. You must supply your device's API token as the `token`. Example:
90
106
 
91
107
  [source,ruby]
92
108
  ----
93
- endpoint = TRMNL::API::Endpoints::Display.new
94
- endpoint.call token: "secret"
109
+ client = TRMNL::API::Client.new
110
+ client.display token: "secret"
95
111
 
96
112
  # Success(
97
113
  # #<struct TRMNL::API::Models::Display
@@ -107,73 +123,72 @@ endpoint.call token: "secret"
107
123
  # )
108
124
  ----
109
125
 
110
- === Firmware
126
+ ==== Firmware
111
127
 
112
128
  Allows you to obtain the current stable firmware version. Example:
113
129
 
114
130
  [source,ruby]
115
131
  ----
116
- endpoint = TRMNL::API::Endpoints::Firmware.new
117
- endpoint.call
132
+ client = TRMNL::API::Client.new
133
+ client.call
118
134
 
119
135
  # Success(#<data TRMNL::API::Models::Firmware url="https://trmnl-fw.s3.us-east-2.amazonaws.com/FW1.4.8.bin", version="1.4.8">)
120
136
  ----
121
137
 
122
- === Log
138
+ ==== Log
123
139
 
124
140
  Allows you to create a log entry (which is what the device reports when it captures an error). You must supply your device's API token as the `token`. Example:
125
141
 
126
142
  [source,ruby]
127
143
  ----
128
- endpoint = TRMNL::API::Endpoints::Log.new
129
- endpoint.call token: "secret",
130
- log: {
131
- logs_array: [
132
- {
133
- log_id: 1,
134
- creation_timestamp: 1742022124,
135
- log_message: "returned code is not OK: 404",
136
- log_codeline: 597,
137
- device_status_stamp: {
138
- wifi_status: "connected",
139
- wakeup_reason: "timer",
140
- current_fw_version: "1.4.7",
141
- free_heap_size: 160656,
142
- special_function: "none",
143
- refresh_rate: 30,
144
- battery_voltage: 4.772,
145
- time_since_last_sleep_start: 31,
146
- wifi_rssi_level: -54
147
- },
148
- additional_info: {
149
- retry_attempt: 1
150
- },
151
- log_sourcefile: "src/bl.cpp"
152
- }
153
- ]
154
- }
144
+ client = TRMNL::API::Client.new
145
+ client.call token: "secret",
146
+ log: {
147
+ logs_array: [
148
+ {
149
+ log_id: 1,
150
+ creation_timestamp: 1742022124,
151
+ log_message: "returned code is not OK: 404",
152
+ log_codeline: 597,
153
+ device_status_stamp: {
154
+ wifi_status: "connected",
155
+ wakeup_reason: "timer",
156
+ current_fw_version: "1.4.7",
157
+ free_heap_size: 160656,
158
+ special_function: "none",
159
+ refresh_rate: 30,
160
+ battery_voltage: 4.772,
161
+ time_since_last_sleep_start: 31,
162
+ wifi_rssi_level: -54
163
+ },
164
+ additional_info: {
165
+ retry_attempt: 1
166
+ },
167
+ log_sourcefile: "src/bl.cpp"
168
+ }
169
+ ]
170
+ }
155
171
 
156
172
  # Success(#<HTTP::Response/1.1 204 No Content...)
157
173
  ----
158
174
 
159
175
  You'll either get a 204 No Content or 200 OK response depending on if the device exists or not.
160
176
 
161
- === Setup
177
+ ==== Setup
162
178
 
163
179
  Allows you to obtain the setup response for when a new device is setup. You must supply your device's MAC Address as the `id`. Example:
164
180
 
165
-
166
181
  [source,ruby]
167
182
  ----
168
- endpoint = TRMNL::API::Endpoints::Setup.new
169
- endpoint.call id: "B0:81:84:22:BD:E8"
183
+ client = TRMNL::API::Client.new
184
+ client.call id: "A1:B2:C3:D4:E5:F6"
170
185
 
171
186
  # Success(
172
187
  # #<data TRMNL::API::Models::Setup
173
188
  # api_key="secret",
174
- # friendly_id="40DEF8",
189
+ # friendly_id="F51FDE",
175
190
  # image_url="https://usetrmnl.com/images/setup/setup-logo.bmp",
176
- # message="Register at usetrmnl.com/signup with Device ID '40DEF8'"
191
+ # message="Register at usetrmnl.com/signup with Device ID 'F51FDE'"
177
192
  # >
178
193
  # )
179
194
  ----
@@ -1,39 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "dry/monads"
4
- require "http"
5
-
6
3
  module TRMNL
7
4
  module API
8
- # Provides a low level configurable and monadic API client.
9
- # :reek:DataClump
5
+ # Provides the primary client for making API requests.
10
6
  class Client
11
- include Dry::Monads[:result]
12
-
13
- HEADERS = {}.freeze
14
-
15
- def initialize settings: TRMNL::API::Configuration::Loader.new.call, http: HTTP
16
- @settings = settings
17
- @http = http
18
-
7
+ include Dependencies[:settings]
8
+
9
+ include Endpoints::Dependencies[
10
+ endpoint_current_screen: :current_screen,
11
+ endpoint_display: :display,
12
+ endpoint_firmware: :firmware,
13
+ endpoint_log: :log,
14
+ endpoint_setup: :setup
15
+ ]
16
+
17
+ def initialize(**)
18
+ super
19
19
  yield settings if block_given?
20
20
  end
21
21
 
22
- def get(path, headers: HEADERS, **params) = call(__method__, path, headers, params:)
22
+ def current_screen(**) = endpoint_current_screen.call(**)
23
23
 
24
- def post(path, headers: HEADERS, **json) = call(__method__, path, headers, json:)
24
+ def display(**) = endpoint_display.call(**)
25
25
 
26
- private
26
+ def firmware = endpoint_firmware.call
27
27
 
28
- attr_reader :settings, :http
28
+ def log(**) = endpoint_log.call(**)
29
29
 
30
- # rubocop:todo Metrics/ParameterLists
31
- def call method, path, headers, **options
32
- http.headers(settings.headers.merge(headers))
33
- .public_send(method, "#{settings.uri}/#{path}", options)
34
- .then { |response| response.status.success? ? Success(response) : Failure(response) }
35
- end
36
- # rubocop:enable Metrics/ParameterLists
30
+ def setup(**) = endpoint_setup.call(**)
37
31
  end
38
32
  end
39
33
  end
@@ -10,11 +10,12 @@ module TRMNL
10
10
  module Container
11
11
  extend Containable
12
12
 
13
- register(:client) { API::Client.new }
13
+ register(:settings) { TRMNL::API::Configuration::Loader.new.call }
14
+ register(:requester) { API::Requester.new }
14
15
  register(:logger) { Cogger.new id: "trmnl-api", formatter: :json }
15
16
 
16
17
  register :http do
17
- HTTP.default_options = ::HTTP::Options.new features: {logging: {logger: self[:logger]}}
18
+ HTTP.default_options = HTTP::Options.new features: {logging: {logger: self[:logger]}}
18
19
  HTTP
19
20
  end
20
21
 
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "containable"
4
+
5
+ module TRMNL
6
+ module API
7
+ module Endpoints
8
+ # Registers all endpoints.
9
+ module Container
10
+ extend Containable
11
+
12
+ register(:current_screen) { CurrentScreen.new }
13
+ register(:display) { Display.new }
14
+ register(:firmware) { Firmware.new }
15
+ register(:log) { Log.new }
16
+ register(:setup) { Setup.new }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -7,8 +7,8 @@ module TRMNL
7
7
  module Endpoints
8
8
  # Handles API request/response.
9
9
  class CurrentScreen
10
- include Dependencies[
11
- :client,
10
+ include TRMNL::API::Dependencies[
11
+ :requester,
12
12
  contract: "contracts.current_screen",
13
13
  model: "models.current_screen"
14
14
  ]
@@ -16,7 +16,7 @@ module TRMNL
16
16
  include Pipeable
17
17
 
18
18
  def call token:
19
- pipe client.get("current_screen", headers: {"Access-Token" => token}),
19
+ pipe requester.get("current_screen", headers: {"Access-Token" => token}),
20
20
  try(:parse, catch: JSON::ParserError),
21
21
  validate(contract, as: :to_h),
22
22
  to(model, :for)
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infusible"
4
+
5
+ module TRMNL
6
+ module API
7
+ module Endpoints
8
+ Dependencies = Infusible[Container]
9
+ end
10
+ end
11
+ end
@@ -7,11 +7,16 @@ module TRMNL
7
7
  module Endpoints
8
8
  # Handles API request/response.
9
9
  class Display
10
- include Dependencies[:client, contract: "contracts.display", model: "models.display"]
10
+ include TRMNL::API::Dependencies[
11
+ :requester,
12
+ contract: "contracts.display",
13
+ model: "models.display"
14
+ ]
15
+
11
16
  include Pipeable
12
17
 
13
18
  def call token:
14
- pipe client.get("display", headers: {"Access-Token" => token}),
19
+ pipe requester.get("display", headers: {"Access-Token" => token}),
15
20
  try(:parse, catch: JSON::ParserError),
16
21
  validate(contract, as: :to_h),
17
22
  to(model, :for)
@@ -7,11 +7,16 @@ module TRMNL
7
7
  module Endpoints
8
8
  # Handles API request/response.
9
9
  class Firmware
10
- include Dependencies[:client, contract: "contracts.firmware", model: "models.firmware"]
10
+ include TRMNL::API::Dependencies[
11
+ :requester,
12
+ contract: "contracts.firmware",
13
+ model: "models.firmware"
14
+ ]
15
+
11
16
  include Pipeable
12
17
 
13
18
  def call
14
- pipe client.get("firmware/latest"),
19
+ pipe requester.get("firmware/latest"),
15
20
  try(:parse, catch: JSON::ParserError),
16
21
  validate(contract, as: :to_h),
17
22
  to(model, :for)
@@ -5,9 +5,9 @@ module TRMNL
5
5
  module Endpoints
6
6
  # Handles API request/response.
7
7
  class Log
8
- include Dependencies[:client]
8
+ include TRMNL::API::Dependencies[:requester]
9
9
 
10
- def call(token:, **) = client.post("log", headers: {"Access-Token" => token}, **)
10
+ def call(token:, **) = requester.post("log", headers: {"Access-Token" => token}, **)
11
11
  end
12
12
  end
13
13
  end
@@ -7,11 +7,16 @@ module TRMNL
7
7
  module Endpoints
8
8
  # Handles API request/response.
9
9
  class Setup
10
- include Dependencies[:client, contract: "contracts.setup", model: "models.setup"]
10
+ include TRMNL::API::Dependencies[
11
+ :requester,
12
+ contract: "contracts.setup",
13
+ model: "models.setup"
14
+ ]
15
+
11
16
  include Pipeable
12
17
 
13
18
  def call id:
14
- pipe client.get("setup", headers: {"ID" => id}),
19
+ pipe requester.get("setup", headers: {"ID" => id}),
15
20
  try(:parse, catch: JSON::ParserError),
16
21
  validate(contract, as: :to_h),
17
22
  to(model, :for)
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/monads"
4
+ require "http"
5
+
6
+ module TRMNL
7
+ module API
8
+ # Provides a low level configurable and monadic API client.
9
+ # :reek:DataClump
10
+ class Requester
11
+ include Dependencies[:settings, :http]
12
+ include Dry::Monads[:result]
13
+
14
+ HEADERS = {}.freeze
15
+
16
+ def initialize(**)
17
+ super
18
+ yield settings if block_given?
19
+ end
20
+
21
+ def get(path, headers: HEADERS, **params) = call(__method__, path, headers, params:)
22
+
23
+ def post(path, headers: HEADERS, **json) = call(__method__, path, headers, json:)
24
+
25
+ private
26
+
27
+ attr_reader :settings, :http
28
+
29
+ # rubocop:todo Metrics/ParameterLists
30
+ def call method, path, headers, **options
31
+ http.headers(settings.headers.merge(headers))
32
+ .public_send(method, "#{settings.uri}/#{path}", options)
33
+ .then { |response| response.status.success? ? Success(response) : Failure(response) }
34
+ end
35
+ # rubocop:enable Metrics/ParameterLists
36
+ end
37
+ end
38
+ end
data/lib/trmnl/api.rb CHANGED
@@ -18,5 +18,7 @@ module TRMNL
18
18
  def self.loader registry = Zeitwerk::Registry
19
19
  @loader ||= registry.loaders.find { |loader| loader.tag == "trmnl-api" }
20
20
  end
21
+
22
+ def self.new(&) = Client.new(&)
21
23
  end
22
24
  end
data/trmnl-api.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "trmnl-api"
5
- spec.version = "0.0.0"
5
+ spec.version = "0.1.0"
6
6
  spec.authors = ["TRMNL"]
7
7
  spec.email = ["support@usetrmnl.com"]
8
8
  spec.homepage = "https://github.com/usetrmnl/trmnl-api"
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trmnl-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TRMNL
@@ -167,7 +167,9 @@ files:
167
167
  - lib/trmnl/api/contracts/firmware.rb
168
168
  - lib/trmnl/api/contracts/setup.rb
169
169
  - lib/trmnl/api/dependencies.rb
170
+ - lib/trmnl/api/endpoints/container.rb
170
171
  - lib/trmnl/api/endpoints/current_screen.rb
172
+ - lib/trmnl/api/endpoints/dependencies.rb
171
173
  - lib/trmnl/api/endpoints/display.rb
172
174
  - lib/trmnl/api/endpoints/firmware.rb
173
175
  - lib/trmnl/api/endpoints/log.rb
@@ -176,6 +178,7 @@ files:
176
178
  - lib/trmnl/api/models/display.rb
177
179
  - lib/trmnl/api/models/firmware.rb
178
180
  - lib/trmnl/api/models/setup.rb
181
+ - lib/trmnl/api/requester.rb
179
182
  - trmnl-api.gemspec
180
183
  homepage: https://github.com/usetrmnl/trmnl-api
181
184
  licenses:
metadata.gz.sig CHANGED
Binary file