Skip to content

Commit

Permalink
feat: GitHub api retry (vn7n24fzkq#42)
Browse files Browse the repository at this point in the history
* Add github token updater & error card

* Fix missing required module

* Fix constant variable

* Fix github token updater

* Update github updater

* Update github token index

* Add github token updater log

* Update token name

* Update web host

* Add github button

* Update web UI

Co-authored-by: profile-summary-cards[bot] <[email protected]>
  • Loading branch information
vn7n24fzkq and profile-summary-cards[bot] authored Apr 8, 2021
1 parent 3a1f607 commit fec7d89
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 22 deletions.
27 changes: 24 additions & 3 deletions api/cards/most-commit-language.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
const {
getCommitsLanguageSVGWithThemeName,
} = require('../../src/cards/most-commit-lauguage-card');
const { changToNextGitHubToken } = require('../utils/github-token-updater');
const { getErrorMsgCard } = require('../utils/error-card');

module.exports = async (req, res) => {
const { username, theme } = req.query;
const cardSVG = await getCommitsLanguageSVGWithThemeName(username, theme);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
try {
let tokenIndex = 0;
while (true) {
try {
const cardSVG = await getCommitsLanguageSVGWithThemeName(
username,
theme
);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
return;
} catch (err) {
console.log(err.message);
// We update github token and try again, until getNextGitHubToken throw an Error
changToNextGitHubToken(tokenIndex);
tokenIndex += 1;
}
}
} catch (err) {
console.log(err);
res.send(getErrorMsgCard(err.message, theme));
}
};
27 changes: 24 additions & 3 deletions api/cards/productive-time.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
const {
getProductiveTimeSVGWithThemeName,
} = require('../../src/cards/productive-time-card');
const { changToNextGitHubToken } = require('../utils/github-token-updater');
const { getErrorMsgCard } = require('../utils/error-card');

module.exports = async (req, res) => {
const { username, theme } = req.query;
const cardSVG = await getProductiveTimeSVGWithThemeName(username, theme);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
try {
let tokenIndex = 0;
while (true) {
try {
const cardSVG = await getProductiveTimeSVGWithThemeName(
username,
theme
);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
return;
} catch (err) {
console.log(err.message);
// We update github token and try again, until getNextGitHubToken throw an Error
changToNextGitHubToken(tokenIndex);
tokenIndex += 1;
}
}
} catch (err) {
console.log(err);
res.send(getErrorMsgCard(err.message, theme));
}
};
27 changes: 24 additions & 3 deletions api/cards/profile-details.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
const {
getProfileDetailsSVGWithThemeName,
} = require('../../src/cards/profile-details-card');
const { changToNextGitHubToken } = require('../utils/github-token-updater');
const { getErrorMsgCard } = require('../utils/error-card');

module.exports = async (req, res) => {
const { username, theme } = req.query;
const cardSVG = await getProfileDetailsSVGWithThemeName(username, theme);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
try {
let tokenIndex = 0;
while (true) {
try {
const cardSVG = await getProfileDetailsSVGWithThemeName(
username,
theme
);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
return;
} catch (err) {
console.log(err.message);
// We update github token and try again, until getNextGitHubToken throw an Error
changToNextGitHubToken(tokenIndex);
tokenIndex += 1;
}
}
} catch (err) {
console.log(err);
res.send(getErrorMsgCard(err.message, theme));
}
};
27 changes: 24 additions & 3 deletions api/cards/repos-per-language.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
const {
getReposPerLanguageSVGWithThemeName,
} = require('../../src/cards/repos-per-language-card');
const { changToNextGitHubToken } = require('../utils/github-token-updater');
const { getErrorMsgCard } = require('../utils/error-card');

module.exports = async (req, res) => {
const { username, theme } = req.query;
const cardSVG = await getReposPerLanguageSVGWithThemeName(username, theme);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
try {
let tokenIndex = 0;
while (true) {
try {
const cardSVG = await getReposPerLanguageSVGWithThemeName(
username,
theme
);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
return;
} catch (err) {
console.log(err.message);
// We update github token and try again, until getNextGitHubToken throw an Error
changToNextGitHubToken(tokenIndex);
tokenIndex += 1;
}
}
} catch (err) {
console.log(err);
res.send(getErrorMsgCard(err.message, theme));
}
};
24 changes: 21 additions & 3 deletions api/cards/stats.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
const { getStatsSVGWithThemeName } = require('../../src/cards/stats-card');
const { changToNextGitHubToken } = require('../utils/github-token-updater');
const { getErrorMsgCard } = require('../utils/error-card');

module.exports = async (req, res) => {
const { username, theme } = req.query;
const cardSVG = await getStatsSVGWithThemeName(username, theme);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
try {
let tokenIndex = 0;
while (true) {
try {
const cardSVG = await getStatsSVGWithThemeName(username, theme);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(cardSVG);
return;
} catch (err) {
console.log(err.message);
// We update github token and try again, until getNextGitHubToken throw an Error
changToNextGitHubToken(tokenIndex);
tokenIndex += 1;
}
}
} catch (err) {
console.log(err);
res.send(getErrorMsgCard(err.message, theme));
}
};
23 changes: 17 additions & 6 deletions api/pages/demo.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -27,7 +25,7 @@
<div id="app">
<v-app>
<v-main>
<v-container>
<v-container fluid class="py-8 px-6">
<a href="https://github.com/vn7n24fzkq/github-profile-summary-cards" class="github-corner" aria-label="View source on GitHub">
<svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true">
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
Expand All @@ -39,6 +37,12 @@
<v-col cols="3">
<v-card width="800">
<v-col md="auto">
<p class="font-weight-black text-center">
GitHub Profile Summary Cards
</p>
<div>
<iframe src="https://ghbtns.com/github-btn.html?user=vn7n24fzkq&repo=github-profile-summary-cards&type=star&count=true&size=large" frameborder="0" scrolling="0" width="100%" height="30" title="GitHub"></iframe>
</div>
<v-switch
v-model="$vuetify.theme.dark"
inset
Expand Down Expand Up @@ -66,6 +70,7 @@
>
Submit
</v-btn>

</v-col>
</v-card>
</v-col>
Expand Down Expand Up @@ -146,6 +151,14 @@
</v-card>
<v-row>
</v-container>
<v-footer padless>
<v-col
class="text-center"
cols="12"
>
<strong>GitHub Profile Summary Cards</strong>
</v-col>
</v-footer>
</v-main>
</v-app>
</div>
Expand All @@ -165,7 +178,7 @@
commitLanguageSource:'',
statsSource:'',
productiveTimeSource:'',
baseURL:'https://github-profile-summary-cards.vercel.app',
baseURL:`https://${window.location.host}`,
},
created(){
axios
Expand Down Expand Up @@ -194,5 +207,3 @@
</script>
</body>
</html>


23 changes: 23 additions & 0 deletions api/utils/error-card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Card = require('../../src/templates/card');
const ThemeMap = require('../../src/const/theme');

const getErrorMsgCard = function (msg, themeName) {
const theme = ThemeMap.get(themeName);
theme.title_color = 'red';

const card = new Card('ERROR!!!', 340, 200, theme);
const svg = card.getSVG();
const panel = svg.append('g').attr('transform', `translate(30,20)`);
panel
.append('text')
.attr('y', `${card.yPadding}`)
.style('font-size', `14px`)
.style('fill', 'red')
.text(msg);

return card.toString();
};

module.exports = {
getErrorMsgCard,
};
12 changes: 12 additions & 0 deletions api/utils/github-token-updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const changToNextGitHubToken = function (currentIndex) {
const tokenName = `GITHUB_TOKEN_${currentIndex + 1}`;
console.log(`Change to ${tokenName}`);
process.env.GITHUB_TOKEN = process.env[tokenName];
if (!process.env.GITHUB_TOKEN) {
throw new Error('No more GITHUB_TOKEN can be used');
}
};

module.exports = {
changToNextGitHubToken,
};
3 changes: 2 additions & 1 deletion src/templates/card.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const ThemeMap = require('../const/theme');
const select = require('d3').select;
const JSDOM = require('jsdom').JSDOM;

Expand All @@ -6,7 +7,7 @@ class Card {
title = 'Title',
width = 1280,
height = 1024,
theme = Theme['default'],
theme = ThemeMap.get('default'),
xPadding = 30,
yPadding = 40
) {
Expand Down

0 comments on commit fec7d89

Please sign in to comment.