1- import json
21import requests
2+ import pandas as pd
33from pathlib import Path
4+ from lib .secrets import secrets
5+ import lib .downloading as dl
6+ from lib .progressBar import ProgressBar
7+ import logging
48import lib .dataframes as dff
5- import pandas as pd
69
7- def collect (outputDir : Path , profile : str , tokenFilePath : Path ) -> None :
8- with open (tokenFilePath ) as fp :
9- token = json .load (fp )
10+ def collect (outputDir : Path , profile : str ) -> None :
11+ session = requests .session ()
12+
13+ response = session .post (
14+ "https://auth.ala.org.au/cas/oidc/oidcAccessToken" ,
15+ headers = {"Content-Type" : "application/x-www-form-urlencoded" },
16+ data = "grant_type=client_credentials&scope=openid email profile roles" ,
17+ auth = dl .buildAuth (secrets .ala .id , secrets .ala .secret )
18+ )
19+
20+ accessToken = response .json ()["access_token" ]
1021
11- bearerToken = token ["access_token" ]
1222 baseURL = "https://api.ala.org.au/profiles"
1323 endpoint = f"/api/opus/{ profile } /profile?pageSize=1000"
14- response = requests .get (baseURL + endpoint , headers = {"Authorization" : f"Bearer { bearerToken } " })
24+ response = requests .get (baseURL + endpoint , headers = {"Authorization" : f"Bearer { accessToken } " })
1525 data = response .json ()
1626
1727 if "message" in data and "not authorized" in data ["message" ]:
18- print ("Failed to authorize, please make sure bearer token is valid." )
28+ logging . error ("Failed to authorize, please make sure bearer token is valid." )
1929 return
2030
21- print (f"Accessing profile: { profile } " )
31+ logging .info (f"Accessing profile: { profile } " )
32+
33+ dataLength = len (data )
34+ logging .info (f"Found { dataLength } records" )
2235
36+ progress = ProgressBar (dataLength )
2337 records = []
24- for idx , entry in enumerate ( data , start = 1 ) :
38+ for entry in data :
2539 uuid = entry ["uuid" ]
26- print (f"At record: { idx } " , end = "\r " )
27-
28- response = requests .get (baseURL + f"/api/opus/{ profile } /profile/{ uuid } " , headers = {"Authorization" : f"Bearer { bearerToken } " })
40+ response = requests .get (baseURL + f"/api/opus/{ profile } /profile/{ uuid } " , headers = {"Authorization" : f"Bearer { accessToken } " })
2941 records .append (response .json ())
30- print ()
42+ progress . update ()
3143
3244 df = pd .DataFrame .from_records (records )
3345 df = dff .removeSpaces (df )
34- df .to_csv (outputDir / f"{ profile } .csv" , index = False )
46+ df .to_csv (outputDir / f"{ profile } .csv" , index = False )
0 commit comments