haystack/.github/utils/readme_api.py
Silvano Cerza 145dfce3aa
ci: Slightly rework docs release process (#8363)
* Slightly rework docs release process

* Apply suggestions from code review

Co-authored-by: Madeesh Kannan <shadeMe@users.noreply.github.com>

---------

Co-authored-by: Madeesh Kannan <shadeMe@users.noreply.github.com>
2024-09-16 09:28:40 +00:00

56 lines
1.7 KiB
Python

import os
import base64
import requests
class ReadmeAuth(requests.auth.AuthBase):
def __call__(self, r: requests.Request):
r.headers["authorization"] = f"Basic {readme_token()}"
return r
def readme_token():
api_key = os.getenv("RDME_API_KEY", None)
if not api_key:
raise Exception("RDME_API_KEY env var is not set")
api_key = f"{api_key}:"
return base64.b64encode(api_key.encode("utf-8")).decode("utf-8")
def get_versions():
"""
Return all versions currently published in Readme.io.
"""
url = "https://dash.readme.com/api/v1/version"
res = requests.get(url, auth=ReadmeAuth(), timeout=30)
res.raise_for_status()
return [v["version"] for v in res.json()]
def create_new_unstable(current: str, new: str):
"""
Create new version by copying current.
:param current: Existing current unstable version
:param new: Non existing new unstable version
"""
url = "https://dash.readme.com/api/v1/version/"
payload = {"is_beta": False, "version": new, "from": current, "is_hidden": False, "is_stable": False}
res = requests.post(url, json=payload, auth=ReadmeAuth(), timeout=30)
res.raise_for_status()
def promote_unstable_to_stable(unstable: str, stable: str):
"""
Rename the current unstable to stable and set it as stable.
:param unstable: Existing unstable version
:param stable: Non existing new stable version
"""
url = f"https://dash.readme.com/api/v1/version/{unstable}"
payload = {"is_beta": False, "version": stable, "from": unstable, "is_hidden": False, "is_stable": True}
res = requests.put(url, json=payload, auth=ReadmeAuth(), timeout=30)
res.raise_for_status()