Skip to content

Commit

Permalink
Merge pull request #51 from chrisramsay/feature/add-headers-to-http
Browse files Browse the repository at this point in the history
Feature/add headers to http
  • Loading branch information
jim-easterbrook committed Aug 18, 2017
2 parents 081df44 + 677ac54 commit 824f629
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/contributors/contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Simon Josefsson simon@josefsson.org
Matthew Hilton matthilton2005@gmail.com
Sabine Tobolka oe1yvw@gmail.com
Markus Birth markus@birth-online.de
Chris Ramsay chris@ramsay-family.net

Translators
-----------
Expand Down
22 changes: 21 additions & 1 deletion src/doc/guides/integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
How to integrate pywws with various weather services
How to integrate pywws with various weather services
====================================================

This guide gives brief instructions on how to use pywws with some other weather services and software.
Expand Down Expand Up @@ -280,3 +280,23 @@ wetter.com

[live]
services = ['wetterarchivde', 'underground_rf']

Custom Request Headers
----------------------

The :py:mod:`pywws.toservice` module does support the injection of one or more
custom request headers for special cases where you want to integrate with a
service that, for example, requires you to pass an authentication key header
along with each request, such as ``x-api-key``.

These headers can be added to your ``a_service.ini`` file in the format of key
value pairs::

[config]
url = https://my-aws-api-gw.execute-api.eu-west-1.amazonaws.com/test/station
catchup = 100
interval = 0
use get = True
result = []
auth_type = None
http_headers = [('x-api-key', 'my-api-key'), ('x-some-header', 'value')]
10 changes: 10 additions & 0 deletions src/pywws/services/aws_api_gw_service.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[config]
url = https://my-aws-api-gw.execute-api.eu-west-1.amazonaws.com/test/station
catchup = 100
interval = 0
use get = True
result = []
auth_type = None
http_headers = [('x-api-key', 'my-api-key')]

[fixed]
35 changes: 35 additions & 0 deletions src/pywws/services/aws_api_gw_service_template_1080.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! pywws - Python software for USB Wireless Weather Stations #
#! http://github.com/jim-easterbrook/pywws #
#! Copyright (C) 2008-13 Jim Easterbrook jim@jim-easterbrook.me.uk #
#! #
#! This program is free software; you can redistribute it and/or #
#! modify it under the terms of the GNU General Public License #
#! as published by the Free Software Foundation; either version 2 #
#! of the License, or (at your option) any later version. #
#! #
#! This program is distributed in the hope that it will be useful, #
#! but WITHOUT ANY WARRANTY; without even the implied warranty of #
#! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
#! GNU General Public License for more details. #
#! #
#! You should have received a copy of the GNU General Public License #
#! along with this program; if not, write to the Free Software #
#! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
#live#
{
#hum_in "'hum_in' : '%.d',"#
#temp_in "'temp_in' : '%.1f',"#
#hum_out "'hum_out' : '%.d',"#
#temp_out "'temp_out' : '%.1f',"#
#calc "dew_point(data['temp_out'], data['hum_out'])" "'temp_dewpt' : '%.1f',"#
#rel_pressure "'abs_pressure' : '%.4f',"#
#wind_ave "'wind_ave' : '%.2f',"#
#wind_gust "'wind_gust' : '%.2f',"#
#wind_dir "'wind_dir' : '%.0f'," "" "winddir_degrees(x)"#
#calc "rain_hour(data)" "'rain' : '%g',"#
#calc "rain_day(data)" "'rain_day' : '%g',"#
#calc "wind_chill(data['temp_out'], data['wind_ave'])" "'wind_chill' : '%.1f',"#
#calc "apparent_temp(data['temp_out'], data['hum_out'], data['wind_ave'])" "'temp_apprt' : '%.1f',"#
#idx "'tdate' : '%Y-%m-%d',"#
#idx "'ttime' : '%H:%M:%S',"#
}
6 changes: 6 additions & 0 deletions src/pywws/toservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def __init__(self, params, status, calib_data, service_name):
self.parent = config_section
self.old_response = None
self.old_ex = None
self.http_headers = None
# set default socket timeout, so urlopen calls don't hang forever
if eval(self.params.get('config', 'asynchronous', 'False')):
socket.setdefaulttimeout(60)
Expand Down Expand Up @@ -269,6 +270,8 @@ def __init__(self, params, status, calib_data, service_name):
self.interval = eval(service_params.get('config', 'interval'))
self.interval = max(self.interval, 40)
self.interval = timedelta(seconds=self.interval)
if service_params.has_option('config', 'http_headers'):
self.http_headers = eval(service_params.get('config', 'http_headers'))
# move 'last update' from params to status
last_update = self.params.get_datetime(self.service_name, 'last update')
if last_update:
Expand Down Expand Up @@ -458,6 +461,9 @@ def http_send_data(self, timestamp, prepared_data, ignore_last_update=False):
request = urllib2.Request(self.server, coded_data.encode('ASCII'))
if self.auth_type == 'basic':
request.add_header('Authorization', self.auth)
if self.http_headers is not None:
for header in self.http_headers:
request.add_header(header[0], header[1])
rsp = urllib2.urlopen(request)
response = rsp.readlines()
rsp.close()
Expand Down

0 comments on commit 824f629

Please sign in to comment.