Skip to content

Commit 2f50809

Browse files
author
Prathamesh Pandit
committedNov 12, 2019
github restApi
0 parents  commit 2f50809

File tree

7 files changed

+1715
-0
lines changed

7 files changed

+1715
-0
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
.vscode/
3+
server/

‎Evans.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
* Request https://httpbin.org
2+
* Request https://httpbin.org/anything. httpbin.org/anything will look at the request you made, parse it, and echo back to you what you requested. curl’s default is to make a GET request.
3+
* Make a POST request to https://httpbin.org/anything
4+
* Make a GET request to https://httpbin.org/anything, but this time add some query parameters (set value=panda).
5+
* Request google’s robots.txt file (www.google.com/robots.txt)
6+
* Make a GET request to https://httpbin.org/anything and set the header User-Agent: elephant.
7+
* Make a DELETE request to https://httpbin.org/anything
8+
Make a POST request to https://httpbin.com/anything with the JSON body {"value": "panda"}
9+
* Request https://httpbin.org/anything and also get the response headers
10+
* Make the same POST request as the previous exercise, but set the Content-Type header to application/json (because POST requests need to have a content type that matches their body). Look at the json field in the response to see the difference from the previous one.
11+
* Make a GET request to https://httpbin.org/anything and set the header Accept-Encoding: gzip (what happens? why?)
12+
* Put a bunch of a JSON in a file and then make a POST request to https://httpbin.org/anything with the JSON in that file as the body
13+
* Make a request to https://httpbin.org/image and set the header ‘Accept: images/png’. Save the output to a PNG file and open the file in an image viewer. Try the same thing with with different Accept: headers.
14+
* Make a PUT request to https://httpbin.org/anything
15+
Request https://httpbin.org/image/jpeg, save it to a file, and open that file in your image editor.
16+
* Request https://google.com. You’ll get an empty response. Get curl to show you the response headers too, and try to figure out why the response was empty.
17+
* Make any request to https://httpbin.org/anything and just set some nonsense headers (like panda: elephant)
18+
* Request https://httpbin.org/status/404 and https://httpbin.org/status/200. Request them again and get curl to show the response headers.
19+
* Request https://httpbin.org/anything and set a username and password (with -u username:password)
20+
* Download the Twitter homepage (https://twitter.com) in Spanish by setting the Accept-Language: es-ES header.
21+
* Make a request to the Stripe API with curl. (see https://stripe.com/docs/development for how, they give you a test API key). Try making exactly the same request to https://httpbin.org/anything.

‎README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Interacting with GitHub using Rest API
2+
3+
Using the GitHub API, the following functionalities have been added to this project:
4+
5+
* Listing branches in a given repo under an owner.
6+
* Creating a new repo.
7+
* Creating an issue.
8+
* Editing a repo.
9+
* Enabling wiki support for a repo.

‎index.js

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
var request = require('request');
2+
3+
const chalk = require('chalk');
4+
5+
var urlRoot = "https://api.github.ncsu.edu";
6+
7+
// Github Standard Endpoint
8+
//var urlRoot = "https://api.github.com";
9+
// NCSU Enterprise endpoint:
10+
//var urlRoot = "https://api.github.ncsu.edu";
11+
12+
var config = {};
13+
// Retrieve our api token from the environment variables.
14+
config.token = process.env.GITHUBTOKEN;
15+
16+
if( !config.token )
17+
{
18+
console.log(chalk`{red.bold GITHUBTOKEN is not defined!}`);
19+
console.log(`Please set your environment variables with appropriate token.`);
20+
console.log(chalk`{italic You may need to refresh your shell in order for your changes to take place.}`);
21+
process.exit(1);
22+
}
23+
24+
console.log(chalk.green(`Your token is: ${config.token.substring(0,4)}...`));
25+
26+
27+
if (process.env.NODE_ENV != 'test')
28+
{
29+
(async () => {
30+
await listAuthenicatedUserRepos();
31+
await listBranches("pppandi2", "HW1-510");
32+
await createRepo("pppandi2","Test-Repo");
33+
await createIssue("pppandi2", "HW1-510", "Issue created using REST Api", "This issue was created using REST Api for the purpose of learning.");
34+
await enableWikiSupport("pppandi2", "Test-Repo");
35+
36+
})()
37+
}
38+
39+
function getDefaultOptions(endpoint, method)
40+
{
41+
var options = {
42+
url: urlRoot + endpoint,
43+
method: method,
44+
headers: {
45+
"User-Agent": "CSC510-REST-WORKSHOP",
46+
"content-type": "application/json",
47+
"Authorization": `token ${config.token}`
48+
}
49+
};
50+
return options;
51+
}
52+
53+
async function getUser()
54+
{
55+
let options = getDefaultOptions("/user", "GET");
56+
57+
// Send a http request to url and specify a callback that will be called upon its return.
58+
return new Promise(function(resolve, reject)
59+
{
60+
request(options, function (error, response, body) {
61+
62+
resolve( JSON.parse(body).login );
63+
});
64+
});
65+
}
66+
67+
function listAuthenicatedUserRepos()
68+
{
69+
let options = getDefaultOptions("/user/repos?visibility=all", "GET");
70+
71+
// Send a http request to url and specify a callback that will be called upon its return.
72+
return new Promise(function(resolve, reject)
73+
{
74+
request(options, function (error, response, body)
75+
{
76+
if( error )
77+
{
78+
console.log( chalk.red( error ));
79+
reject(error);
80+
return; // Terminate execution.
81+
}
82+
83+
var obj = JSON.parse(body);
84+
// console.log(JSON.stringify(body));
85+
console.log('Authenticated user repositories:');
86+
for( var i = 0; i < obj.length; i++ )
87+
{
88+
var name = obj[i].name;
89+
console.log( name );
90+
}
91+
console.log();
92+
// Return object for people calling our method.
93+
resolve(obj);
94+
});
95+
});
96+
97+
}
98+
99+
// 1. Write code for listBranches in a given repo under an owner. See list branches
100+
async function listBranches(owner,repo)
101+
{
102+
let options = getDefaultOptions(`/repos/`+owner+`/`+repo+`/branches`, "GET");
103+
// console.log(options);
104+
// Send a http request to url and specify a callback that will be called upon its return.
105+
return new Promise(function(resolve, reject)
106+
{
107+
request(options, function (error, response, body) {
108+
109+
if (error) {
110+
console.log(chalk.red(error));
111+
reject(error);
112+
return; // Terminate execution.
113+
}
114+
var obj = JSON.parse(body);
115+
console.log('Branches belonging to the '+repo+' repository:');
116+
for (var i = 0; i < obj.length; i++) {
117+
var name = obj[i].name;
118+
console.log(name);
119+
}
120+
console.log();
121+
resolve(obj);
122+
123+
});
124+
});
125+
}
126+
127+
// 2. Write code to create a new repo
128+
async function createRepo(owner,repo)
129+
{
130+
let options = getDefaultOptions("/user/repos", "POST");
131+
options.body = JSON.stringify({
132+
name: repo,
133+
description:'This repo was created using REST Api'
134+
})
135+
// Send a http request to url and specify a callback that will be called upon its return.
136+
return new Promise(function(resolve, reject)
137+
{
138+
request(options, function (error, response, body) {
139+
if (error) {
140+
console.log(chalk.red(error));
141+
reject(error);
142+
return; // Terminate execution.
143+
}
144+
145+
resolve( response.statusCode );
146+
147+
});
148+
});
149+
150+
}
151+
152+
// 3. Write code for creating an issue for an existing repo.
153+
async function createIssue(owner, repo, issueName, issueBody)
154+
{
155+
let options = getDefaultOptions("/repos/"+owner+"/"+repo+"/issues", "POST");
156+
157+
var details = {
158+
title:issueName,
159+
body:issueBody
160+
}
161+
162+
options.body = JSON.stringify(details);
163+
// console.log(options)
164+
// Send a http request to url and specify a callback that will be called upon its return.
165+
return new Promise(function(resolve, reject)
166+
{
167+
request(options, function (error, response, body) {
168+
if (error) {
169+
console.log(chalk.red(error));
170+
reject(error);
171+
return; // Terminate execution.
172+
}
173+
// if(response.statusCode==201){console.log('successful!')}
174+
resolve( response.statusCode );
175+
176+
});
177+
});
178+
}
179+
180+
// 4. Write code for editing a repo to enable wiki support.
181+
async function enableWikiSupport(owner, repo)
182+
{
183+
let options = getDefaultOptions("/repos/"+owner+"/"+repo, "PATCH");
184+
185+
options.body = JSON.stringify({
186+
has_wiki:true
187+
})
188+
189+
// console.log(options)
190+
191+
// Send a http request to url and specify a callback that will be called upon its return.
192+
return new Promise(function(resolve, reject)
193+
{
194+
request(options, function (error, response, body) {
195+
if (error) {
196+
console.log(chalk.red(error));
197+
reject(error);
198+
return; // Terminate execution.
199+
}
200+
console.log(JSON.parse(body));
201+
resolve( JSON.parse(body) );
202+
});
203+
});
204+
}
205+
206+
module.exports.getUser = getUser;
207+
module.exports.listAuthenicatedUserRepos = listAuthenicatedUserRepos;
208+
module.exports.listBranches = listBranches;
209+
module.exports.createRepo = createRepo;
210+
module.exports.createIssue = createIssue;
211+
module.exports.enableWikiSupport = enableWikiSupport;

‎package-lock.json

+1,397
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "csc510-rest-workshop",
3+
"version": "1.0.0",
4+
"description": "Practice with REST concepts.",
5+
"scripts": {
6+
"test": "mocha --reporter spec"
7+
},
8+
"main": "index.js",
9+
"keywords": [
10+
"REST"
11+
],
12+
"dependencies": {
13+
"chalk": "^2.4.2",
14+
"parse-link-header": "^1.0.1",
15+
"request": "^2.88.0"
16+
},
17+
"devDependencies": {
18+
"chai": "^4.2.0",
19+
"mocha": "^6.2.0"
20+
}
21+
}

‎test/test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var chai = require('chai');
2+
var assert = chai.assert,
3+
expect = chai.expect;
4+
5+
process.env.NODE_ENV = 'test'
6+
var github = require('../index');
7+
8+
// Turn off logging
9+
console.log = function(){};
10+
11+
describe("GitHub EndPoint Tests", function() {
12+
13+
this.timeout(5000);
14+
it("listAuthenicatedUserRepos returns repo objects", async function() {
15+
16+
let repos = await github.listAuthenicatedUserRepos();
17+
expect(repos).to.be.an('array').that.have.nested.property('[1].owner.login');
18+
});
19+
20+
it("listBranches returns list branches", async function() {
21+
22+
let user = await github.getUser();
23+
let repos = await github.listBranches(user,"HW1-510");
24+
expect(repos).to.be.an('array').that.have.nested.property("[0].name").equals("master");
25+
26+
});
27+
28+
it("createRepo successfully creates repo", async function() {
29+
30+
let user = await github.getUser();
31+
let status = await github.createRepo(user, "test-HW1-510");
32+
expect(status).to.equal(201);
33+
34+
});
35+
36+
37+
it("createIssue successfully creates issue", async function() {
38+
39+
let user = await github.getUser();
40+
let status = await github.createIssue(user, "HW1-510", "issue name", "issue body");
41+
expect(status).to.equal(201);
42+
43+
});
44+
45+
it("enableWikiSupport successfully enables wiki support", async function() {
46+
47+
let user = await github.getUser();
48+
let response = await github.enableWikiSupport(user, "HW1-510");
49+
50+
expect(response).to.have.property('has_wiki');
51+
expect(response.has_wiki).to.equal(true);
52+
});
53+
});

0 commit comments

Comments
 (0)
Please sign in to comment.