avryhof / ambient_api

Python module for accessing the Ambient Weather API
MIT License
32 stars 18 forks source link

How do I set the API key in the script? #8

Open Viss opened 4 years ago

Viss commented 4 years ago

I'd like to be able to set the API key in the script itself versus using environment variables, making it a bit more portable could you provide an example of how to do that?

avryhof commented 4 years ago

You can pass it in as a Keyword argument.

api = AmbientAPI(AMBIENT_API_KEY="your key here")

kyrlon commented 5 months ago

I ended up using a json file to keep all of my values with the example below:

contents of AUTH.json:

{
    "AMBIENT_APPLICATION_KEY":"KEY_HEREEE",
    "AMBIENT_API_KEY":"KEY HERE",
    "AMBIENT_ENDPOINT":"https://rt.ambientweather.net/v1",
    "log_level": "debug"
}

ex.py script:

from ambient_api.ambientapi import AmbientAPI
import time, json
from pathlib import Path
import inspect

CURRENT_FILE_PATH = Path(inspect.getfile(lambda: None)).parent

LOG_FILE = CURRENT_FILE_PATH / "data.log"

with open(CURRENT_FILE_PATH / "AUTH.json") as fd:
    auth = json.load(fd)

api = AmbientAPI(**auth)

devices = api.get_devices()

device = devices[0]
time.sleep(1)
print(device.get_data())