UNPKG

ltdreams-jdk-api-client

Version:
91 lines (67 loc) 2.31 kB
import axios from 'axios' export class LTDreamsClient { constructor() { this.apiBaseUrl = null; this.headers=null; } /** * Connects to the domain and retrieves the base API URL. * @param {string} domain - The domain (e.g., "ltdreams.com") * @param env.Authorized ->using different token */ connect(domain, headers) { if (!domain) { throw new Error('api_base_url not found in response'); } this.apiBaseUrl = domain; this.headers = headers; console.log("connect setup ready!"); } /*** * getInfo * */ getInfo(){ return { apiBaseUrl: this.apiBaseUrl, headers: this.headers }; } /** * Sample API call: tode (example function) * @returns {Promise<object>} */ callApi(apiId, request) { if (!this.apiBaseUrl) { return Promise.reject(new Error('Not connected. Call connect(domain) first.')); } const url = `${this.apiBaseUrl}/apiCallByRequest/${encodeURIComponent(apiId)}`; return axios .post(url, request, { headers: this.headers }) // use POST if sending a body .then(res => res.data) .catch(err => { throw new Error(`Failed to call /callApi: ${err.message}`); }); } /*** * findLearningKitByid */ findLearningKitByid(kitId) { if (!this.apiBaseUrl) { return Promise.reject(new Error('Not connected. Call connect(domain) first.')); } const url = `${this.apiBaseUrl}/v3/edu/ComponentRepo/findByIdAndAccess/${encodeURIComponent(kitId)}`; return axios .get(url, { headers: this.headers }) // use POST if sending a body .then(res => res.data) .catch(err => { throw new Error(`Failed to call /findLearningKitByid: ${err.message}`); }); } } // Export as default for `import LTDreamsClient from '...'` export default LTDreamsClient;