Repository: abh1nav/gnippy

Issue:
Rules URL is incorrectly generated if endpoint URL has parameters
This does not work if `conf['url']` has params.

```
rules_url = _generate_rules_url(conf['url']) + "?_method=delete"
```

Example generated URL:

```
https://api.gnip.com:443/accounts/XXX/publishers/twitter/streams/track/prod/rules.json?client=2?_method=delete
```

Discussion:

Patch:
diff --git a/gnippy/rules.py b/gnippy/rules.py
index 0309b5d..f5f1d04 100644
--- a/gnippy/rules.py
+++ b/gnippy/rules.py
@@ -2,6 +2,11 @@
 
 import json
 
+try:
+    from urllib.parse import urlparse
+except:
+    from urlparse import urlparse
+
 import requests
 from six import string_types
 
@@ -82,6 +87,17 @@ def _post(conf, built_rules):
         error_text = "HTTP Response Code: %s, Text: '%s'" % (str(r.status_code), r.text)
         raise RuleAddFailedException(error_text)
 
+def _generate_delete_url(conf):
+    """
+        Generate the Rules URL for a DELETE request.
+    """
+    generated_url = _generate_rules_url(conf['url'])
+    parsed_url = urlparse(generated_url)
+    query = parsed_url.query
+    if query != '':
+        return generated_url.replace(query, query + "&_method=delete")
+    else:
+        return generated_url + "?_method=delete"
 
 def _delete(conf, built_rules):
     """
@@ -99,7 +115,7 @@ def _delete(conf, built_rules):
             built_rules: A single or list of built rules.
     """
     _check_rules_list(built_rules)
-    rules_url = _generate_rules_url(conf['url']) + "?_method=delete"
+    rules_url = _generate_delete_url(conf)
     delete_data = json.dumps(_generate_post_object(built_rules))
     r = requests.post(rules_url, auth=conf['auth'], data=delete_data)
     if not r.status_code in range(200,300):
diff --git a/setup.py b/setup.py
index 42ddcbc..d25cd66 100644
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@
 import os
 import sys
 
-version = "0.5.0"
+version = "0.5.1"
 
 try:
     from setuptools import setup


Test patch:
diff --git a/gnippy/test/test_rules.py b/gnippy/test/test_rules.py
index 4310376..a79a857 100644
--- a/gnippy/test/test_rules.py
+++ b/gnippy/test/test_rules.py
@@ -258,6 +258,20 @@ class RulesTestCase(unittest.TestCase):
         r = rules.get_rules(config_file_path=test_utils.test_config_path)
         self.assertEqual(1, len(r))
 
+    def test_generate_delete_url_normal_case(self):
+        """ Check if the Delete URL is generated correctly. """
+        conf = { 'url': 'https://stream.gnip.com:443/accounts/XXX/publishers/twitter/streams/track/prod.json' }
+        url = rules._generate_delete_url(conf)
+        self.assertEqual('https://api.gnip.com:443/accounts/XXX/publishers/twitter/streams/track/prod/rules.json?_method=delete',
+            url)
+
+    def test_generate_delete_url_with_query(self):
+        """ Account for https://github.com/abh1nav/gnippy/issues/15 """
+        conf = { 'url': 'https://stream.gnip.com:443/accounts/XXX/publishers/twitter/streams/track/prod.json?client=2' }
+        url = rules._generate_delete_url(conf)
+        self.assertEqual('https://api.gnip.com:443/accounts/XXX/publishers/twitter/streams/track/prod/rules.json?client=2&_method=delete',
+            url)
+
     @mock.patch('requests.post', good_delete)
     def test_delete_rules_single(self):
         """ Delete one rule. """