p4u-client-ai
Version:
Empower Your Apps with AI: Access Multiple Models, Automate Tasks, Create AI Agents Easily
102 lines • 3.96 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
/**
* AIClient class for interacting with the API at https://app.apihub4ai.com/doc
* Manages user authentication, task operations requests.
*/
class AIClient {
constructor() {
this.headers = {
'content-type': 'application/json',
Authorization: '',
};
this.apiUrl = 'https://api.programmers4u.com';
}
/**
* Asynchronous method to log in with the provided username and password.
*
* @param userName The username of the user.
* @param password The password of the user.
* @returns A Promise that resolves with void.
* @throws Error if the login request fails with a status other than 200.
*/
login(userName, password) {
return __awaiter(this, void 0, void 0, function* () {
const data = {
username: userName,
password: password,
};
const res = yield this.makeRequest('POST', '/auth/login', data);
if (res.status >= 300) {
throw new Error(res.statusText);
}
this.headers.Authorization = `Bearer ${res.data.access_token}` || '';
});
}
/**
* Asynchronous method to make a request to a specified endpoint with optional data.
*
* @param method The HTTP method for the request.
* @param endpoint The endpoint to send the request to.
* @param data Optional data to be sent with the request.
* @returns A Promise that resolves with the AxiosResponse containing the response data.
* @throws Error if the request fails.
*/
makeRequest(method, endpoint, data) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield (0, axios_1.default)({
method,
url: `${this.debugUrl ? this.debugUrl : this.apiUrl}${endpoint}`,
headers: this.headers,
data: data ? JSON.stringify(data) : undefined,
});
return response;
}
catch (err) {
throw err;
}
});
}
pingPong() {
return __awaiter(this, void 0, void 0, function* () {
return this.makeRequest('GET', '/ping');
});
}
listTasks() {
return __awaiter(this, void 0, void 0, function* () {
return this.makeRequest('GET', '/products/tasks');
});
}
runTask(request) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeRequest('POST', '/products/tasks/query', request);
});
}
deleteTask(request) {
return __awaiter(this, void 0, void 0, function* () {
const { idTask } = request;
return this.makeRequest('DELETE', `/products/tasks/${idTask}`);
});
}
createTask(request) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeRequest('PUT', '/products/tasks/', request);
});
}
setLocalUrl(url) {
this.debugUrl = url;
}
}
exports.default = AIClient;
//# sourceMappingURL=index.js.map