Skip to content

Commit

Permalink
replace backfillMinutes if it already exists in configured url
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Unsworth committed Oct 6, 2016
1 parent 7db5d3f commit eee3b8e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
48 changes: 37 additions & 11 deletions gnippy/powertrackclient.py
Expand Up @@ -4,11 +4,32 @@
import sys
import threading

try:
import urlparse
from urllib import urlencode
except: # For Python 3
import urllib.parse as urlparse
from urllib.parse import urlencode

import requests

from gnippy import config


def append_backfill_to_url(url, backfill_minutes):
parsed = list(urlparse.urlparse(url))

params = {'backfillMinutes': backfill_minutes}

qs = dict(urlparse.parse_qsl(parsed[4]))

qs.update(params)

parsed[4] = urlencode(qs)

return urlparse.urlunparse(parsed)


class PowerTrackClient:
"""
PowerTrackClient allows you to connect to the GNIP
Expand All @@ -27,6 +48,19 @@ def __init__(self, callback, exception_callback=None, **kwargs):

def connect(self, backfill_minutes=None):

connection_url = self.get_connection_url(backfill_minutes)

self.worker = Worker(
url=connection_url,
auth=self.auth,
callback=self.callback,
exception_callback=self.exception_callback)

self.worker.setDaemon(True)

self.worker.start()

def get_connection_url(self, backfill_minutes=None):
connection_url = self.url

if backfill_minutes:
Expand All @@ -38,18 +72,10 @@ def connect(self, backfill_minutes=None):
"backfill_minutes should be 5 or less: {0}".format(
backfill_minutes)

connection_url = "{0}?backfillMinutes={1}".format(
self.url, backfill_minutes)

self.worker = Worker(
url=connection_url,
auth=self.auth,
callback=self.callback,
exception_callback=self.exception_callback)

self.worker.setDaemon(True)
connection_url = append_backfill_to_url(
connection_url, backfill_minutes)

self.worker.start()
return connection_url

def connected(self):

Expand Down
25 changes: 22 additions & 3 deletions gnippy/test/test_powertrackclient.py
Expand Up @@ -5,10 +5,8 @@

import mock

<<<<<<< 5aad86859b37e17aa6cd8c972150886fdc989cbb
=======
from gnippy.powertrackclient import append_backfill_to_url

>>>>>>> added option to specify backfill minutes on the connect call
try:
import unittest2 as unittest
except ImportError:
Expand Down Expand Up @@ -222,3 +220,24 @@ def test_backfill_value_greater_than_five_raises_exception(self):
client = PowerTrackClient(_dummy_callback, config_file_path=config_file)

self.assertRaises(AssertionError, client.connect, backfill_minutes)

def test_append_backfill_to_url_appends_backfillMinutes_param(self):

base_url = "http://www.twitter.com"
backfill_minutes = 2
expected_url = "http://www.twitter.com?backfillMinutes=2"

returned_value = append_backfill_to_url(base_url, backfill_minutes)

self.assertEqual(returned_value, expected_url)

def test_append_backfill_to_url_replaces_existing_backfillMinutes_param(
self):

base_url = "http://www.twitter.com?backfillMinutes=5"
backfill_minutes = 1
expected_url = "http://www.twitter.com?backfillMinutes=1"

returned_value = append_backfill_to_url(base_url, backfill_minutes)

self.assertEqual(returned_value, expected_url)

0 comments on commit eee3b8e

Please sign in to comment.