Equivalent of requests.get(url, auth=(username, password))?

I want to somehow use a web API to get the recent precipitations, to automate garden watering in the summer. OpenWeatherMap does not have that unless you purchase an expensive package, WeatherStack is limited to 100 queries per month (basically useless), but I found Meteomatics which provide that on a free plan.

The catch is they don't have an API key type of authentication, but rather a username/password. They give a coding example in Python on their site:

which looks like:

url = "https://api.meteomatics.com/{date}/{parameters}/{location}"
username = “your_username”
password = “your_password”

response = requests.get(url, auth=(username, password))

data = response.json() # Converts JSON response to a Python dictionary

Any pointer on how to translate the requests.get(url, auth=(username, password)) in Groovy ?

Thanks

Look at asynchttpGet:

Pretty sure OP is asking about how to handle the authentication, not how to do a web request.

1 Like

The provide a Java example here: https://github.com/meteomatics/java-connector-api/blob/master/MeteomaticsExample.java

That's pretty much what you'd do in Groovy too.

1 Like

Indeed, the authentication part is what throws me off... It would be so much simpler with just an API key...

It's not that bad really.

String getAuthorizationB64() {
  return "${settings.username}:${settings.password}".bytes.encodeBase64()
}

You can use a helper method like that. Then call that when building your params Map for your asyncHttpGet:

  Map params = [
    uri: "https://api.meteomatics.com/${date}/${parameters}/${location}",
    headers: [authorization: "Basic ${getAuthorizationB64()}"],
    requestContentType: 'application/json',
    contentType: 'application/json'
    ]
1 Like

Thanks. That should be enough to get me started... maybe I'll be releasing a Hubitat driver for MeteoMatics API soon then :slight_smile:

1 Like

I got some time today, so I wrote the driver...

1 Like