emissions-api-sdk 1.0.1 → 1.0.3
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.
- package/README.md +9 -0
- package/dist/Constants.js +2 -1
- package/dist/api/Calculation.js +4 -4
- package/dist/api/Factor.js +7 -7
- package/dist/api/FactorSets.js +1 -1
- package/dist/api/Fugitive.js +4 -4
- package/dist/api/Location.js +4 -4
- package/dist/api/Mobile.js +4 -6
- package/dist/api/Stationary.js +4 -4
- package/dist/api/TransportationAndDistribution.js +5 -5
- package/dist/api/Usage.js +26 -0
- package/dist/coverage/clover.xml +346 -0
- package/dist/coverage/coverage-final.json +20 -0
- package/dist/coverage/lcov-report/base.css +224 -0
- package/dist/coverage/lcov-report/block-navigation.js +87 -0
- package/dist/coverage/lcov-report/favicon.png +0 -0
- package/dist/coverage/lcov-report/index.html +161 -0
- package/dist/coverage/lcov-report/prettify.css +1 -0
- package/dist/coverage/lcov-report/prettify.js +2 -0
- package/dist/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/dist/coverage/lcov-report/sorter.js +210 -0
- package/dist/coverage/lcov-report/src/Client.ts.html +838 -0
- package/dist/coverage/lcov-report/src/Constants.ts.html +214 -0
- package/dist/coverage/lcov-report/src/api/Calculation.ts.html +436 -0
- package/dist/coverage/lcov-report/src/api/Factor.ts.html +622 -0
- package/dist/coverage/lcov-report/src/api/FactorSets.ts.html +157 -0
- package/dist/coverage/lcov-report/src/api/Fugitive.ts.html +436 -0
- package/dist/coverage/lcov-report/src/api/Location.ts.html +433 -0
- package/dist/coverage/lcov-report/src/api/Mobile.ts.html +448 -0
- package/dist/coverage/lcov-report/src/api/Stationary.ts.html +442 -0
- package/dist/coverage/lcov-report/src/api/TransportationAndDistribution.ts.html +436 -0
- package/dist/coverage/lcov-report/src/api/Usage.ts.html +160 -0
- package/dist/coverage/lcov-report/src/api/index.html +236 -0
- package/dist/coverage/lcov-report/src/index.html +161 -0
- package/dist/coverage/lcov-report/src/request.ts.html +235 -0
- package/dist/coverage/lcov-report/src/utils.ts.html +139 -0
- package/dist/coverage/lcov-report/test/index.html +116 -0
- package/dist/coverage/lcov-report/test/mocks/CommonRequest.ts.html +142 -0
- package/dist/coverage/lcov-report/test/mocks/FactorRequest.ts.html +133 -0
- package/dist/coverage/lcov-report/test/mocks/GenericCalculationRequest.ts.html +142 -0
- package/dist/coverage/lcov-report/test/mocks/LocationRequest.ts.html +139 -0
- package/dist/coverage/lcov-report/test/mocks/SearchRequest.ts.html +142 -0
- package/dist/coverage/lcov-report/test/mocks/index.html +176 -0
- package/dist/coverage/lcov-report/test/testUtils.ts.html +97 -0
- package/dist/coverage/lcov.info +578 -0
- package/dist/index.js +2 -1
- package/dist/interfaces/response/AreaResponse.js +2 -0
- package/dist/interfaces/response/EmissionResponse.js +2 -0
- package/dist/interfaces/response/EmissionResponseWithDetails.js +2 -0
- package/dist/interfaces/response/FactorResponse.js +2 -0
- package/dist/interfaces/response/FactorSetResponse.js +2 -0
- package/dist/interfaces/response/SearchResponse.js +2 -0
- package/dist/interfaces/response/TypeResponse.js +2 -0
- package/dist/interfaces/response/UnitResponse.js +2 -0
- package/dist/interfaces/response/UsageResponse.js +5 -0
- package/dist/types/Constants.d.ts +1 -0
- package/dist/types/api/Calculation.d.ts +13 -8
- package/dist/types/api/Factor.d.ts +18 -13
- package/dist/types/api/FactorSets.d.ts +3 -2
- package/dist/types/api/Fugitive.d.ts +13 -8
- package/dist/types/api/Location.d.ts +13 -8
- package/dist/types/api/Mobile.d.ts +13 -10
- package/dist/types/api/Stationary.d.ts +13 -8
- package/dist/types/api/TransportationAndDistribution.d.ts +14 -9
- package/dist/types/api/Usage.d.ts +13 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/interfaces/response/AreaResponse.d.ts +20 -0
- package/dist/types/interfaces/response/EmissionResponse.d.ts +31 -0
- package/dist/types/interfaces/response/EmissionResponseWithDetails.d.ts +144 -0
- package/dist/types/interfaces/response/FactorResponse.d.ts +53 -0
- package/dist/types/interfaces/response/FactorSetResponse.d.ts +38 -0
- package/dist/types/interfaces/response/SearchResponse.d.ts +41 -0
- package/dist/types/interfaces/response/TypeResponse.d.ts +7 -0
- package/dist/types/interfaces/response/UnitResponse.d.ts +16 -0
- package/dist/types/interfaces/response/UsageResponse.d.ts +17 -0
- package/docs/_sources/index.rst.txt +1 -0
- package/docs/_sources/reference.rst.txt +6 -0
- package/docs/_sources/troubleshooting.rst.txt +486 -0
- package/docs/authentication.html +1 -0
- package/docs/client.html +1 -0
- package/docs/genindex.html +5 -0
- package/docs/getting_started.html +1 -0
- package/docs/index.html +15 -0
- package/docs/objects.inv +0 -0
- package/docs/reference.html +71 -35
- package/docs/sdk.html +1 -0
- package/docs/search.html +1 -0
- package/docs/searchindex.js +1 -1
- package/docs/troubleshooting.html +777 -0
- package/package.json +1 -1
- package/sphinx-build/requirements.txt +5 -0
- package/sphinx-build/source/index.rst +1 -0
- package/sphinx-build/source/reference.rst +6 -0
- package/sphinx-build/source/troubleshooting.rst +486 -0
- package/src/Constants.ts +2 -0
- package/src/api/Calculation.ts +19 -14
- package/src/api/Factor.ts +27 -22
- package/src/api/FactorSets.ts +4 -3
- package/src/api/Fugitive.ts +19 -14
- package/src/api/Location.ts +20 -15
- package/src/api/Mobile.ts +19 -16
- package/src/api/Stationary.ts +19 -14
- package/src/api/TransportationAndDistribution.ts +21 -16
- package/src/api/Usage.ts +26 -0
- package/src/index.ts +35 -1
- package/src/interfaces/response/AreaResponse.ts +25 -0
- package/src/interfaces/response/EmissionResponse.ts +44 -0
- package/src/interfaces/response/EmissionResponseWithDetails.ts +183 -0
- package/src/interfaces/response/FactorResponse.ts +78 -0
- package/src/interfaces/response/FactorSetResponse.ts +52 -0
- package/src/interfaces/response/SearchResponse.ts +52 -0
- package/src/interfaces/response/TypeResponse.ts +7 -0
- package/src/interfaces/response/UnitResponse.ts +17 -0
- package/src/interfaces/response/UsageResponse.ts +23 -0
- package/test/apiTest.test.ts +35 -4
package/package.json
CHANGED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
Troubleshooting Guide
|
|
2
|
+
=====================
|
|
3
|
+
|
|
4
|
+
Overview
|
|
5
|
+
--------
|
|
6
|
+
|
|
7
|
+
This document lists all SDK-specific errors that can occur when using the Emissions API Node.js SDK. These errors are related to SDK initialization and client management.
|
|
8
|
+
|
|
9
|
+
----
|
|
10
|
+
|
|
11
|
+
SDK Initialization Errors
|
|
12
|
+
--------------------------
|
|
13
|
+
|
|
14
|
+
Configuration Errors
|
|
15
|
+
~~~~~~~~~~~~~~~~~~~~
|
|
16
|
+
|
|
17
|
+
These errors occur when ``Client.getClient()`` is called with invalid or missing configuration parameters.
|
|
18
|
+
|
|
19
|
+
1. Missing Auth URL with Custom Host
|
|
20
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
21
|
+
|
|
22
|
+
**Error Message:**
|
|
23
|
+
|
|
24
|
+
.. code-block:: text
|
|
25
|
+
|
|
26
|
+
Error: If custom "host" is provided, "authUrl" must also be provided.
|
|
27
|
+
|
|
28
|
+
**Cause:** Custom host URL provided without corresponding authentication URL
|
|
29
|
+
|
|
30
|
+
**Solution:** Provide both ``host`` and ``authUrl`` parameters together
|
|
31
|
+
|
|
32
|
+
**Sample Function Call That Causes This Error:**
|
|
33
|
+
|
|
34
|
+
.. code-block:: typescript
|
|
35
|
+
|
|
36
|
+
await Client.getClient({
|
|
37
|
+
host: 'https://api.ibm.com',
|
|
38
|
+
clientId: 'client123',
|
|
39
|
+
orgId: 'org456'
|
|
40
|
+
// Missing authUrl parameter
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
----
|
|
44
|
+
|
|
45
|
+
2. Missing Client ID with Token
|
|
46
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
47
|
+
|
|
48
|
+
**Error Message:**
|
|
49
|
+
|
|
50
|
+
.. code-block:: text
|
|
51
|
+
|
|
52
|
+
Error: If token is provided directly, "clientId" must also be provided.
|
|
53
|
+
|
|
54
|
+
**Cause:** Pre-existing token provided without client ID
|
|
55
|
+
|
|
56
|
+
**Solution:** Provide ``clientId`` parameter when using a token
|
|
57
|
+
|
|
58
|
+
**Sample Function Call That Causes This Error:**
|
|
59
|
+
|
|
60
|
+
.. code-block:: typescript
|
|
61
|
+
|
|
62
|
+
await Client.getClient({
|
|
63
|
+
token: 'your-jwt-token'
|
|
64
|
+
// Missing clientId parameter
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
----
|
|
68
|
+
|
|
69
|
+
3. Missing Parameters with API Key
|
|
70
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
71
|
+
|
|
72
|
+
**Error Message:**
|
|
73
|
+
|
|
74
|
+
.. code-block:: text
|
|
75
|
+
|
|
76
|
+
Error: If apiKey is provided , "clientId" and "OrgId" must also be provided.
|
|
77
|
+
|
|
78
|
+
**Cause:** API key authentication requires both client ID and organization ID
|
|
79
|
+
|
|
80
|
+
**Solution:** Provide ``apiKey``, ``clientId``, and ``orgId`` parameters together
|
|
81
|
+
|
|
82
|
+
**Sample Function Call That Causes This Error:**
|
|
83
|
+
|
|
84
|
+
.. code-block:: typescript
|
|
85
|
+
|
|
86
|
+
// Missing orgId
|
|
87
|
+
await Client.getClient({
|
|
88
|
+
apiKey: 'your-api-key',
|
|
89
|
+
clientId: 'client123'
|
|
90
|
+
// Missing orgId parameter
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Or missing clientId
|
|
94
|
+
await Client.getClient({
|
|
95
|
+
apiKey: 'your-api-key',
|
|
96
|
+
orgId: 'org456'
|
|
97
|
+
// Missing clientId parameter
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
----
|
|
101
|
+
|
|
102
|
+
Authentication Errors (HTTP 401)
|
|
103
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
104
|
+
|
|
105
|
+
These errors occur when authentication credentials are invalid or expired.
|
|
106
|
+
|
|
107
|
+
4. Invalid API Key
|
|
108
|
+
^^^^^^^^^^^^^^^^^^
|
|
109
|
+
|
|
110
|
+
**Error Response:**
|
|
111
|
+
|
|
112
|
+
.. code-block:: json
|
|
113
|
+
|
|
114
|
+
{
|
|
115
|
+
"httpCode": "401",
|
|
116
|
+
"httpMessage": "Unauthorized",
|
|
117
|
+
"moreInformation": "Cannot pass the security checks that are required by the target API or operation, Enable debug headers for more details."
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
**Cause:** API key is invalid, expired, or revoked
|
|
121
|
+
|
|
122
|
+
**Solution:** Verify API key from IBM Cloud console and ensure it has proper permissions
|
|
123
|
+
|
|
124
|
+
**Sample Function Call That Causes This Error:**
|
|
125
|
+
|
|
126
|
+
.. code-block:: typescript
|
|
127
|
+
|
|
128
|
+
await Client.getClient({
|
|
129
|
+
apiKey: 'invalid_api_key_12345',
|
|
130
|
+
clientId: 'client123',
|
|
131
|
+
orgId: 'org456',
|
|
132
|
+
host: 'https://api.ibm.com',
|
|
133
|
+
authUrl: 'https://api.ibm.com/auth-url'
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
----
|
|
137
|
+
|
|
138
|
+
5. Invalid Organization ID
|
|
139
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
140
|
+
|
|
141
|
+
**Error Response:**
|
|
142
|
+
|
|
143
|
+
.. code-block:: json
|
|
144
|
+
|
|
145
|
+
{
|
|
146
|
+
"httpCode": "401",
|
|
147
|
+
"httpMessage": "Unauthorized",
|
|
148
|
+
"moreInformation": "Cannot pass the security checks that are required by the target API or operation, Enable debug headers for more details."
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
**Cause:** Organization ID doesn't match account or user lacks access
|
|
152
|
+
|
|
153
|
+
**Solution:** Verify organization ID from IBM Cloud account settings
|
|
154
|
+
|
|
155
|
+
**Sample Function Call That Causes This Error:**
|
|
156
|
+
|
|
157
|
+
.. code-block:: typescript
|
|
158
|
+
|
|
159
|
+
await Client.getClient({
|
|
160
|
+
apiKey: 'your-valid-api-key',
|
|
161
|
+
clientId: 'client123',
|
|
162
|
+
orgId: 'invalid-org-id-12345',
|
|
163
|
+
host: 'https://api.ibm.com',
|
|
164
|
+
authUrl: 'https://api.ibm.com/auth-url'
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
----
|
|
168
|
+
|
|
169
|
+
6. Invalid Client ID
|
|
170
|
+
^^^^^^^^^^^^^^^^^^^^^
|
|
171
|
+
|
|
172
|
+
**Error Response:**
|
|
173
|
+
|
|
174
|
+
.. code-block:: json
|
|
175
|
+
|
|
176
|
+
{
|
|
177
|
+
"httpCode": "401",
|
|
178
|
+
"httpMessage": "Unauthorized",
|
|
179
|
+
"moreInformation": "Invalid client id or secret."
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
**Cause:** Client ID is invalid or doesn't match credentials
|
|
183
|
+
|
|
184
|
+
**Solution:** Verify client ID from IBM Cloud credentials
|
|
185
|
+
|
|
186
|
+
**Sample Function Call That Causes This Error:**
|
|
187
|
+
|
|
188
|
+
.. code-block:: typescript
|
|
189
|
+
|
|
190
|
+
await Client.getClient({
|
|
191
|
+
apiKey: 'your-valid-api-key',
|
|
192
|
+
clientId: 'invalid-client-id-12345',
|
|
193
|
+
orgId: 'org456',
|
|
194
|
+
host: 'https://api.ibm.com',
|
|
195
|
+
authUrl: 'https://api.ibm.com/auth-url'
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
----
|
|
199
|
+
|
|
200
|
+
7. Empty Token Response
|
|
201
|
+
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
202
|
+
|
|
203
|
+
**Error Message:**
|
|
204
|
+
|
|
205
|
+
.. code-block:: text
|
|
206
|
+
|
|
207
|
+
Error: Token response is empty
|
|
208
|
+
|
|
209
|
+
**Cause:** Authentication server returned an empty or null token in the response body
|
|
210
|
+
|
|
211
|
+
**Solution:**
|
|
212
|
+
|
|
213
|
+
- Verify the ``authUrl`` is correct and points to a valid authentication endpoint
|
|
214
|
+
- Ensure the authentication service is responding correctly
|
|
215
|
+
- Check that the API key, client ID, and org ID combination is valid
|
|
216
|
+
- Verify the authentication endpoint is returning a token in the response
|
|
217
|
+
- Contact support if the authentication service is not responding properly
|
|
218
|
+
|
|
219
|
+
**Sample Function Call That Causes This Error:**
|
|
220
|
+
|
|
221
|
+
.. code-block:: typescript
|
|
222
|
+
|
|
223
|
+
await Client.getClient({
|
|
224
|
+
apiKey: 'your-api-key',
|
|
225
|
+
clientId: 'client123',
|
|
226
|
+
orgId: 'org456',
|
|
227
|
+
host: 'https://api.ibm.com/',
|
|
228
|
+
authUrl: 'https://valid-url-but-returns-empty-token.example.com/token'
|
|
229
|
+
// Valid URL but returns empty token in response
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
**Note:** If the authUrl is completely invalid or unreachable (e.g., wrong domain), you'll get a network error (ENOTFOUND) instead of this error. This error specifically occurs when the authentication endpoint responds but returns an empty token.
|
|
233
|
+
|
|
234
|
+
----
|
|
235
|
+
|
|
236
|
+
8. Missing Expiry Field in Token
|
|
237
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
238
|
+
|
|
239
|
+
**Error Message:**
|
|
240
|
+
|
|
241
|
+
.. code-block:: text
|
|
242
|
+
|
|
243
|
+
Error: exp field is missing in the token
|
|
244
|
+
|
|
245
|
+
**Cause:** The provided JWT token does not contain the required ``exp`` (expiry) field
|
|
246
|
+
|
|
247
|
+
**Solution:**
|
|
248
|
+
|
|
249
|
+
- Ensure the token is a valid JWT with an expiry field
|
|
250
|
+
- Regenerate the token if it's malformed
|
|
251
|
+
- Verify the token was generated correctly by the authentication service
|
|
252
|
+
|
|
253
|
+
**Sample Function Call That Causes This Error:**
|
|
254
|
+
|
|
255
|
+
.. code-block:: typescript
|
|
256
|
+
|
|
257
|
+
await Client.getClient({
|
|
258
|
+
token: 'malformed-jwt-token-without-exp-field',
|
|
259
|
+
clientId: 'client123'
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
**Note:** This error occurs when using token-based authentication with a JWT that doesn't include the standard ``exp`` (expiration time) claim.
|
|
263
|
+
|
|
264
|
+
----
|
|
265
|
+
|
|
266
|
+
Client Instance Errors
|
|
267
|
+
-----------------------
|
|
268
|
+
|
|
269
|
+
Client Not Initialized
|
|
270
|
+
~~~~~~~~~~~~~~~~~~~~~~
|
|
271
|
+
|
|
272
|
+
**Error Message:**
|
|
273
|
+
|
|
274
|
+
.. code-block:: text
|
|
275
|
+
|
|
276
|
+
Error: Client is not initialized. Call Client.getClient() first.
|
|
277
|
+
|
|
278
|
+
**Cause:** Attempting to use ``Client.getInstance()`` or API methods before calling ``Client.getClient()``
|
|
279
|
+
|
|
280
|
+
**Solution:** Always call ``Client.getClient()`` before using any SDK methods
|
|
281
|
+
|
|
282
|
+
**When This Occurs:**
|
|
283
|
+
|
|
284
|
+
- Calling ``Client.getInstance()`` before initialization
|
|
285
|
+
- Calling any API method (Stationary.calculate(), Mobile.calculate(), etc.) before initialization
|
|
286
|
+
- Application restart without re-initializing the client
|
|
287
|
+
|
|
288
|
+
**Sample Function Calls That Cause This Error:**
|
|
289
|
+
|
|
290
|
+
.. code-block:: typescript
|
|
291
|
+
|
|
292
|
+
// Error 1: Calling getInstance() before initialization
|
|
293
|
+
const client = Client.getInstance(); // Throws error
|
|
294
|
+
|
|
295
|
+
// Error 2: Calling API methods before initialization
|
|
296
|
+
await Stationary.calculate({
|
|
297
|
+
time: { date: "2025-01-04" },
|
|
298
|
+
location: { country: "usa" },
|
|
299
|
+
activity: { type: "Coal - Lignite", unit: "KJ", value: 3 }
|
|
300
|
+
}); // Throws error
|
|
301
|
+
|
|
302
|
+
// Error 3: Calling any other API method
|
|
303
|
+
await Mobile.calculate({...}); // Throws error
|
|
304
|
+
await Fugitive.calculate({...}); // Throws error
|
|
305
|
+
await Location.getUnits("steam"); // Throws error
|
|
306
|
+
|
|
307
|
+
----
|
|
308
|
+
|
|
309
|
+
Token Management
|
|
310
|
+
----------------
|
|
311
|
+
|
|
312
|
+
Automatic Token Refresh
|
|
313
|
+
~~~~~~~~~~~~~~~~~~~~~~~
|
|
314
|
+
|
|
315
|
+
The SDK automatically refreshes authentication tokens when they are about to expire (within 60 seconds). Token refresh failures will cause subsequent API calls to fail with 401 errors.
|
|
316
|
+
|
|
317
|
+
**Note:** Token refresh only occurs for tokens generated by the SDK, not for user-provided tokens.
|
|
318
|
+
|
|
319
|
+
----
|
|
320
|
+
|
|
321
|
+
Error Summary Table
|
|
322
|
+
-------------------
|
|
323
|
+
|
|
324
|
+
.. list-table::
|
|
325
|
+
:header-rows: 1
|
|
326
|
+
:widths: 20 40 10 30
|
|
327
|
+
|
|
328
|
+
* - Error Type
|
|
329
|
+
- Error Message
|
|
330
|
+
- HTTP Code
|
|
331
|
+
- Solution
|
|
332
|
+
* - Configuration
|
|
333
|
+
- ``If custom "host" is provided, "authUrl" must also be provided.``
|
|
334
|
+
- N/A
|
|
335
|
+
- Provide both host and authUrl
|
|
336
|
+
* - Configuration
|
|
337
|
+
- ``If token is provided directly, "clientId" must also be provided.``
|
|
338
|
+
- N/A
|
|
339
|
+
- Provide clientId with token
|
|
340
|
+
* - Configuration
|
|
341
|
+
- ``If apiKey is provided , "clientId" and "OrgId" must also be provided.``
|
|
342
|
+
- N/A
|
|
343
|
+
- Provide all three parameters
|
|
344
|
+
* - Authentication
|
|
345
|
+
- ``Cannot pass the security checks...``
|
|
346
|
+
- 401
|
|
347
|
+
- Verify API key and org ID
|
|
348
|
+
* - Authentication
|
|
349
|
+
- ``Invalid client id or secret``
|
|
350
|
+
- 401
|
|
351
|
+
- Verify client ID
|
|
352
|
+
* - Authentication
|
|
353
|
+
- ``Token response is empty``
|
|
354
|
+
- N/A
|
|
355
|
+
- Check network and authUrl
|
|
356
|
+
* - Token
|
|
357
|
+
- ``exp field is missing in the token``
|
|
358
|
+
- N/A
|
|
359
|
+
- Use valid JWT with exp field
|
|
360
|
+
* - Instance
|
|
361
|
+
- ``Client is not initialized. Call Client.getClient() first.``
|
|
362
|
+
- N/A
|
|
363
|
+
- Call getClient() first
|
|
364
|
+
|
|
365
|
+
----
|
|
366
|
+
|
|
367
|
+
Required Configuration Parameters
|
|
368
|
+
----------------------------------
|
|
369
|
+
|
|
370
|
+
When calling ``Client.getClient()``, the following parameters are required based on authentication method:
|
|
371
|
+
|
|
372
|
+
API Key Authentication (Recommended)
|
|
373
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
374
|
+
|
|
375
|
+
- ``apiKey`` - Your IBM Cloud API key
|
|
376
|
+
- ``clientId`` - Your client ID
|
|
377
|
+
- ``orgId`` - Your organization ID
|
|
378
|
+
- ``host`` - API host URL
|
|
379
|
+
- ``authUrl`` - Authentication URL
|
|
380
|
+
|
|
381
|
+
Token Authentication
|
|
382
|
+
~~~~~~~~~~~~~~~~~~~~
|
|
383
|
+
|
|
384
|
+
- ``token`` - Pre-existing JWT token
|
|
385
|
+
- ``clientId`` - Your client ID
|
|
386
|
+
|
|
387
|
+
----
|
|
388
|
+
|
|
389
|
+
Initialization Checklist
|
|
390
|
+
-------------------------
|
|
391
|
+
|
|
392
|
+
Before using the SDK, ensure:
|
|
393
|
+
|
|
394
|
+
- [ ] All required configuration parameters are provided
|
|
395
|
+
- [ ] Credentials are valid and not expired
|
|
396
|
+
- [ ] Correct environment URLs are used
|
|
397
|
+
- [ ] ``Client.getClient()`` is called before any API methods
|
|
398
|
+
- [ ] Error handling is implemented for initialization failures
|
|
399
|
+
- [ ] Credentials are stored securely (environment variables recommended)
|
|
400
|
+
|
|
401
|
+
----
|
|
402
|
+
|
|
403
|
+
Common Scenarios
|
|
404
|
+
----------------
|
|
405
|
+
|
|
406
|
+
Scenario 1: Application Startup
|
|
407
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
408
|
+
|
|
409
|
+
**Issue:** SDK must be initialized when application starts
|
|
410
|
+
|
|
411
|
+
**Solution:** Call ``Client.getClient()`` in application initialization code before using any API methods
|
|
412
|
+
|
|
413
|
+
Scenario 2: Invalid Credentials
|
|
414
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
415
|
+
|
|
416
|
+
**Issue:** 401 Unauthorized errors during initialization
|
|
417
|
+
|
|
418
|
+
**Solution:** Verify all credentials (API key, client ID, org ID) from IBM Cloud console
|
|
419
|
+
|
|
420
|
+
Scenario 3: Missing Configuration
|
|
421
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
422
|
+
|
|
423
|
+
**Issue:** Configuration error thrown before API call
|
|
424
|
+
|
|
425
|
+
**Solution:** Ensure all required parameters are provided based on authentication method
|
|
426
|
+
|
|
427
|
+
Scenario 4: Client Not Initialized
|
|
428
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
429
|
+
|
|
430
|
+
**Issue:** Error when calling API methods
|
|
431
|
+
|
|
432
|
+
**Solution:** Verify ``Client.getClient()`` was called successfully before using API methods
|
|
433
|
+
|
|
434
|
+
----
|
|
435
|
+
|
|
436
|
+
Troubleshooting Steps
|
|
437
|
+
----------------------
|
|
438
|
+
|
|
439
|
+
1. **Verify Configuration**
|
|
440
|
+
|
|
441
|
+
- Check all required parameters are provided
|
|
442
|
+
- Ensure no parameters are empty or null
|
|
443
|
+
- Verify host and authUrl match your environment
|
|
444
|
+
|
|
445
|
+
2. **Verify Credentials**
|
|
446
|
+
|
|
447
|
+
- Confirm API key is valid and not expired
|
|
448
|
+
- Check organization ID matches your IBM Cloud account
|
|
449
|
+
- Verify client ID is correct
|
|
450
|
+
|
|
451
|
+
3. **Check Initialization**
|
|
452
|
+
|
|
453
|
+
- Ensure ``Client.getClient()`` is called before API methods
|
|
454
|
+
- Verify initialization completed without errors
|
|
455
|
+
- Check for any error messages during initialization
|
|
456
|
+
|
|
457
|
+
4. **Network and Service**
|
|
458
|
+
|
|
459
|
+
- Verify network connectivity
|
|
460
|
+
- Check authentication service availability
|
|
461
|
+
- Ensure firewall allows connections to IBM Cloud
|
|
462
|
+
|
|
463
|
+
----
|
|
464
|
+
|
|
465
|
+
Support
|
|
466
|
+
-------
|
|
467
|
+
|
|
468
|
+
For issues not covered in this guide:
|
|
469
|
+
|
|
470
|
+
1. Verify SDK version is up to date
|
|
471
|
+
2. Check IBM Cloud service status
|
|
472
|
+
3. Review error messages and HTTP status codes
|
|
473
|
+
4. Contact IBM Support with:
|
|
474
|
+
|
|
475
|
+
- Error message and stack trace
|
|
476
|
+
- SDK version
|
|
477
|
+
- Environment details
|
|
478
|
+
- Steps to reproduce (excluding sensitive credentials)
|
|
479
|
+
|
|
480
|
+
----
|
|
481
|
+
|
|
482
|
+
**Last Updated:** 2025-11-24
|
|
483
|
+
|
|
484
|
+
**SDK Version:** 1.0.1
|
|
485
|
+
|
|
486
|
+
**Coverage:** SDK initialization, client instance, and authentication errors
|
package/src/Constants.ts
CHANGED
|
@@ -35,6 +35,8 @@ export const GENERAL_API_UNITS = "/v3/carbon/calculation/units";
|
|
|
35
35
|
export const FACTOR_API_UNITS = "/v3/carbon/factor/units";
|
|
36
36
|
export const TRANSPORTATION_AND_DISTRIBUTION_API_UNITS = "/v3/carbon/transportation-and-distribution/units";
|
|
37
37
|
|
|
38
|
+
export const USAGE_API = "/v3/carbon/usage";
|
|
39
|
+
|
|
38
40
|
export const GET = "GET";
|
|
39
41
|
export const POST = "POST";
|
|
40
42
|
export const CLIENT_SOURCE_HEADER = "X-Client-Source";
|
package/src/api/Calculation.ts
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import { Client } from "../Client";
|
|
2
2
|
import { GENERIC_CALCULATION_API_PATH,CALCULATION_TYPES, POST, GET, GENERAL_API_AREA, GENERAL_API_UNITS } from "../Constants";
|
|
3
3
|
import { CalculationRequest } from "../interfaces/Api";
|
|
4
|
+
import { AreaResponse } from "../interfaces/response/AreaResponse";
|
|
5
|
+
import { EmissionResponse } from "../interfaces/response/EmissionResponse";
|
|
6
|
+
import { EmissionResponseWithDetails } from "../interfaces/response/EmissionResponseWithDetails";
|
|
7
|
+
import { TypeResponse } from "../interfaces/response/TypeResponse";
|
|
8
|
+
import { UnitResponse } from "../interfaces/response/UnitResponse";
|
|
4
9
|
import { makeApiRequest } from "../request";
|
|
5
10
|
|
|
6
11
|
|
|
7
12
|
/**
|
|
8
13
|
* Performs emission calculations by making a POST request to the generic calculation API endpoint.
|
|
9
|
-
*
|
|
14
|
+
*
|
|
10
15
|
* @export
|
|
11
16
|
* @param {CalculationRequest} payload - The calculation request data to be sent to the API
|
|
12
|
-
* @return {Promise<
|
|
17
|
+
* @return {Promise<EmissionResponse | EmissionResponseWithDetails>} A promise that resolves to the emission calculation result. Returns EmissionResponseWithDetails if includeDetails is true, otherwise EmissionResponse
|
|
13
18
|
* @throws {Error} May throw an error if the API request fails
|
|
14
|
-
*
|
|
19
|
+
*
|
|
15
20
|
* @example
|
|
16
21
|
* const calculationRequest = {
|
|
17
22
|
"time" : {
|
|
@@ -32,11 +37,11 @@ import { makeApiRequest } from "../request";
|
|
|
32
37
|
*/
|
|
33
38
|
export async function calculate(
|
|
34
39
|
payload: CalculationRequest
|
|
35
|
-
): Promise<
|
|
40
|
+
): Promise<EmissionResponse | EmissionResponseWithDetails> {
|
|
36
41
|
const client = Client.getInstance();
|
|
37
42
|
const url = client.getDomain() + GENERIC_CALCULATION_API_PATH;
|
|
38
43
|
|
|
39
|
-
return makeApiRequest<
|
|
44
|
+
return makeApiRequest<EmissionResponse | EmissionResponseWithDetails>({
|
|
40
45
|
method: POST,
|
|
41
46
|
url,
|
|
42
47
|
data: payload,
|
|
@@ -48,17 +53,17 @@ export async function calculate(
|
|
|
48
53
|
* Retrieves available calculation types by making a GET request to the calculation types API endpoint.
|
|
49
54
|
*
|
|
50
55
|
* @export
|
|
51
|
-
* @return {Promise<
|
|
56
|
+
* @return {Promise<TypeResponse>} A promise that resolves to a TypeResponse containing the available calculation types
|
|
52
57
|
* @throws {Error} May throw an error if the API request fails
|
|
53
58
|
*
|
|
54
59
|
* @example
|
|
55
60
|
* const types = await getTypes();
|
|
56
61
|
*/
|
|
57
|
-
export async function getTypes(): Promise<
|
|
62
|
+
export async function getTypes(): Promise<TypeResponse> {
|
|
58
63
|
const client = Client.getInstance();
|
|
59
64
|
const url = client.getDomain() + CALCULATION_TYPES;
|
|
60
65
|
|
|
61
|
-
return makeApiRequest<
|
|
66
|
+
return makeApiRequest<TypeResponse>({
|
|
62
67
|
method: GET,
|
|
63
68
|
url
|
|
64
69
|
});
|
|
@@ -68,17 +73,17 @@ export async function getTypes(): Promise<string> {
|
|
|
68
73
|
* Retrieves information about geographical areas supported by the calculation API.
|
|
69
74
|
*
|
|
70
75
|
* @export
|
|
71
|
-
* @return {Promise<
|
|
76
|
+
* @return {Promise<AreaResponse>} A promise that resolves to an AreaResponse containing the supported geographical areas
|
|
72
77
|
* @throws {Error} May throw an error if the API request fails
|
|
73
78
|
*
|
|
74
79
|
* @example
|
|
75
80
|
* const areas = await getArea();
|
|
76
81
|
*/
|
|
77
|
-
export async function getArea(): Promise<
|
|
82
|
+
export async function getArea(): Promise<AreaResponse> {
|
|
78
83
|
const client = Client.getInstance();
|
|
79
84
|
const url = client.getDomain() + GENERAL_API_AREA;
|
|
80
85
|
|
|
81
|
-
return makeApiRequest<
|
|
86
|
+
return makeApiRequest<AreaResponse>({
|
|
82
87
|
method: GET,
|
|
83
88
|
url
|
|
84
89
|
});
|
|
@@ -89,7 +94,7 @@ export async function getArea(): Promise<string> {
|
|
|
89
94
|
*
|
|
90
95
|
* @export
|
|
91
96
|
* @param {string} type - The calculation type to get units for
|
|
92
|
-
* @return {Promise<
|
|
97
|
+
* @return {Promise<UnitResponse>} A promise that resolves to a UnitResponse containing the available units
|
|
93
98
|
* @throws {Error} May throw an error if the API request fails
|
|
94
99
|
*
|
|
95
100
|
* @example
|
|
@@ -101,11 +106,11 @@ export async function getArea(): Promise<string> {
|
|
|
101
106
|
*/
|
|
102
107
|
export async function getUnits(
|
|
103
108
|
type: string
|
|
104
|
-
): Promise<
|
|
109
|
+
): Promise<UnitResponse> {
|
|
105
110
|
const client = Client.getInstance();
|
|
106
111
|
const url = client.getDomain() + GENERAL_API_UNITS;
|
|
107
112
|
|
|
108
|
-
return makeApiRequest<
|
|
113
|
+
return makeApiRequest<UnitResponse>({
|
|
109
114
|
method: GET,
|
|
110
115
|
url,
|
|
111
116
|
params : { type }
|