import json
import os
import requests
from urllib.parse import urlunparse, urlencode, ParseResult

MEMBERSHIP_API_FQDN = "www.usgo.org"
MEMBERSHIP_API_ROOT = "/mm/members/"
MEMBERSHIP_API_KEY = os.environ.get("AGA_MM_API_KEY", "")
EMAIL_FIELD_NAME = "email"

def get_email_address(aga_id):
    url = urlunparse(ParseResult(
        scheme="https",
        netloc=MEMBERSHIP_API_FQDN,
        path=os.path.join(MEMBERSHIP_API_ROOT, str(aga_id)),
        params="",
        query=urlencode({"select": EMAIL_FIELD_NAME, "api_key": MEMBERSHIP_API_KEY}),
        fragment=""))
    response = requests.get(url)
    response_data = json.loads(response.data)
    return response_data.get(EMAIL_FIELD_NAME, "")

@@ -1,16 +1,4 @@
-import json%0A
 impo
@@ -107,12 +107,8 @@
  = %22
-www.
 usgo
@@ -140,16 +140,20 @@
  = %22/mm/
+api/
 members/
@@ -474,36 +474,8 @@
 de(%7B
-%22select%22: EMAIL_FIELD_NAME, 
 %22api
@@ -566,97 +566,177 @@
     
-response_data = json.loads(response.data)%0A    return response_data.get(EMAIL_FIELD_NAME, %22%22)
+try:%0A        if not response.json()%5B'success'%5D:%0A            return None%0A        return response.json()%5B'payload'%5D%5B'row'%5D%5B'email'%5D%0A    except IndexError:%0A        return None
 %0A

Fix aga membership client