Feat: Implementacion consumo de Api Tryton

This commit is contained in:
Rodia 2025-01-17 23:27:12 -05:00
parent 2a937653df
commit eb75a13857
3 changed files with 93 additions and 0 deletions

View File

@ -1,5 +1,6 @@
{
"name": "don-confiao",
"type": "module",
"version": "0.0.0",
"scripts": {
"dev": "vite --host 0.0.0.0",

View File

@ -1,4 +1,5 @@
import DjangoApi from './django-api';
import TrytonApiClient from './tryton-api';
import Api from './api';
class ApiImplementation {
@ -7,6 +8,13 @@ class ApiImplementation {
let apiImplementation;
if (implementation === 'django') {
apiImplementation = new DjangoApi();
} else if (implementation === 'tryton'){
const url = 'http:localhost:8000';
const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1';
const db = 'tryton';
const applicationName = 'sale_don_confiao';
apiImplementation = new TrytonApiClient(
url, key, db, applicationName);
} else {
throw new Error("API implementation don't configured");
}

View File

@ -0,0 +1,84 @@
class TrytonApiClient {
constructor(url, key, db, applicationName) {
this.baseUrl = `${url}/${db}/${applicationName}`;
this.headers = {
'Authorization': `Bearer ${key}`,
'Content-Type': 'application/json'
};
}
async getParties() {
return this._fetch('/parties');
}
async getParty(id){
return this._fetch(`/party/${id}`);
}
async postParty(name) {
return this._post('/parties', { name });
}
async getCategories() {
return this._fetch('/categories');
}
async getProducts() {
return this._fetch('/products');
}
async searchProducts(query) {
return this._fetch(`/search_products/${query}`);
}
async getSales() {
return this._fetch('/sales');
}
async getSale(id) {
return this._fetch(`/sale/${id}`);
}
async postSale(saleData) {
return this._post('/post_sale', saleData);
}
async _fetch(endpoint) {
const response = await fetch(`${this.baseUrl}${endpoint}`, {
method: 'GET',
headers: this.headers
});
return this._handleResponse(response);
}
async _post(endpoint, data) {
const response = await fetch(`${this.baseUrl}${endpoint}`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(data)
});
return this._handleResponse(response);
}
async _handleResponse(response) {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
}
export default TrytonApiClient;
const url = 'http:localhost:8000';
const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1';
const db = 'tryton';
const applicationName = 'sale_don_confiao';
const apiClient = new TrytonApiClient(url, key, db, applicationName);
console.log(
apiClient.getProducts()
.then(data => console.log(data))
.catch(error => console.error('Error:', error)))