Repository: internetarchive/openlibrary

Issue:
Organizing presentation for 2021 review



Discussion:
https://docs.google.com/document/d/1RPMdHSnaGAblp6W5adCJkTv9IzRY5kHtd9vHJ4ppo44/edit
@mekarpeles presented this at last call; ^^ Those are the notes

Resolving patch:
diff --git a/openlibrary/core/civicrm.py b/openlibrary/core/civicrm.py
--- a/openlibrary/core/civicrm.py
+++ b/openlibrary/core/civicrm.py
@@ -1,4 +1,6 @@
 import json
+import logging
+
 import requests
 
 from openlibrary.core import lending 
@@ -6,6 +8,7 @@
 CIVI_ISBN = 'custom_52'
 CIVI_USERNAME = 'custom_51'
 CIVI_CONTEXT = 'custom_53'
+logger = logging.getLogger("openlibrary.civicrm")
 
 
 def get_contact(username=None, contact_id=None):
@@ -25,14 +28,20 @@ def get_contact(username=None, contact_id=None):
     if contact_id:
         data["json"]["contact_id"] = contact_id
     data["json"] = json.dumps(data["json"])  # flatten the json field as a string
-    r = requests.get(
-        lending.config_ia_civicrm_api.get("url", ""),
-        params=data,
-        headers={
-            "Authorization": "Basic %s" % lending.config_ia_civicrm_api.get("auth", "")
-        })
-    contacts = r.status_code == 200 and r.json().get("values", None)
-    return contacts and contacts[0]
+    try:
+        r = requests.get(
+            lending.config_ia_civicrm_api.get("url", ""),
+            params=data,
+            timeout=3,
+            headers=dict(
+                Authorization=f"Basic {lending.config_ia_civicrm_api.get('auth', '')}"
+            ))
+        contacts = r.status_code == 200 and r.json().get("values", None)
+        return contacts and contacts[0]
+    except requests.Timeout:
+        logger.error('Timeout accessing CiviCRM')
+
+    return None
 
 
 def get_contact_id_by_username(username):
@@ -68,18 +77,23 @@ def get_sponsorships_by_contact_id(contact_id=None, isbn=None):
     if isbn:
         data["json"][CIVI_ISBN] = isbn
     data["json"] = json.dumps(data["json"])  # flatten the json field as a string
-    r = requests.get(
-        lending.config_ia_civicrm_api.get("url", ""),
-        params=data,
-        headers={
-            "Authorization": "Basic %s" % lending.config_ia_civicrm_api.get("auth", "")
-        })
-    txs = r.json().get("values")
-    return [{
-        "isbn": t.pop(CIVI_ISBN),
-        "context": t.pop(CIVI_CONTEXT),
-        "receive_date": t.pop("receive_date"),
-        "total_amount": t.pop("total_amount"),
-        "contact_id": t.pop("contact_id"),
-        "contribution_status": t.pop("contribution_status")
-    } for t in txs]
+    try:
+        txs = requests.get(
+            lending.config_ia_civicrm_api.get("url", ""),
+            timeout=3,
+            params=data,
+            headers=dict(
+                Authorization=f"Basic {lending.config_ia_civicrm_api.get('auth', '')}"
+            )).json().get("values")
+        return [{
+            "isbn": t.pop(CIVI_ISBN),
+            "context": t.pop(CIVI_CONTEXT),
+            "receive_date": t.pop("receive_date"),
+            "total_amount": t.pop("total_amount"),
+            "contact_id": t.pop("contact_id"),
+            "contribution_status": t.pop("contribution_status")
+        } for t in txs]
+    except requests.Timeout:
+        logger.error('Timeout accessing CiviCRM')
+
+    return []


Test patch: