Skip to content

Commit 15639c2

Browse files
authored
Introducing (#1)
* Fetches data from a GitHub API endpoint, handling pagination. * Makes an HTTP PUT request to a GitHub API endpoint. * Performs an HTTP DELETE request to a GitHub API endpoint. * Makes an HTTP POST request to a GitHub API endpoint. * Creates an options object for HTTP requests to the GitHub API. * Apps Script JSON
1 parent c53051d commit 15639c2

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

GithubHttpApp/appsscript.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"timeZone": "America/Sao_Paulo",
3+
"dependencies": {
4+
},
5+
"exceptionLogging": "STACKDRIVER",
6+
"runtimeVersion": "V8"
7+
}

GithubHttpApp/deleter.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Performs an HTTP DELETE request to a GitHub API endpoint.
3+
*
4+
* @param {string} GH_TOKEN - The GitHub access token.
5+
* @param {string} URL - The URL of the endpoint to which the request will be sent.
6+
* @returns {Object} A response object from the `UrlFetchApp.fetch` function.
7+
* @throws {Error} Throws an exception if an error occurs during the request.
8+
*/
9+
async function deleter(GH_TOKEN, URL) {
10+
11+
const options = getHttpOptions('DELETE', GH_TOKEN);
12+
13+
try {
14+
15+
return UrlFetchApp.fetch(URL, options);
16+
17+
} catch (error) {
18+
Logger.log('Error deleting: ' + error);
19+
throw error;
20+
}
21+
}

GithubHttpApp/fetcher.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Fetches data from a GitHub API endpoint, handling pagination.
3+
*
4+
* @param {string} GH_TOKEN - The GitHub access token.
5+
* @param {string} URL - The URL of the GitHub API endpoint to fetch data from.
6+
* @returns {Promise<Array<Object>>} A promise that resolves to an array of data objects fetched from the API.
7+
*/
8+
async function fetcher(GH_TOKEN, URL) {
9+
10+
const options = getHttpOptions('GET', GH_TOKEN);
11+
12+
const allData = [];
13+
14+
let page = 1;
15+
16+
while (true) {
17+
18+
const pageUrl = `${URL}?page=${page}`;
19+
20+
const response = await UrlFetchApp.fetch(pageUrl, options);
21+
22+
if (response.getResponseCode() !== 200) {
23+
throw new Error(`API request failed with status ${response.getResponseCode()}: ${response.getContentText()}`);
24+
}
25+
26+
const data = JSON.parse(response.getContentText());
27+
28+
if (data.length === 0) {
29+
break;
30+
}
31+
32+
allData.push(...data);
33+
page++;
34+
35+
}
36+
37+
return allData;
38+
39+
}

GithubHttpApp/poster.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Makes an HTTP POST request to a GitHub API endpoint.
3+
*
4+
* @param {string} GGH_TOKEN - The GitHub access token.
5+
* @param {string} URL - The URL of the endpoint to which the request will be sent.
6+
* @param {object} payload - The request payload, which will be converted to JSON and included in the request body.
7+
* @returns {Object} A response object from the `UrlFetchApp.fetch` function.
8+
* @throws {Error} Throws an exception if an error occurs during the request.
9+
*/
10+
async function poster(GH_TOKEN, URL, payload) {
11+
12+
const options = getHttpOptions('POST', GH_TOKEN, payload);
13+
14+
try {
15+
16+
return UrlFetchApp.fetch(URL, options);
17+
18+
} catch (error) {
19+
Logger.log('Error posting: ' + error);
20+
throw error;
21+
}
22+
}

GithubHttpApp/putter.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Makes an HTTP PUT request to a GitHub API endpoint.
3+
*
4+
* @param {string} GH_TOKEN - The GitHub access token.
5+
* @param {string} URL - The URL of the endpoint to which the request will be sent.
6+
* @param {object} payload - The request payload, which will be converted to JSON and included in the request body.
7+
* @returns {Object} A response object from the `UrlFetchApp.fetch` function.
8+
* @throws {Error} Throws an exception if an error occurs during the request.
9+
*/
10+
async function putter(GH_TOKEN, URL, payload) {
11+
12+
const options = getHttpOptions('PUT', GH_TOKEN, payload);
13+
14+
try {
15+
16+
return UrlFetchApp.fetch(URL, options);
17+
18+
} catch (error) {
19+
Logger.log('Error putting: ' + error);
20+
throw error;
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Creates an options object for HTTP requests to the GitHub API.
3+
*
4+
* @param {string} method - The HTTP method to use (e.g., 'GET', 'POST', 'PUT', 'DELETE').
5+
* @param {string} GH_TOKEN - The access token for the GitHub API.
6+
* @param {object} [payload] - The request payload, which will be converted to JSON and included in the request body.
7+
* @returns {object} An options object to be used with `UrlFetchApp.fetch`.
8+
* @throws {Error} Throws an error if the payload is not an object.
9+
*
10+
*/
11+
function getHttpOptions(method, GH_TOKEN, payload = null) {
12+
13+
const baseOptions = {
14+
'headers': {
15+
'Accept': 'application/vnd.github+json',
16+
'Authorization': `Bearer ${GH_TOKEN}`,
17+
'X-GitHub-Api-Version': '2022-11-28',
18+
},
19+
'method': method,
20+
};
21+
22+
if (payload) {
23+
// Ensure payload is an object and handle potential errors
24+
if (typeof payload !== 'object' || payload === null) {
25+
throw new Error('Invalid payload format. Payload must be an object.');
26+
}
27+
28+
// Convert non-string values to strings for correct JSON serialization
29+
baseOptions.payload = JSON.stringify(payload);
30+
}
31+
32+
return baseOptions;
33+
}

0 commit comments

Comments
 (0)