chiark / gitweb /
Cache the auto-discovery values from Exchange
authorMatthew Vernon <mv3@sanger.ac.uk>
Fri, 5 Oct 2018 13:51:51 +0000 (14:51 +0100)
committerMatthew Vernon <mv3@sanger.ac.uk>
Fri, 5 Oct 2018 13:51:51 +0000 (14:51 +0100)
This saves us a bit of time.

gooswapper.py

index 274cd13cd47671fe3633fbf417a5af7c01dc233c..d01d2b38a2e8daaa2ddeaaee0f6150b17b8a3363 100644 (file)
@@ -67,13 +67,38 @@ def get_ex_cred(username="SANGER\mv3",password=None):
         password = getpass.getpass(prompt="Password for user %s: " % username)
     return exchangelib.ServiceAccount(username,password)
 
-def ex_login(emailaddr,autodiscover=True):
+def ex_login(emailaddr,ad_cache_path=None):
     global exchange_credential
+    autodiscover = True
     if exchange_credential is None:
         exchange_credential = get_ex_cred()
-    return exchangelib.Account(emailaddr,
-                               credentials = exchange_credential,
-                               autodiscover = autodiscover)
+    if ad_cache_path is not None:
+        try:
+            with open(ad_cache_path,"rb") as f:
+                url,auth_type = pickle.load(f)
+                autodiscover = False
+        except FileNotFoundError:
+            pass
+
+    if autodiscover:
+        ex_ac = exchangelib.Account(emailaddr,
+                                    credentials = exchange_credential,
+                                    autodiscover = autodiscover)
+        if ad_cache_path is not None:
+            cache=(ex_ac.protocol.service_endpoint,
+                   ex_ac.protocol.auth_type)
+            with open(ad_cache_path,"wb") as f:
+                pickle.dump(cache,f)
+    else:
+        ex_conf = exchangelib.Configuration(service_endpoint=url,
+                                            credentials=exchange_credential,
+                                            auth_type=auth_type)
+        ex_ac = exchangelib.Account(emailaddr,
+                                    config=ex_conf,
+                                    autodiscover=False,
+                                    access_type=exchangelib.DELEGATE)
+
+    return ex_ac
 
 def get_ex_events(calendar):
     ans={}
@@ -166,7 +191,7 @@ def main():
     except FileNotFoundError:
         cache = None
 
-    ex_account = ex_login("mv3@sanger.ac.uk")
+    ex_account = ex_login("mv3@sanger.ac.uk",".gooswapper_exch_conf.dat")
     current = get_ex_events(ex_account.calendar)
 
     gcal_account = gcal_login()