✨ Feat: [#72] 깃허브 관련 기능 구현 완료
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import requests
|
||||
|
||||
from .models import *
|
||||
from projects.models import *
|
||||
from portfolios.models import *
|
||||
@@ -5,6 +7,8 @@ from portfolios.models import *
|
||||
from django.utils import timezone
|
||||
from datetime import timedelta
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
DUPLICATE_CHECK = {
|
||||
'email': 'email',
|
||||
@@ -12,6 +16,8 @@ DUPLICATE_CHECK = {
|
||||
'phone': 'phone'
|
||||
}
|
||||
|
||||
GITHUB_TOKEN_URL = "https://github.com/login/oauth/access_token"
|
||||
|
||||
class CheckUserFieldDuplicateService:
|
||||
@staticmethod
|
||||
def check_duplicate(field: str, value: str) -> bool:
|
||||
@@ -92,3 +98,35 @@ class UserToProjectService:
|
||||
@staticmethod
|
||||
def get_scrap_project(user: User):
|
||||
return user.scrapped_projects.filter(is_published=True)
|
||||
|
||||
|
||||
class GithubTokenService:
|
||||
@staticmethod
|
||||
def exchange_code_to_access_token(code: str) -> dict:
|
||||
try:
|
||||
resp = requests.post(
|
||||
GITHUB_TOKEN_URL,
|
||||
headers={"Accept": "application/json"},
|
||||
data = {
|
||||
"client_id": settings.GITHUB_CLIENT_ID,
|
||||
"client_secret": settings.GITHUB_CLIENT_SECRET,
|
||||
"code": code,
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except requests.HTTPError as e:
|
||||
raise Exception(
|
||||
f"Github token exchange failed: {e.response.status_code} - {e.response.text}"
|
||||
)
|
||||
|
||||
def github_token_save(data: dict, user: User):
|
||||
return GithubToken.objects.update_or_create(
|
||||
user=user,
|
||||
defaults={
|
||||
"access_token": data["access_token"],
|
||||
"scope": data.get("scope", ""),
|
||||
"token_type": data.get("token_type", "bearer"),
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user