Read the SNCF authentication key from an environment variable

This commit is contained in:
Dimitri Lozeve 2022-09-09 22:45:13 +02:00
parent efb0c4d1fe
commit ab4073f31f
2 changed files with 14 additions and 5 deletions

View file

@ -5,11 +5,12 @@
Get an access to the [[https://www.digital.sncf.com/startup/api][SNCF API]]. By creating an account, you should Get an access to the [[https://www.digital.sncf.com/startup/api][SNCF API]]. By creating an account, you should
receive an email with your authentication key. receive an email with your authentication key.
You can now call the program with the authentication key as the first The program reads the authentication key from the =SNCF_AUTH_KEY=
argument. environment variable.
#+begin_src sh #+begin_src sh
$ sncf <sncf-key> $ export SNCF_AUTH_KEY=<sncf-key>
$ sncf
┌────────────┬────────────────────────────────┬───────────────┐ ┌────────────┬────────────────────────────────┬───────────────┐
│ Réseau │ Direction │ Heure │ │ Réseau │ Direction │ Heure │
├────────────┼────────────────────────────────┼───────────────┤ ├────────────┼────────────────────────────────┼───────────────┤

12
sncf.ss
View file

@ -15,14 +15,22 @@
(def +sncf-url+ "https://api.sncf.com/v1/coverage/sncf/stop_areas/stop_area:SNCF:87415604/departures") (def +sncf-url+ "https://api.sncf.com/v1/coverage/sncf/stop_areas/stop_area:SNCF:87415604/departures")
(def (main . args) (def (main . args)
(def gopt (getopt (argument 'sncf-key help: "SNCF API authentication key.") (def gopt (getopt (flag 'help "-h" "--help" help: "Display this help.")
(option 'mattermost-url "--mattermost-url" help: "Mattermost incoming webhook URL.") (option 'mattermost-url "--mattermost-url" help: "Mattermost incoming webhook URL.")
(option 'mattermost-channel "--channel" help: "Mattermost channel."))) (option 'mattermost-channel "--channel" help: "Mattermost channel.")))
(try (try
(let* ((options (getopt-parse gopt args)) (let* ((options (getopt-parse gopt args))
(sncf-key (hash-ref options 'sncf-key)) (help (hash-ref options 'help #f))
(mattermost-url (hash-ref options 'mattermost-url)) (mattermost-url (hash-ref options 'mattermost-url))
(mattermost-channel (hash-ref options 'mattermost-channel))) (mattermost-channel (hash-ref options 'mattermost-channel)))
(when help
(getopt-display-help gopt "sncf")
(exit 0))
(def sncf-key (getenv "SNCF_AUTH_KEY" #f))
(unless sncf-key
(display "No SNCF API authentication key found. Set the SNCF_AUTH_KEY environment variable.\n"
(current-error-port))
(exit 1))
(let-values (((departures disruptions) (get-departures sncf-key))) (let-values (((departures disruptions) (get-departures sncf-key)))
(display-departures-table departures) (display-departures-table departures)
(display-disruptions disruptions) (display-disruptions disruptions)