Echoesong / DestinyCharacterViewer

1 stars 1 forks source link

Glow #12

Open Echoesong opened 1 year ago

Echoesong commented 1 year ago

from django.http import HttpResponseRedirect
from django.conf import settings
import uuid
import requests
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
from django.views.generic.edit import DeleteView
from .models import *
def bungie_callback(request):

    print('Bungie callback')
    code = request.GET.get('code')
    state = request.GET.get('state')
    stored_state = request.session.get('bungie_auth_state')

    if state != stored_state:
        return JsonResponse({"error": "Invalid state parameter"})

    data = {
        "grant_type": "authorization_code",
        "code": code,
        "client_id": settings.BUNGIE_CLIENT_ID,

    }
    response = requests.post(settings.BUNGIE_TOKEN_URL, data=data)
    token_data = response.json()
    membership_id = token_data["membership_id"]
    bearer = token_data['token_type']
    token = token_data['access_token']
    # Possibly: check if the BungieID exists in our database, if so, don't create/route back. if NOT present, continue below
    # Use token data to make a second request from Bungie API, then create profile

    response2 = requests.get(f'https://www.bungie.net/Platform/Destiny2/3/Profile/{membership_id}/LinkedProfiles', headers={'x-api-key': settings.BUNGIE_API_KEY})

    destiny2_data = response2.json()
    # NOTE: Eventually need to handle edge case of when existing user tries to connect
    destiny2_membership_id = destiny2_data['Response']['profiles'][0]['membershipId']

    existing_profile = Profile.objects.filter(user=request.user)
    if not existing_profile :

      Profile.objects.create(
        user=request.user, 
        access_token=token_data['access_token'], 
        token_type=bearer, 
        expires_in=token_data['expires_in'], 
        membership_id=token_data['membership_id'], 
        destiny2_membership_id=destiny2_membership_id)
      print('Request resolved')
      # Instead of redirecting to home, chain this request with the request to get destinyMembershipId
    else:
       existing_profile.update(
        access_token=token_data['access_token'], 
        token_type=bearer, 
        expires_in=token_data['expires_in'], 
        membership_id=token_data['membership_id'], 
        destiny2_membership_id=destiny2_membership_id
        )

    return redirect('gatheringdata')

We are most proud of the above function. This function is quite long and does a lot, but its ultimate function is to instantiate a Profile with two added properties we receive from API calls:

  1. The access token to authorize further calls from the Bungie API
  2. The Destiny 2 Membership Id, which is used as part of the target for further API calls.

We are proud of this function because it is the synthesis of what we learned about the Bungie API with our existing/developing knowledge of Django.

krabecb commented 1 year ago

Very impressive! 👏🏽 Excellent work, both of you!