Skip to content

Commit

Permalink
Merge pull request #247 from canonical/WD-13749-endpoint-for-returnin…
Browse files Browse the repository at this point in the history
…g-list-of-employees-for-author
  • Loading branch information
immortalcodes authored Sep 20, 2024
2 parents 8694a96 + 0ea5db0 commit b0c16c0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ OS_AUTH_URL=http://localhost:8080/auth/v1.0
OS_USERNAME=test:tester
OS_PASSWORD=testing
OS_AUTH_VERSION=1.0
DIRECTORY_API_TOKEN=ADD_DIRECTORY_API_TOKEN_HERE
6 changes: 6 additions & 0 deletions webapp/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
get_tokens,
update_asset,
update_redirect,
get_users,
)


Expand Down Expand Up @@ -170,3 +171,8 @@ def update():
view_func=delete_redirect,
methods=["DELETE"],
)
api_blueprint.add_url_rule(
"/get-users/<username>",
view_func=get_users,
methods=["GET"],
)
37 changes: 37 additions & 0 deletions webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import json
import re
import uuid
import requests
from datetime import datetime
from distutils.util import strtobool
from urllib.parse import unquote, urlparse
from os import environ

# Packages
from flask import (
Expand All @@ -20,6 +22,7 @@
# Local
from webapp.database import db_session
from webapp.decorators import token_required
from webapp.sso import login_required
from webapp.lib.file_helpers import get_mimetype, remove_filename_hash
from webapp.lib.http_helpers import set_headers_for_type
from webapp.lib.processors import ImageProcessor
Expand Down Expand Up @@ -373,3 +376,37 @@ def delete_redirect(redirect_path):
db_session.commit()

return jsonify({}), 204


@login_required
def get_users(username: str):
query = """
query($name: String!) {
employees(filter: { contains: { name: $name }}) {
id
name
email
team
department
jobTitle
}
}
"""

headers = {"Authorization": "token " + environ.get("DIRECTORY_API_TOKEN")}
response = requests.post(
"https://directory.wpe.internal/graphql/",
json={
"query": query,
"variables": {"name": username.strip()},
},
headers=headers,
verify=False,
timeout=10,
)

if response.status_code == 200:
users = response.json().get("data", {}).get("employees", [])
return jsonify(list(users))
else:
return jsonify({"error": "Failed to fetch users"}), 500

0 comments on commit b0c16c0

Please sign in to comment.