Skip to content

Commit

Permalink
feat: Add GitHub profile fetching option (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b authored and hsb1007 committed Jul 1, 2024
1 parent 03dbf95 commit f065c1b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ PROFILES_DIRECTORY=
REDIS_URL=redis://localhost:6379

LINKEDIN_PROFILE_FETCHER_API_KEY=
LINKEDIN_PROFILE_FETCHER_HOST=\
LINKEDIN_PROFILE_FETCHER_HOST=

GITHUB_PROFILE_FETCHER_API_KEY=

SESSION_SECRET=
31 changes: 31 additions & 0 deletions src/user/profile-fetcher/github.profile-fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { InternalServerErrorException } from '@nestjs/common';
import { ProfileDetails, ProfileFetcher } from './base.profile-fetcher';
import { SocialAccountType } from '@prisma/client';

export class GitHubProfileFetcher extends ProfileFetcher {
async getProfileDetails(): Promise<ProfileDetails> {
const apiKey = process.env.GITHUB_PROFILE_FETCHER_API_KEY;

const username = this.profileUrl.split('https://github.com/')[1];

const response = await fetch(`https://api.github.com/users/${username}`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
});

if (response.ok) {
const data = await response.json();
return {
name: data.name,
socialAccountType: SocialAccountType.GITHUB,
profileUrl: data.html_url,
headline: data.bio,
socialId: data.login,
profilePictureUrl: data.avatar_url,
};
} else {
throw new InternalServerErrorException('Failed to fetch GitHub profile');
}
}
}
3 changes: 3 additions & 0 deletions src/user/profile-fetcher/profile-fetcher.factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { BadRequestException } from '@nestjs/common';
import { LinkedInProfileFetcher } from './linkedin.profile-fetcher';
import { GitHubProfileFetcher } from './github.profile-fetcher';

export class ProfileFetcherFactory {
generateProfileFetcher(profileUrl: string) {
if (profileUrl.startsWith('https://www.linkedin.com')) {
return new LinkedInProfileFetcher(profileUrl);
} else if (profileUrl.startsWith('https://github.com')) {
return new GitHubProfileFetcher(profileUrl);
}
throw new BadRequestException('Unsupported profile URL');
}
Expand Down

0 comments on commit f065c1b

Please sign in to comment.