Greetings Everyone, I'm attempting to use `prefect...
# ask-community
f
Greetings Everyone, I'm attempting to use
prefect.Client
to query the GraphQL endpoint, however, I am getting errors that have me stumped. The relevant code:
Copy code
import prefect
import pandas as pd

client = prefect.Client()

client.graphql("""
    query {
        agent{
          flow_runs(limit:5, where:{state:{_eq: "Success"}start_time:{_gt:"2022-02-14"}}){
            start_time
            end_time
            state
            flow{
              name
              id
              tasks_aggregate{
                aggregate{
                  count
                }
              }
            }
      		
          }
          
        }

    } """)
The errors:
Copy code
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/prefect/client/client.py", line 465, in _request
    json_resp = response.json()
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/workspaces/prefect/ETL/CDNY/GraphQL.py", line 7, in <module>
    client.graphql("""
  File "/usr/local/lib/python3.8/site-packages/prefect/client/client.py", line 298, in graphql
    result = <http://self.post|self.post>(
  File "/usr/local/lib/python3.8/site-packages/prefect/client/client.py", line 213, in post
    response = self._request(
  File "/usr/local/lib/python3.8/site-packages/prefect/client/client.py", line 468, in _request
    raise ClientError(
prefect.utilities.exceptions.ClientError: Malformed response received from Cloud - please ensure that you have an API token properly configured.
We're using Prefect core as our backend at the moment and the documentation for this is sparse when I google. Thanks
k
Hey Fredrick, could you move the code and traceback to the thread when you get the chance to keep the main channel cleaner? This normally arises when you are not authenticated. How did you authenticate?
f
Hey @Kevin Kho sorry about that. I didn't authenticate, just used an old tab... What do you need me to do about moving the text?
k
No worries! Here to the thread is fine. You need login locally with
prefect auth login --key API_KEY
to use the Client like this
f
Oh G_d, do I have to generate a key ???
k
Yeah so that you can access the data attached to your account
Ah well, are you on Server or Cloud?
f
Server
We're moving to the Hybrid model soon
k
You shouldn’t need an API_KEY. You need to just point to the Server with
prefect backend server
and then define your endpoint in the
config.toml
? or with the env var
PREFECT___SERVER___ENDPOINT
f
We use config.toml but I'm developing from a devcontainer, will that affect this as well?
k
Yes if the endpoint is
localhost
, no if the endpoint is remote
some_ip_address:4200
and your container has internet connection?
Well have you been able to register flows? Cuz that hits the same API anyway so if that works, you can do
client.graphql()
as well
f
I ran :
prefect backend server
and modified my config.toml for the server section like so:
#### Server Setup
[server]
host = "<http://172.18.1.5>"
endpoint = "<http://172.18.1.5:4200>"
backend = "server"
and the error I'm receiving now:
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='172.18.1.5', port=4200): Max retries exceeded with url: /graphql (Caused by ConnectTimeoutError
k
You are still inside a container? It looks like it just can’t connect. Is it the same if you do something like starting an agent (
prefect agent local start
)?
f
I'll try it...
k
It also might be the server setup though? How did you start your server and have you connected before successfully? Do you see the UI at http://172.18.1.5:8080 ?
f
Yes!! I am now running two(2) agents. How do I get the query results back from the call to
client
?
k
It should return a dictionary? you can print it to see the shape
f
Thanks for you help, @Kevin Kho, I will attempt to not bother you for awhile 😇
k
Of course!
s
Hi @Frederick Thomas were you able to solve this issue? I have the same issue
k
Are you on Server or Cloud?
f
Hi @Sagi Timinsky!! I was able to remedy the situation and I am using Server