Skip to content

Commit a388a30

Browse files
committed
fix: Too Many Requests Toggl
1 parent c4b6c2d commit a388a30

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/libs/toggl.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
TimeEntry
1717
} from './toggl-types';
1818
import { EnrichedCustomer, EnrichedProject } from './types';
19-
import { initFetch, Logger } from './utils';
19+
import { initFetch, Logger, sleep } from './utils';
2020

2121
const BASE_URL = 'https://api.track.toggl.com/api/v8';
2222
const TIME_ENTRIES_PATH = 'time_entries';
@@ -175,11 +175,14 @@ export const enrichTimeEntries = async (
175175
billableTimeEntries: TimeEntry[]
176176
): Promise<ClientTimeEntries[]> => {
177177
const customers: Grouped = {};
178+
let cached = false;
178179
for (const timeEntry of billableTimeEntries) {
179180
const { duration, pid } = timeEntry;
180181

181182
const togglProject = await fetchProject(pid);
183+
if (!cached) await sleep(1000);
182184
const togglCustomer = await fetchClient(togglProject.cid);
185+
cached = true;
183186

184187
const contentfulCustomers = await fetchCustomers();
185188
const contentfulCustomer = contentfulCustomers.find(
@@ -291,6 +294,7 @@ export async function updateTogglClients() {
291294
({ name, notes }) =>
292295
notes === customer.sys.id || customer.fields.name === name
293296
);
297+
await sleep(1000);
294298
await fetch(`${BASE_URL}/${CLIENT_PATH}${found ? `/${found.id}` : ''}`, {
295299
method: found ? 'PUT' : 'POST',
296300
body: JSON.stringify({
@@ -306,6 +310,7 @@ export async function updateTogglClients() {
306310

307311
export async function updateTogglProjects() {
308312
const togglClients = await fetch<Customer[]>(`${BASE_URL}/${CLIENT_PATH}`);
313+
await sleep(1000);
309314
const togglProjects = await fetch<Project[]>(
310315
`${BASE_URL}/${WORKSPACE_PATH}/${WORKSPACE_ID}/${PROJECT_PATH}`
311316
);
@@ -317,6 +322,7 @@ export async function updateTogglProjects() {
317322
const togglProject = togglProjects.find(
318323
({ name, cid }) => cid === togglClient.id && project.fields.name === name
319324
);
325+
await sleep(1000);
320326
await fetch(
321327
`${BASE_URL}/${PROJECT_PATH}${togglProject ? `/${togglProject.id}` : ''}`,
322328
{

0 commit comments

Comments
 (0)