/
DefaultGitHubApiService.ts
58 lines (52 loc) · 2.15 KB
/
DefaultGitHubApiService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as log4js from "log4js";
import fetch from "node-fetch";
import { isLeft } from 'fp-ts/lib/Either';
import {GitHubApiService} from '../domain/GitHubApiService';
import {GitHubRepositoryJsonCacheRepository} from '../domain/GitHubRepositoryJsonCacheRepository';
import {GithubRepoJson, githubRepoJsonType} from '../types';
export class DefaultGitHubApiService implements GitHubApiService {
constructor(
private readonly logger: log4js.Logger,
private readonly githubCredential: {githubClientId: string, githubClientSecret: string} | undefined,
private readonly gitHubRepositoryJsonCacheRepository: GitHubRepositoryJsonCacheRepository) {}
async getRepository(repoName: string): Promise<{repo: GithubRepoJson} | {status: number, resText: string}> {
let jsonStr: string | undefined = await this.gitHubRepositoryJsonCacheRepository.get(repoName);
if (jsonStr === undefined) {
this.logger.info(`Repository JSON ${repoName} is not cached`);
let query = '';
const headers= (() => {
if (this.githubCredential === undefined) {
return {};
} else {
const {githubClientId, githubClientSecret} = this.githubCredential;
const h: {[key: string]: string} = {
// Basic Auth
"Authorization": `Basic: ${Buffer.from(`${githubClientId}:${githubClientSecret}`).toString("base64")}`,
};
query = `?client_id=${githubClientId}&client_secret=${githubClientSecret}`;
return h;
}
})();
const githubRes = await fetch(`https://api.github.com/repos/${repoName}${query}`, {
headers,
});
if (githubRes.status !== 200) {
return {
status: githubRes.status,
resText: await githubRes.text(),
};
}
jsonStr = await githubRes.text();
// Cache
this.gitHubRepositoryJsonCacheRepository.cache(repoName, jsonStr);
}
const githubRepoJsonEither = githubRepoJsonType.decode(JSON.parse(jsonStr));
if (isLeft(githubRepoJsonEither)) {
throw githubRepoJsonEither.left;
}
const githubRepoJson = githubRepoJsonEither.right;
return {
repo: githubRepoJson,
};
}
}