Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Update graphql python client #2372

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions chroma_core/lib/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,57 @@
this.API_CRED = None


class GraphQlQueryException(Exception):
pass


def get_api_session_request():
if not this.API_CRED:
cursor = connection.cursor()
cursor.execute("SELECT * from api_key()")
this.API_CRED = cursor.fetchone()
cursor.close()

s = requests.Session()
s.headers.update({"AUTHORIZATION": "ApiKey {}:{}".format(this.API_CRED[0], this.API_CRED[1])})

return s


def graphql_query(query):
def graphql_query(query, variables={}):
"""
Issue a GraphQL query
"""
s = get_api_session_request()
path = os.path.join(settings.IML_API_PROXY_PASS, "graphql")
q = {"query": query}
q = {"query": query, "variables": variables}
res = s.post(url=path, json=q)
return res.json()["data"]

body = res.json()

errors = body.get("errors")

if errors is not None:
raise GraphQlQueryException(errors)

return body.get("data")


def get_targets(**kwargs):
query = """
query Targets($limit: Int, $offset: Int, $dir: SortDir, $fsname: String, $exclude_unmounted: Boolean) {
targets(limit: $limit, offset: $offset, dir: $dir, fsName: $fsname, excludeUnmounted: $exclude_unmounted) {
id
state
name
dev_path: devPath
active_host_id: activeHostId
host_ids: hostIds
filesystems
uuid
mount_path: mountPath
}
}
"""

return graphql_query(query, variables=kwargs)["targets"]