Readme

(Part of: https://www.val.town/v/vtdocs.resyBot)

Given a valid booking token, this val attempts to make a reservation for the booking token's slot.

There is some retry logic as the API route (rarely) returns an internal server error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { fetch } from "https://esm.town/v/std/fetch";
import { resyPublicAPIKey } from "https://esm.town/v/vtdocs/resyPublicAPIKey";
import { sleep } from "https://esm.town/v/vtdocs/sleep";
export const resyBookSlot = async (
token: string,
bookToken: string,
paymentMethodId: number,
): Promise<{
resyToken: string;
reservationId: number;
}> => {
let bookJSON = null;
// Retry with a little bit of backoff
// (in testing, there was a rare internal server error)
const retryLimit = 5;
let retryCount = 0;
while (true) {
try {
const bookRes = await fetch(`https://api.resy.com/3/book`, {
method: "POST",
headers: {
"authorization": `ResyAPI api_key="${resyPublicAPIKey}"`,
"x-resy-auth-token": token,
"content-type": "application/x-www-form-urlencoded",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
},
body: `book_token=${encodeURIComponent(bookToken)}&struct_payment_method=${
encodeURIComponent(JSON.stringify({
id: paymentMethodId,
}))
}`,
});
bookJSON = await bookRes.json();
}
catch (e) {
console.log(e);
if (retryCount === retryLimit) {
throw new Error(`failed to book: ${e}`);
}
retryCount++;
await sleep(retryCount * 1500);
console.log("retrying...");
continue;
}
break;
}
if (!bookJSON.resy_token || !bookJSON.reservation_id) {
throw new Error(`failed to book: ${bookJSON}`);
}
return {
resyToken: bookJSON.resy_token,
reservationId: bookJSON.reservation_id,
};
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
September 6, 2024