Skip to content

Commit

Permalink
Merge pull request #28 from HSLdevcom/request-headers
Browse files Browse the repository at this point in the history
Add support for providing headers with env
  • Loading branch information
sharhio authored May 17, 2024
2 parents d778390 + 1947a02 commit e3bec85
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
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

0 comments on commit e3bec85

Please sign in to comment.