forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomains.py
More file actions
110 lines (86 loc) · 3.29 KB
/
domains.py
File metadata and controls
110 lines (86 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""This module provides functions for getting lists of domains you have ownership of"""
import json
from slugify import slugify
from core import execute_query
from formatting import format_all_domains, format_acronym_domains, format_name_domains
from queries import ALL_DOMAINS_QUERY, DOMAINS_BY_SLUG
def get_all_domains(client):
"""Get lists of all domains you have ownership of, with org as key
:param Client client: a gql Client object
:return: formatted JSON data with all organizations and their domains
:rtype: str
:Example:
>>> import tracker_client.client as tracker_client
>>> client = tracker_client.create_client(auth_token=tracker_client.get_auth_token())
>>> print(tracker_client.get_all_domains(client))
{
"FOO": [
"foo.bar",
"foo.bar.baz"
],
"BAR": [
"fizz.buzz",
"buzz.bang",
"ab.cd.ef",
]
}
"""
result = execute_query(client, ALL_DOMAINS_QUERY)
# If there is an error the result contains the key "error"
if "error" not in result:
result = format_all_domains(result)
return json.dumps(result, indent=4)
def get_domains_by_acronym(client, acronym):
"""Get the domains belonging to the organization identified by acronym
:param Client client: a gql Client object
:param str acronym: an acronym referring to an organization
:return: formatted JSON data with an organization's domains
:rtype: str
:Example:
>>> import tracker_client.client as tracker_client
>>> client = tracker_client.create_client(auth_token=tracker_client.get_auth_token())
>>> print(tracker_client.get_domains_by_acronym(client, "foo"))
{
"FOO": [
"foo.bar",
"foo.bar.baz"
]
}
"""
# API doesn't allow query by acronym so we filter the get_all_domains result
result = execute_query(client, ALL_DOMAINS_QUERY)
if "error" not in result:
# Since the server doesn't see the acronym we check if it's in the result
# and simulate the server error response if it's not there
try:
result = format_acronym_domains(result, acronym)
except KeyError:
result = {
"error": {
"message": "No organization with the provided acronym could be found."
}
}
return json.dumps(result, indent=4)
def get_domains_by_name(client, name):
"""Get the domains belonging to the organization identified by name
:param Client client: a gql Client object
:param str name: the full name of an organization
:return: formatted JSON data with an organization's domains
:rtype: str
:Example:
>>> import tracker_client.client as tracker_client
>>> client = tracker_client.create_client(auth_token=tracker_client.get_auth_token())
>>> print(tracker_client.get_domains_by_name(client, "foo bar"))
{
"FOO": [
"foo.bar",
"foo.bar.baz"
]
}
"""
slugified_name = slugify(name) # API expects a slugified string for name
params = {"orgSlug": slugified_name}
result = execute_query(client, DOMAINS_BY_SLUG, params)
if "error" not in result:
result = format_name_domains(result)
return json.dumps(result, indent=4)