'How to get Nomad precompiled binary using python requests client?

I'm trying to get the latest version of Nomad from official Github hashicorp repo. (written the explicit url for the version assets for the sake of the question)

import requests

response = requests.get("https://api.github.com/repos/hashicorp/nomad/releases/latest")
release_url = response.json()["url"]
print(release_url)

asset_response = requests.get("https://api.github.com/repos/hashicorp/nomad/releases/assets/57485344", headers={"Accept": "application/octet-stream; application/vnd.github.v3+json"})
print(asset_response.json())

When running the code I'm getting error Not Found: {'message': 'Not Found', 'documentation_url': 'https://docs.github.com/rest/reference/repos#get-a-release-asset'}

I've also tried setting the Accept header to "application/octet-stream" as it states in the docs and I'm getting the same exact error.

In addition, tried using the browser with the same url and I'm getting the same error, so I'm guessing the issue is not likely to be with python requests.

Am I doing something wrong here? Or is there another way to get the latest precompiled binary of the latest version using the GitHub API?



Solution 1:[1]

Hashicorp doesn't publish the precompiled binaries on github. It releases them on it's own cdn https://releases.hashicorp.com/nomad/

To download the latest stable version dynamically from python you can use a combination of both github(to get latest version) and Hashicorp cdn(to download the binary).

import requests
from urllib.request import urlretrieve
from os.path import basename

response = requests.get("https://api.github.com/repos/hashicorp/nomad/releases/latest")
latest_stable_version = response.json()["name"][1:]
print(latest_stable_version)

release_url = "https://releases.hashicorp.com/nomad/{0}/nomad_{0}_linux_amd64.zip".format(latest_stable_version)
file_name = basename(release_url)
print("Downloading", file_name)
urlretrieve(release_url, file_name)

In the above example, I am downloading the binary for linux OS and amd64 CPU type. You can find all precompiled OS and CPU type here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sanket Dalvi