d4h_api 2.0.0 → 2.0.1

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: 90ba7bf33fc815fbea860ba872f991052c3bf4057383275cffeca1212c1c53af
4
- data.tar.gz: 4f37a538b43e1162fe706c229004a469b744a9521c5ea54df0c543baa802b28f
3
+ metadata.gz: 2aa8bae136f351de1c996f430d2f20aa781ca4a44a99475efe661e2828bfd9ed
4
+ data.tar.gz: bed47fee1b00ca08c2a404efad96458093984f4f5887fc644409af66ae8affc5
5
5
  SHA512:
6
- metadata.gz: e5a6ff0f16ca0cddc34e344a0bc9b31d347002885de3d7c8133cc78248b24bbd84d922ad6a68907b481a7e15a441a9f30ec06823797fd2a7e84a6bec1205aced
7
- data.tar.gz: ada411f80bc829d3e582a0200b55569dc634daf580b58802e70d55c048b33559eec12ee2c89b1ac6f4f2f7fe78fb78b9a11fefe4b7e3ab2042b90283c9e52d03
6
+ metadata.gz: 9810b27605cd21faeebbc3b15eb36d4a45c1f3d6f06948f1e3db69b13d39b70b202603a19c9245a42f5d5a106ff1080bc323eb40f6fddaa598b16146a02becc9
7
+ data.tar.gz: 722d42a916aaf25acf3ad13206e94a7400c76f37a00ec61fbbcc73c9e58459ad22e94743f08ef0a4ceaddcd985baee928833a26cf7cf0a58c58ece15eb68a888
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -32,15 +32,17 @@ gem install d4h_api
32
32
  ```ruby
33
33
  require "d4h"
34
34
 
35
+ # Discover your identity (no context_id needed)
36
+ client = D4H::API::Client.new(api_key: ENV.fetch("D4H_TOKEN"))
37
+ me = client.whoami.show
38
+ puts "#{me.name} (#{me.email})"
39
+
40
+ # Use context_id for all other resources
35
41
  client = D4H::API::Client.new(
36
42
  api_key: ENV.fetch("D4H_TOKEN"),
37
43
  context_id: ENV.fetch("D4H_TEAM_ID").to_i,
38
44
  )
39
45
 
40
- # Who am I?
41
- me = client.whoami.show
42
- puts "#{me.name} (#{me.email})"
43
-
44
46
  # List all operational members
45
47
  members = client.member.list(status: "OPERATIONAL")
46
48
  members.each { |m| puts m.name }
@@ -54,9 +56,13 @@ puts "#{team.title} — #{team.country}"
54
56
 
55
57
  ### Client Initialization
56
58
 
57
- The client requires an `api_key` and a `context_id` (your D4H team ID). The `context` defaults to `"team"` but can be set to `"organisation"` for organisation-scoped API calls.
59
+ The client requires an `api_key`. The `context_id` (your D4H team or organisation ID) is optional — it is required for all resources except `whoami`, which can be used to discover your context. The `context` defaults to `"team"` but can be set to `"organisation"` for organisation-scoped API calls.
58
60
 
59
61
  ```ruby
62
+ # Minimal client — only whoami is available (no context_id)
63
+ client = D4H::API::Client.new(api_key: ENV.fetch("D4H_TOKEN"))
64
+ me = client.whoami.show
65
+
60
66
  # Team context (default)
