diff --git a/.env.example b/.env.example index 2765b65..3c473c9 100644 --- a/.env.example +++ b/.env.example @@ -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= \ No newline at end of file diff --git a/src/user/profile-fetcher/github.profile-fetcher.ts b/src/user/profile-fetcher/github.profile-fetcher.ts new file mode 100644 index 0000000..7dad26d --- /dev/null +++ b/src/user/profile-fetcher/github.profile-fetcher.ts @@ -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 { + 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'); + } + } +} diff --git a/src/user/profile-fetcher/profile-fetcher.factory.ts b/src/user/profile-fetcher/profile-fetcher.factory.ts index 93f4f0f..35123ad 100644 --- a/src/user/profile-fetcher/profile-fetcher.factory.ts +++ b/src/user/profile-fetcher/profile-fetcher.factory.ts @@ -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'); }