Skip to content

Commit

Permalink
make calls to fasjson from python 2
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Coady <[email protected]>
  • Loading branch information
StephenCoady committed Dec 2, 2020
1 parent dad8b7b commit 22c49db
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
55 changes: 55 additions & 0 deletions fmn/fasjson_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import requests
import requests.exceptions
from requests.compat import urlencode, urljoin
from requests_gssapi import HTTPSPNEGOAuth


class Client(object):
"""
A fasjson client to make very specific requests to fasjson.
Necessary because the official fasjson-client library does not support
python3.
"""
def __init__(self, url, principal=None):
self.url = url
self.principal = principal

gssapi_auth = HTTPSPNEGOAuth(
opportunistic_auth=True, mutual_authentication="OPTIONAL"
)
self.session = requests.Session()
self.session.auth = gssapi_auth

def search(self, email):
"""
A very limited search built to only serve fmn's requirement of
finding a user based on an email.
"""
# email must be an exact match in fasjson, so we will either have
# 1 result or empty result
search_string = "search/users" + "?" + urlencode({"email": email})
endpoint = urljoin(self.url, search_string)

return self.session.get(endpoint)

def get_user(self, username):
"""
Get a specific user based on their username
"""
url_string = "users/" + username + "/"
endpoint = urljoin(self.url, url_string)

return self.session.get(endpoint)

def list_all_entities(self, ent_name):
"""
Return all entities of a certain type. In fmn's case it is users.
"""
endpoint = urljoin(self.url, ent_name + "/")

next_page_url = endpoint + "?" + urlencode({"page_number": 1})
while next_page_url:
res = self.session.get(next_page_url)
for item in res["result"]:
yield item
next_page_url = res.get("page", {}).get("next_page")
15 changes: 8 additions & 7 deletions fmn/fmn_fasshim.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import logging
import socket
import string
import requests

import fedmsg
import fedmsg.meta
import fedora.client
import fedora.client.fas2
import fasjson_client
from fasjson_client import Client
from dogpile.cache import make_region

from fmn import config
Expand All @@ -26,7 +27,7 @@

fasjson = config.app_conf['fasjson']
if fasjson.get('active'):
client = fasjson_client.Client(url=fasjson.get('url', default_url))
client = Client(url=fasjson.get('url', default_url))
else:
client = fedora.client.fas2.AccountSystem(
base_url=creds.get('base_url', default_url),
Expand All @@ -43,7 +44,7 @@ def make_fasjson_cache(**config):
global client
try:
_add_to_cache(list(client.list_all_entities("users")))
except fasjson_client.errors.APIError as e:
except requests.exceptions.RequestException as e:
log.error("Something went wrong building cache with error: %s" % e)
return

Expand Down Expand Up @@ -107,8 +108,8 @@ def update_nick(username):
try:
log.info("Downloading FASJSON cache for %s*" % username)
response = client.get_user(username=username)
_add_to_cache([response.result])
except fasjson_client.errors.APIError as e:
_add_to_cache([response.json().result])
except requests.exceptions.RequestException as e:
log.error("Something went wrong updating the cache with error: %s" % e)
else:
try:
Expand Down Expand Up @@ -144,8 +145,8 @@ def update_email(email):
try:
log.info("Downloading FASJSON cache for %s*" % email)
response = client.search(email=email)
_add_to_cache(response.result)
except fasjson_client.errors.APIError as e:
_add_to_cache(response.json()['result'])
except requests.exceptions.RequestException as e:
log.error("Something went wrong updating the cache with error: %s" % e)
else:
try:
Expand Down

0 comments on commit 22c49db

Please sign in to comment.