61
67
  client = D4H::API::Client.new(
62
68
  api_key: ENV.fetch("D4H_TOKEN"),
@@ -85,7 +91,7 @@ The client reads the following environment variables as defaults. All can be ove
85
91
  | Variable | Default | Constructor param | Description |
86
92
  |----------------|---------------------------------------|-------------------|---------------------------------------------------------------------------|
87
93
  | `D4H_TOKEN` | *(required)* | `api_key:` | Your D4H API Bearer token. Generate one in your [D4H account settings](https://support.d4h.com/en/articles/2334703-api-access). |
88
- | `D4H_TEAM_ID` | *(required)* | `context_id:` | Your D4H team (or organisation) numeric ID. Find it in your D4H URL or via the API. |
94
+ | `D4H_TEAM_ID` | *(optional)* | `context_id:` | Your D4H team (or organisation) numeric ID. Required for all resources except `whoami`. Find it in your D4H URL or via the `whoami` endpoint. |
89
95
  | `D4H_BASE_URL` | `https://api.team-manager.us.d4h.com` | `base_url:` | Base URL for the D4H API. Change for EU (`https://api.team-manager.eu.d4h.com`) or other regional endpoints. |
90
96
 
91
97
  A typical `.env` file:
@@ -137,7 +143,7 @@ client.equipment # => EquipmentResource.new(client)
137
143
  client.event # => EventResource.new(client)
138
144
  ```
139
145
 
140
- The client builds a **base path** from the context — `v3/team/42` for team context, `v3/organisation/99` for organisation context — which all resources prepend to their endpoint URLs.
146
+ The client builds a **base path** from the context — `v3/team/42` for team context, `v3/organisation/99` for organisation context — which most resources prepend to their endpoint URLs. The `context_id` is optional; when omitted, only context-free resources like `whoami` (which uses `v3/whoami`) are available. Calling a context-scoped resource without a `context_id` raises `ArgumentError`.
141
147
 
142
148
  **`D4H::API::Resource`** is the base class for all 56 resource endpoints. It provides five HTTP verb methods (`get_request`, `post_request`, `put_request`, `patch_request`, `delete_request`), each of which injects the Bearer token header and checks the response status. Every subclass defines a `SUB_URL` constant and implements only the CRUD methods the D4H API supports for that resource:
143
149
 
@@ -211,7 +217,7 @@ client.event.update(id: 1, title: "Updated Training")
211
217
  client.tag.destroy(id: 5)
212
218
  ```
213
219
 
214
- Two special cases: `whoami.show` takes no arguments (it returns the authenticated user), and `document.update` uses HTTP PUT instead of PATCH per the D4H API contract.
220
+ Two special cases: `whoami.show` takes no arguments and does not require a `context_id` (it hits `v3/whoami` directly to return the authenticated user), and `document.update` uses HTTP PUT instead of PATCH per the D4H API contract.
215
221
 
216
222
  ## Usage
217
223
 
@@ -351,18 +357,19 @@ If all retries are exhausted, the `RetriableError` propagates to your code so yo
351
357
  ### Team & Identity
352
358
 
353
359
  ```ruby
354
- # Show your team's info (requires the team's own ID)
360
+ # Show your own profile (no context_id needed)
361
+ client = D4H::API::Client.new(api_key: ENV.fetch("D4H_TOKEN"))
362
+ me = client.whoami.show
363
+ me.name # => "John Doe"
364
+ me.email # => "john@example.com"
365
+
366
+ # Show your team's info (requires context_id)
355
367
  team = client.team.show(id: 42)
356
368
  team.title # => "Rocky Mountain Rescue"
357
369
  team.timezone # => "America/Denver"
358
370
  team.memberCounts.total # => 90
359
371
  team.memberCounts.operational # => 85
360
372
 
361
- # Show your own profile
362
- me = client.whoami.show
363
- me.name # => "John Doe"
364
- me.email # => "john@example.com"
365
-
366
373
  # Show an organisation
367
374
  org = client.organisation.show(id: 5)
368
375
  org.title # => "Colorado SAR"
@@ -738,7 +745,7 @@ Every resource is accessible as a method on the client. The table below shows wh
738
745
  | `whoami` | whoami | | x** | | | |
739
746
 
740
747
  \* Document update uses PUT instead of PATCH.
741
- \*\* Whoami `show` takes no arguments — it returns the current authenticated user.
748
+ \*\* Whoami `show` takes no arguments and does not require `context_id` — it hits `v3/whoami` directly to return the current authenticated user.
742
749
 
743
750
  All resources with `list` also support `list_all` for automatic pagination.
744
751
 
data/d4h_api.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "d4h_api"
5
- spec.version = "2.0.0"
5
+ spec.version = "2.0.1"
6
6
  spec.authors = ["Pawel Osiczko"]
7
7
  spec.email = ["p.osiczko@tetrapyloctomy.org"]
8
8
  spec.homepage = "https://github.com/rockymountainrescue/d4h_api"
@@ -17,6 +17,10 @@ module D4H
17
17
  #
18
18
  # Examples
19
19
  #
20
+ # # Discover your identity (no context needed)
21
+ # client = D4H::API::Client.new(api_key: ENV.fetch("D4H_TOKEN"))
22
+ # me = client.whoami.show
23
+ #
20
24
  # # Team context (default)
21
25
  # client = D4H::API::Client.new(
22
26
  # api_key: ENV.fetch("D4H_TOKEN"),
@@ -44,7 +48,8 @@ module D4H
44
48
  #
45
49
  # api_key: - A String Bearer token for API authentication.
46
50
  # context: - The context scope, either "team" (default) or "organisation".
47
- # context_id: - The Integer ID of the team or organisation.
51
+ # context_id: - The Integer ID of the team or organisation (optional;
52
+ # required for all resources except whoami).
48
53
  # base_url: - The base URL for the D4H API. Defaults to D4H_BASE_URL env
49
54
  # var, or "https://api.team-manager.us.d4h.com" if unset.
50
55
  # Change this for EU or other regional endpoints.
@@ -54,7 +59,7 @@ module D4H
54
59
  # Set to 0 to disable retries.
55
60
  # retry_interval: - Float base interval in seconds between retries (default: 1).
56
61
  # Set to 0 in tests to avoid sleeping.
57
- def initialize(api_key:, context: "team", context_id:,
62
+ def initialize(api_key:, context: "team", context_id: nil,
58
63
  base_url: ENV.fetch("D4H_BASE_URL", DEFAULT_BASE_URL),
59
64
  adapter: Faraday.default_adapter,
60
65
  max_retries: MAX_RETRIES, retry_interval: RETRY_INTERVAL)
@@ -72,7 +77,11 @@ module D4H
72
77
  # Examples
73
78
  #
74
79
  # client.base_path # => "v3/team/42"
80
+ #
81
+ # Raises ArgumentError if context_id is nil.
75
82
  def base_path
83
+ raise ArgumentError, "context_id is required for this resource" if context_id.nil?
84
+
76
85
  "v3/#{context}/#{context_id}"
77
86
  end
78
87
 
@@ -128,7 +128,7 @@ module D4H
128
128
  return response if (200..299).cover?(response.status)
129
129
 
130
130
  body = response.body || {}
131
- message = [body["error"], body["message"]].compact.join(": ")
131
+ message = [body["title"], body["detail"], body["error"], body["message"]].compact.uniq.join(": ")
132
132
 
133
133
  raise RetriableError, message if Client::RETRIABLE_STATUSES.include?(response.status)
134
134
 
@@ -6,7 +6,7 @@ module D4H
6
6
  SUB_URL = "whoami"
7
7
 
8
8
  def show
9
- Whoami.new(get_request("#{base_path}/#{SUB_URL}").body)
9
+ Whoami.new(get_request("v3/#{SUB_URL}").body)
10
10
  end
11
11
  end
12
12
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: d4h_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pawel Osiczko
metadata.gz.sig CHANGED
Binary file