AlvinSeville7cf

GistAPI

Aug 11th, 2021 (edited)
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function UserInfo(name, token) {
  2.   this.name = name;
  3.   this.token = token;
  4. }
  5.  
  6. function Gists(userInfo) {
  7.   let commonOptions = {
  8.     accept: "application/vnd.github.v3+json",
  9.     muteHttpExceptions: true,
  10.     headers: {
  11.       "Authorization": `Basic ${Utilities.base64Encode(`${userInfo.name}:${userInfo.token}`)}`
  12.     },
  13.   };
  14.  
  15.   let GistFileInfo = function(jsonGistFile) {
  16.     this.name = jsonGistFile.filename;
  17.     this.size = jsonGistFile.size;
  18.   }
  19.  
  20.   let GistInfo = function(jsonGist) {
  21.     this.id = jsonGist.id;
  22.     this.description = jsonGist.description;
  23.     this.url = jsonGist.url;
  24.     this.isPublic = jsonGist.public;
  25.     this.files = Object.getOwnPropertyNames(jsonGist.files).map(fileNode => new GistFileInfo(jsonGist.files[fileNode]));
  26.   }
  27.  
  28.   let getError = function(response) {
  29.     return JSON.parse(response.getContentText()).message;
  30.   }
  31.  
  32.   this.list = function(isPublic) {
  33.     result = [];
  34.  
  35.     let response = UrlFetchApp.fetch(`https://api.github.com/gists`, commonOptions);
  36.  
  37.     if (response.getResponseCode() !== 200)
  38.       throw new Error(getError(response));
  39.  
  40.     let allGists = JSON.parse(response.getContentText());
  41.     for (i in allGists)
  42.       if (allGists[i].public === isPublic)
  43.         result.push(new GistInfo(allGists[i]));
  44.    
  45.     return result;
  46.   }
  47.  
  48.   this.getGist = function(id) {
  49.     let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, commonOptions);
  50.  
  51.     if (response.getResponseCode() !== 200)
  52.       throw new Error(getError(response));
  53.    
  54.     return new GistInfo(JSON.parse(response.getContentText()));
  55.   }
  56.  
  57.   this.newGist = function(description, fileName, fileContent) {
  58.     let options = Object.assign({}, commonOptions);
  59.  
  60.     options.method = "post";
  61.     options.payload = `{
  62.       "description": "${description}",
  63.       "files": {
  64.         "${fileName}": {
  65.           "content": "${fileContent}"
  66.         }
  67.       }
  68.     }`;
  69.  
  70.     let response = UrlFetchApp.fetch(`https://api.github.com/gists`, options);
  71.  
  72.     if (response.getResponseCode() !== 201)
  73.       throw new Error(getError(response));
  74.    
  75.     return new GistInfo(JSON.parse(response));
  76.   }
  77.  
  78.   this.removeGist = function(id) {
  79.     let options = Object.assign({}, commonOptions);
  80.  
  81.     options.method = "delete";
  82.  
  83.     let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
  84.    
  85.     if (response.getResponseCode() !== 204)
  86.       throw new Error(getError(response));
  87.   }
  88.  
  89.   this.updateGistDescription = function(id, description) {
  90.     let options = Object.assign({}, commonOptions);
  91.  
  92.     options.method = "patch";
  93.     options.payload = `{
  94.       "description": "${description}"
  95.     }`;
  96.  
  97.     let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
  98.    
  99.     if (response.getResponseCode() !== 200)
  100.       throw new Error(getError(response));
  101.    
  102.     return new GistInfo(JSON.parse(response.getContentText()));
  103.   }
  104.  
  105.   this.newGistFile = function(id, fileName, fileContent) {
  106.     let gist = this.getGist(id);
  107.     if (gist.files.filter(file => file.name === fileName).length > 0)
  108.       throw new Error(`File ${fileName} already exists.`);
  109.  
  110.     let options = Object.assign({}, commonOptions);
  111.  
  112.     options.method = "patch";
  113.     options.payload = `{
  114.       "files": {
  115.         "${fileName}": {
  116.           "content": "${fileContent}"
  117.         }
  118.       }
  119.     }`;
  120.  
  121.     let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
  122.    
  123.     if (response.getResponseCode() !== 200)
  124.       throw new Error(getError(response));
  125.    
  126.     return new GistInfo(JSON.parse(response.getContentText()));
  127.   }
  128.  
  129.   this.removeGistFile = function(id, fileName) {
  130.     let gist = this.getGist(id);
  131.     if (gist.files.filter(file => file.name === fileName).length === 0)
  132.       throw new Error(`File ${fileName} doesn't exist.`);
  133.  
  134.    let options = Object.assign({}, commonOptions);
  135.  
  136.    options.method = "patch";
  137.    options.payload = `{
  138.      "files": {
  139.        "${fileName}": {
  140.          "content": ""
  141.        }
  142.      }
  143.    }`;
  144.  
  145.    let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
  146.    
  147.    if (response.getResponseCode() !== 200)
  148.      throw new Error(getError(response));
  149.    
  150.    return new GistInfo(JSON.parse(response.getContentText()));
  151.  }
  152.  
  153.  this.updateGistFile = function(id, fileName, fileContent) {
  154.    let gist = this.getGist(id);
  155.    if (gist.files.filter(file => file.name === fileName).length === 0)
  156.      throw new Error(`File ${fileName} doesn't exist.`);
  157.  
  158.     let options = Object.assign({}, commonOptions);
  159.  
  160.     options.method = "patch";
  161.     options.payload = `{
  162.       "files": {
  163.         "${fileName}": {
  164.           "content": "${fileContent}"
  165.         }
  166.       }
  167.     }`;
  168.  
  169.     let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
  170.    
  171.     if (response.getResponseCode() !== 200)
  172.       throw new Error(getError(response));
  173.    
  174.     return new GistInfo(JSON.parse(response.getContentText()));
  175.   }
  176. }
  177.  
  178. function main() {
  179.   let userName = ScriptProperties.getProperty("user_name");
  180.   let userToken = ScriptProperties.getProperty("user_token");
  181.  
  182.   let gists = new Gists(new UserInfo(userName, userToken));
  183.   console.log(gists.removeGistFile("c7b5b0170125f6d42b2106ee0a2ed2c5", "Script.sh"));
  184. }
Add Comment
Please, Sign In to add comment