I want to use Prefect CLI to get and parse list of...
# ask-community
c
I want to use Prefect CLI to get and parse list of deployments so I can get the deployment ids. I am running prefect under windows. However I cannot script this: • Prefect CLI scripting not supported (login tooling is broken) • Using Prefect CLI tool returns truncated ID strings
Copy code
| <flow-name>/<deployment-name>                  | asdfasdf-1111-xxxà |
Can someone please advise? this is extremely frustrating
Note that even grepping for deployment names will fail in a similar fashion if the names are long enough.
b
Hello Cormac 👋 I'm not 100% sure how to achieve this via the CLI. Out of curiosity, would you be opposed to using the Python SDK or Rest API instead? For the python SDK, I imagine you could use read_deployments to achieve this.
c
@Bianca Hoch thanks for getting in touch. Last time I tried to use the API (a few months ago) I could not even get it to log in.
b
Hmm..and this was after running
prefect cloud login -k <insert-api-key-here>
?
Or where you using the REST api and running something similar to this? (the example below queries for task runs, so it's not 1:1)
Copy code
import requests
import json

base_url="<https://api.prefect.cloud/api/accounts/[insert-your-account-id-here]/workspaces/[insert-your-workspace-id-here]>"

url=f"{base_url}/task_runs/filter"
apikey = "insert-your-api-key-here"
print(url)

headers= {
"Content-Type": "application/json",
"Authorization": f"bearer {apikey}",
}

json =  {
    "task_runs": {
        "start_time" : {
            "before_": "2023-04-27T10:15:22Z",
            "after_": "2023-04-16T00:15:22Z"
        }
    }
}

try:
    response = requests.post(url=url, headers=headers, json=json)
except Exception as e:
    print(e)

#print (response.json())
results = response.json()
print(results[1])