Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for providing headers with env #28

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ You need to configure at least the following env variables that are marked as ma
* (mandatory) "FEED_TYPE" which type of a GTFS RT data is provided (for example vp for vehicle position), used in MQTT topic
* (mandatory) "FEED_NAME" name for the data feed, used in MQTT topic
* (mandatory) "FEED_URL" URL for the HTTP(S) GTFS RT data source
* (optional) "FEED_HEADERS" headers for requesting GTFS RT feed
* (mandatory as of 4.3.2023 if using Digitransit API) "AUTHENTICATION_HEADER" Authentication header name
* (mandatory as of 4.3.2023 if using Digitransit API) "AUTHENTICATION_TOKEN" Authentication header secret
* (optional) "USERNAME" username for publishing to a MQTT broker
Expand Down
5 changes: 4 additions & 1 deletion gtfsrthttp2mqtt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
import os
import json
from threading import Event, Thread

import paho.mqtt.client as mqtt
Expand All @@ -25,7 +26,7 @@ def loop():


class GTFSRTHTTP2MQTTTransformer:
def __init__(self, mqttConnect, mqttCredentials, baseMqttTopic, gtfsrtFeedURL, feedName):
def __init__(self, mqttConnect, mqttCredentials, baseMqttTopic, gtfsrtFeedURL, gtfsrtFeedHeaders, feedName):
self.mqttConnect = mqttConnect
self.mqttCredentials = mqttCredentials
self.baseMqttTopic = baseMqttTopic
Expand All @@ -36,6 +37,7 @@ def __init__(self, mqttConnect, mqttCredentials, baseMqttTopic, gtfsrtFeedURL, f
retry = Retry(connect=60, backoff_factor=1.5)
adapter = HTTPAdapter(max_retries=retry)
self.session.mount(gtfsrtFeedURL, adapter)
self.session.headers.update(json.loads(gtfsrtFeedHeaders))
self.OTPData = None


Expand Down Expand Up @@ -168,6 +170,7 @@ def doOTPPolling(self):
{'username': os.environ['USERNAME'], 'password': os.environ['PASSWORD']},
'/gtfsrt/{0}'.format(os.environ['FEED_TYPE']),
os.environ['FEED_URL'],
os.environ.get('FEED_HEADERS', '{}'),
os.environ['FEED_NAME']
)

Expand Down