Skip to content

Commit

Permalink
Merge pull request #5 from jakejrichards/RefactorPromise
Browse files Browse the repository at this point in the history
 Refactor: Move to promise style api calls and use axios over request
  • Loading branch information
jakejrichards authored Aug 19, 2018
2 parents 9efddc7 + 33789dc commit af30f7a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
68 changes: 44 additions & 24 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
const { getProfile, getRecentMatches, getRecentSummary, getLeaderboards } = require('../index'); // require('cod-api');
const { getLeaderboards, getProfile, getRecentMatches, getRecentSummary } = require('../index'); // require('cod-api');

var options = {
title: "wwii", // bo3, iw, wwii
platform: "psn", // psn, xbl, steam
username: "Consisttt", // username
days: 1, // amount of days (required for recent matches & summary),
type: "core", // core, hc, arena
time: "monthly", // alltime, monthly, weekly
mode: "war" // career, war (Team Deathmatch), dm (Free-For-All), conf (Kill Confirmed), ctf (Capture The Flag), sd (Search & Destroy), dom (Domination), ball (Gridiron), hp (Hardpoint), 1v1, raid (War)
};
const title = "wwii"; // bo3, iw, wwii
const platform = "psn"; // psn, xbl, steam
const username = "Consisttt"; // username
const days = 7; // amount of days (required for recent matches & summary),
const type = "core"; // core, hc, arena
const time = "monthly"; // alltime, monthly, weekly
const mode = "war"; // career, war (Team Deathmatch), dm (Free-For-All), conf (Kill Confirmed), ctf (Capture The Flag), sd (Search & Destroy), dom (Domination), ball (Gridiron), hp (Hardpoint), 1v1, raid (War)

getProfile(options, profile => {
console.log(profile);
});
getProfile({ title, platform, username })
.then(profile => {
// Do something with the profile object
console.log(profile);
})
.catch(err => {
// Do something with this error
console.log(err);
});

getRecentMatches(options, matches => {
console.log(matches);
});

getRecentSummary(options, summary => {
console.log(summary);
});

getLeaderboards(options, leaderboards => {
console.log(leaderboards);
});
getRecentMatches({ title, platform, username, days })
.then(profile => {
// Do something with the recent matches object
console.log(profile);
})
.catch(err => {
// Do something with this error
console.log(err);
});

getRecentSummary({ title, platform, username, days })
.then(profile => {
// Do something with the recent summary object
console.log(profile);
})
.catch(err => {
// Do something with this error
console.log(err);
});

getLeaderboards({ title, platform, time, type, mode, username })
.then(profile => {
// Do something with the leaderboards object
console.log(profile);
})
.catch(err => {
// Do something with this error
console.log(err);
});
102 changes: 47 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,59 @@
const request = require('request');
const axios = require('axios');

const getLeaderboards = (options, callback) => {
const uri = `https://my.callofduty.com/api/papi-client/leaderboards/v2/title/${options.title}/platform/${options.platform}/time/${options.time}/type/${options.type}/mode/${options.mode}/gamer/${options.username}`;
request.get(uri, (error, response, body) => {
const obj = JSON.parse(body);
if (!error){
if (obj.status === "success"){
callback(obj.data);
} else {
console.log("Error: " + obj.data.message);
}
} else {
console.log("Error: " + error);
const COD_API_ENDPOINT = 'https://my.callofduty.com/api/papi-client';

// API Helper
const getDataFromAPI = uri =>
axios.get(uri)
.then(({ data }) => {
const { status, data: error } = data;
if (status !== 'success') {
throw new Error(`cod-api request failed: ${error.message}`);
}
return data;
});

// API Methods
const getLeaderboards = ({ title, platform, time, type, mode, username }) => {
const leaderboardEndpoint = COD_API_ENDPOINT + '/leaderboards/v2';
const uri = leaderboardEndpoint +
`/title/${title}` +
`/platform/${platform}` +
`/time/${time}` +
`/type/${type}` +
`/mode/${mode}` +
`/gamer/${username}`;
return getDataFromAPI(uri);
};

const getProfile = (options, callback) => {
const uri = `https://my.callofduty.com/api/papi-client/crm/cod/v2/title/${options.title}/platform/${options.platform}/gamer/${options.username}/profile/`;
request.get(uri, (error, response, body) => {
const obj = JSON.parse(body);
if (!error){
if (obj.status === "success"){
callback(obj.data);
} else {
console.log("Error: " + obj.data.message);
}
} else {
console.log("Error: " + error);
}
});
const getProfile = ({ title, platform, username }) => {
const profileEndpoint = COD_API_ENDPOINT + '/crm/cod/v2';
const uri = profileEndpoint +
`/title/${title}` +
`/platform/${platform}` +
`/gamer/${username}` +
`/profile`;
return getDataFromAPI(uri);
};

const getRecentMatches = (options, callback) => {
const uri = `https://my.callofduty.com/api/papi-client/crm/cod/v2/title/${options.title}/platform/${options.platform}/gamer/${options.username}/matches/days/${options.days}`;
request.get(uri, (error, response, body) => {
const obj = JSON.parse(body);
if (!error){
if (obj.status === "success"){
callback(obj.data.matches);
} else {
console.log("Error: " + obj.data.message);
}
} else {
console.log("Error: " + error);
}
});
const getRecentMatches = ({ title, platform, username, days }) => {
const recentMatchesEndpoint = COD_API_ENDPOINT + '/crm/cod/v2';
const uri = recentMatchesEndpoint +
`/title/${title}` +
`/platform/${platform}` +
`/gamer/${username}` +
`/matches/days/${days}`;
return getDataFromAPI(uri);
};

const getRecentSummary = (options, callback) => {
const uri = `https://my.callofduty.com/api/papi-client/crm/cod/v2/title/${options.title}/platform/${options.platform}/gamer/${options.username}/matches/days/${options.days}`;
request.get(uri, (error, response, body) => {
const obj = JSON.parse(body);
if (!error){
if (obj.status === "success"){
callback(obj.data.summary);
} else {
console.log("Error: " + obj.data.message);
}
} else {
console.log("Error: " + error);
}
});
const getRecentSummary = ({ title, platform, username, days }) => {
const recentSummaryEndpoint = COD_API_ENDPOINT + '/crm/cod/v2';
const uri = recentSummaryEndpoint +
`/title/${title}` +
`/platform/${platform}` +
`/gamer/${username}` +
`/matches/days/${days}`;
return getDataFromAPI(uri);
};


Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cod-api",
"version": "1.0.3",
"version": "2.0.0",
"description": "A Call of Duty Network API written in Node.js",
"homepage": "https://github.com/jakejrichards/cod-api",
"bugs": {
Expand Down Expand Up @@ -34,7 +34,7 @@
],
"license": "MIT",
"dependencies": {
"request": "^2.83.0"
"axios": "^0.18.0"
},
"repository": "github:jakejrichards/cod-api",
"readmeFilename": "README.md"
Expand Down

0 comments on commit af30f7a

Please sign in to comment.