Hey there, fellow JavaScript devs! Ready to dive into the world of Oracle Cloud HCM API? Let's get our hands dirty with some data syncing for user-facing integrations. Buckle up!
First things first, let's make sure we've got our ducks in a row:
npm install axios dotenv
Now, let's set up our authentication. We'll use OAuth 2.0 because, well, it's 2023 and we're not savages.
require('dotenv').config(); const axios = require('axios'); const token = process.env.ORACLE_HCM_TOKEN; const baseURL = 'https://your-instance.hcm.cloud.oracle.com/hcmRestApi/resources/latest'; const api = axios.create({ baseURL, headers: { Authorization: `Bearer ${token}` } });
Let's grab some employee data, shall we?
async function getEmployees(limit = 10, offset = 0) { try { const response = await api.get(`/workers?limit=${limit}&offset=${offset}`); return response.data; } catch (error) { console.error('Error fetching employees:', error); throw error; } }
Pro tip: Always handle pagination. Your future self will thank you.
Updating employee info is a breeze:
async function updateEmployee(employeeId, data) { try { const response = await api.put(`/workers/${employeeId}`, data); return response.data; } catch (error) { console.error('Error updating employee:', error); throw error; } }
Remember, with great power comes great responsibility. Always validate your data before sending it off!
For real-time sync, webhooks are your best friend. But if you're old school (or Oracle doesn't play nice), polling works too:
async function pollForChanges(interval = 5000) { setInterval(async () => { const changes = await getEmployees(100, 0); // Process changes here }, interval); }
async function retryOperation(operation, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await operation(); } catch (error) { if (i === maxRetries - 1) throw error; await new Promise(resolve => setTimeout(resolve, 1000 * Math.pow(2, i))); } } }
Batch operations are your friend. Use them wisely:
async function batchUpdate(employees) { const batchRequests = employees.map(emp => ({ method: 'PUT', url: `/workers/${emp.id}`, data: emp })); return await api.post('/batch', { requests: batchRequests }); }
Unit test like your life depends on it:
const mockApi = jest.mock('axios'); test('getEmployees fetches data correctly', async () => { mockApi.get.mockResolvedValue({ data: [{ id: 1, name: 'John Doe' }] }); const employees = await getEmployees(); expect(employees).toHaveLength(1); expect(employees[0].name).toBe('John Doe'); });
There you have it, folks! You're now armed and dangerous with Oracle Cloud HCM API knowledge. Remember, with great power comes great responsibility (and hopefully a great user experience). Now go forth and sync that data like a boss!
Happy coding, and may the API gods be ever in your favor! 🚀