diff --git a/tienda_ilusion/don_confiao/frontend/don-confiao/package.json b/tienda_ilusion/don_confiao/frontend/don-confiao/package.json index bb0a559..f4899dd 100644 --- a/tienda_ilusion/don_confiao/frontend/don-confiao/package.json +++ b/tienda_ilusion/don_confiao/frontend/don-confiao/package.json @@ -1,5 +1,6 @@ { "name": "don-confiao", + "type": "module", "version": "0.0.0", "scripts": { "dev": "vite --host 0.0.0.0", diff --git a/tienda_ilusion/don_confiao/frontend/don-confiao/src/services/api-implementation.js b/tienda_ilusion/don_confiao/frontend/don-confiao/src/services/api-implementation.js index 0bbbec6..05016d7 100644 --- a/tienda_ilusion/don_confiao/frontend/don-confiao/src/services/api-implementation.js +++ b/tienda_ilusion/don_confiao/frontend/don-confiao/src/services/api-implementation.js @@ -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"); } diff --git a/tienda_ilusion/don_confiao/frontend/don-confiao/src/services/tryton-api.js b/tienda_ilusion/don_confiao/frontend/don-confiao/src/services/tryton-api.js new file mode 100644 index 0000000..4bfd593 --- /dev/null +++ b/tienda_ilusion/don_confiao/frontend/don-confiao/src/services/tryton-api.js @@ -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)))