-
Notifications
You must be signed in to change notification settings - Fork 9
[FEATURE] Add Data API support with automatic routing metadata header… #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danny-beton-lrn
wants to merge
7
commits into
master
Choose a base branch
from
LRN-49160/feature/add_routing_for_data_api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f4f63c
[FEATURE] Add Data API support with automatic routing metadata header…
danny-beton-lrn 10a34c3
[FEATURE] Lint and codacy fixes (LRN-49160)
danny-beton-lrn 0377dcf
[FEATURE] More lint fixes (LRN-49160)
danny-beton-lrn 2cfb1eb
[FEATURE] Codacy fix (LRN-49160)
danny-beton-lrn 9ab0bef
[FEATURE] Codacy fix (LRN-49160)
danny-beton-lrn 836f58e
[Feature] Codacy fix (LRN-49160)
danny-beton-lrn daa15cf
[FEATURE] Update README, optimise error code (LRN-49160)
danny-beton-lrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| # Data API Routing Layer | ||
|
|
||
| The Node.js SDK now includes a routing layer for the Data API, making it easier to interact with Learnosity's Data API without manually handling HTTP requests and signatures. | ||
|
|
||
| ## Features | ||
|
|
||
| - ✅ **Automatic request signing** - No need to manually sign requests | ||
| - ✅ **Built-in HTTP client** - Makes actual HTTP requests to Data API | ||
| - ✅ **Pagination support** - Automatically handles paginated responses | ||
| - ✅ **Iterator methods** - Easy iteration through pages and individual results | ||
| - ✅ **Routing metadata** - Automatically adds ALB routing headers | ||
| - ✅ **Custom HTTP adapter** - Use your own HTTP library (axios, node-fetch, etc.) | ||
|
|
||
| ## Installation | ||
|
|
||
| The DataApi class is included with the SDK: | ||
|
|
||
| ```javascript | ||
| const LearnositySDK = require('learnosity-sdk-nodejs'); | ||
| const DataApi = LearnositySDK.DataApi; | ||
| ``` | ||
|
|
||
| Or import directly: | ||
|
|
||
| ```javascript | ||
| const DataApi = require('learnosity-sdk-nodejs/lib/DataApi'); | ||
| ``` | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ### Simple Request | ||
|
|
||
| ```javascript | ||
| const DataApi = require('learnosity-sdk-nodejs/lib/DataApi'); | ||
|
|
||
| const dataApi = new DataApi({ | ||
| consumerKey: 'your_consumer_key', | ||
| consumerSecret: 'your_consumer_secret', | ||
| domain: 'yourdomain.com' | ||
| }); | ||
|
|
||
| // Make a request | ||
| const response = await dataApi.request( | ||
| 'https://data.learnosity.com/v2023.1.LTS/itembank/items', | ||
| { | ||
| consumer_key: 'your_consumer_key', | ||
| domain: 'yourdomain.com' | ||
| }, | ||
| 'your_consumer_secret', | ||
| { | ||
| limit: 10, | ||
| references: ['item_1', 'item_2'] | ||
| }, | ||
| 'get' | ||
| ); | ||
|
|
||
| const data = await response.json(); | ||
| console.log(data); | ||
| ``` | ||
|
|
||
| ### Paginated Requests | ||
|
|
||
| Automatically iterate through all pages: | ||
|
|
||
| ```javascript | ||
| for await (const page of dataApi.requestIter( | ||
| 'https://data.learnosity.com/v2023.1.LTS/itembank/items', | ||
| { consumer_key: 'xxx', domain: 'example.com' }, | ||
| 'secret', | ||
| { limit: 100 }, | ||
| 'get' | ||
| )) { | ||
| console.log(`Page has ${page.data.length} items`); | ||
| console.log(`Total records: ${page.meta.records}`); | ||
| } | ||
| ``` | ||
|
|
||
| ### Individual Results Iterator | ||
|
|
||
| Iterate through individual results across all pages: | ||
|
|
||
| ```javascript | ||
| for await (const item of dataApi.resultsIter( | ||
| 'https://data.learnosity.com/v2023.1.LTS/itembank/items', | ||
| { consumer_key: 'xxx', domain: 'example.com' }, | ||
| 'secret', | ||
| { limit: 100 }, | ||
| 'get' | ||
| )) { | ||
| console.log(`Item: ${item.reference}`); | ||
| } | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### Constructor | ||
|
|
||
| ```javascript | ||
| new DataApi(options) | ||
| ``` | ||
|
|
||
| **Options:** | ||
| - `consumerKey` (string, optional) - Your Learnosity consumer key | ||
| - `consumerSecret` (string, optional) - Your Learnosity consumer secret | ||
| - `domain` (string, optional) - Your domain for security packet | ||
| - `httpAdapter` (function, optional) - Custom HTTP adapter function | ||
|
|
||
| ### Methods | ||
|
|
||
| #### `request(endpoint, securityPacket, secret, requestPacket, action)` | ||
|
|
||
| Makes a single HTTP request to the Data API. | ||
|
|
||
| **Parameters:** | ||
| - `endpoint` (string) - Full URL to the Data API endpoint | ||
| - `securityPacket` (object) - Security object with `consumer_key` and `domain` | ||
| - `secret` (string) - Consumer secret for signing | ||
| - `requestPacket` (object, optional) - Request parameters | ||
| - `action` (string, optional) - Action type: `'get'`, `'set'`, `'update'`, `'delete'` (default: `'get'`) | ||
|
|
||
| **Returns:** Promise<Response> | ||
|
|
||
| #### `requestIter(endpoint, securityPacket, secret, requestPacket, action)` | ||
|
|
||
| Async generator that yields pages of results, automatically handling pagination. | ||
|
|
||
| **Parameters:** Same as `request()` | ||
|
|
||
| **Yields:** Page objects with `meta` and `data` properties | ||
|
|
||
| #### `resultsIter(endpoint, securityPacket, secret, requestPacket, action)` | ||
|
|
||
| Async generator that yields individual results from the `data` array, automatically handling pagination. | ||
|
|
||
| **Parameters:** Same as `request()` | ||
|
|
||
| **Yields:** Individual result objects | ||
|
|
||
| ## Advanced Usage | ||
|
|
||
| ### Custom HTTP Adapter | ||
|
|
||
| Use your own HTTP library (e.g., axios): | ||
|
|
||
| ```javascript | ||
| const axios = require('axios'); | ||
|
|
||
| const dataApi = new DataApi({ | ||
| consumerKey: 'xxx', | ||
| consumerSecret: 'secret', | ||
| domain: 'example.com', | ||
| httpAdapter: async (url, options) => { | ||
| const response = await axios({ | ||
| method: options.method, | ||
| url: url, | ||
| headers: options.headers, | ||
| data: options.body | ||
| }); | ||
|
|
||
| return { | ||
| ok: response.status >= 200 && response.status < 300, | ||
| status: response.status, | ||
| statusText: response.statusText, | ||
| json: async () => response.data, | ||
| text: async () => JSON.stringify(response.data) | ||
| }; | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| ## Routing Metadata | ||
|
|
||
| The DataApi automatically adds routing metadata headers to all requests: | ||
|
|
||
| - `X-Learnosity-Consumer` - Consumer key | ||
| - `X-Learnosity-Action` - Derived action (e.g., `get_/itembank/items`) | ||
| - `X-Learnosity-SDK` - SDK version (e.g., `Node.js:0.6.2`) | ||
|
|
||
| These headers are used by Learnosity's Application Load Balancer for routing. | ||
|
|
||
| ## Examples | ||
|
|
||
| See the [examples/data-api-example.js](../examples/data-api-example.js) file for complete working examples. | ||
|
|
||
| ## Comparison with Python SDK | ||
|
|
||
| This implementation mirrors the Python SDK's DataApi class, providing: | ||
|
|
||
| - `request()` - Single request (Python: `request()`) | ||
| - `requestIter()` - Page iterator (Python: `request_iter()`) | ||
| - `resultsIter()` - Results iterator (Python: `results_iter()`) | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 Praise: Excellent documentation 🙌