githubapi

by ygongdev

JavaScript

let GithubApi = {
	url: 'https://api.github.com/',
	access_token: 'put your access token here',

	/**
	 * An asychronous helper function to grab data from github
	 * given an url and add data the firebase.
	 *
	 * @param {string} url
	 * @param {Firebase Object} firebaseRef
	 */
	_get_data_from_github_with_firebase: async function(url) {
		try {
			const response = await fetch(url);
			const responseStatus = response.status;
			if (responseStatus === 200) {
				const responseJson = await response.json();
        console.log(responseJson);
        return true;
			}
			return false;
		} catch (error) {
			return false;
		}
  },
    
  get_followings: function() {
    return this._get_data_from_github_with_firebase(
      this.url + 'user/following?' + this.access_token,);
  },

  unfollow_user: async function(username) {
    try {
      const url = this.url + 'user/following/' + username + '?' + this.access_token;
      const response = await fetch(url, { method: 'DELETE'});
      const responseStatus = response.status;
      if (responseStatus === 204) {
        return true;
      }
      return false;
    } catch (error) {
      return false;
    }
  },
}

async function serial() {
	await GithubApi.get_followings();
  await GithubApi.unfollow_user('tomroush');
  await GithubApi.get_followings();
}
//works fine...
serial().then(()=>console.log('done'));
/*GithubApi.get_followings()
.then(() => GithubApi.unfollow_user('tomroush')
.then(() => GithubApi.get_followings()
.then(() => console.log('done'))));*/