Exemplo n.º 1
0
 def test_tagCount(self):
     params = {"tagged": "ptvs;ironpython"}
     instance = StackExchangeQueryString(params)
     self.assertEqual(instance._tagCount, 2)
     instance.addTag("debug")
     self.assertEqual(instance._tagCount, 3)
     instance.removeTag('ptvs')
     self.assertEqual(2, instance._tagCount)
     instance.removeTag('debug')
     instance.removeTag('ironpython')
     self.assertEqual(0, instance._tagCount)
Exemplo n.º 2
0
 def __init__(self, site, queryParams=None):
     self.segments = {
         'scheme': 'https',
         'netloc': StackExchangeQuery._netloc,
         'path': None,
         'params':
         None,  # Do not confuse this with query string parameters, which should be put with query as the key.  See urllib.parse module for more.
         'query': None,
         'fragment': None
     }
     # We have a query string object to allow rich functionality in manipulating this query's url.
     self.queryString = StackExchangeQueryString(queryParams)
     self.setSite(site)
     self.queryString.addPair('key', StackExchangeQuery._API_KEY)
Exemplo n.º 3
0
 def test_getFullString(self):
     instance = StackExchangeQueryString()
     instance.addPair("tagged", "ptvs")
     instance.addTag("python")
     self.assertRegex(
         instance.getFullString(),
         '(tagged=ptvs;python&site=stackoverflow)|(site=stackoverflow&tagged=ptvs;python)'
     )
Exemplo n.º 4
0
 def test_addPair(self):
     params = {"tagged": "ptvs", "pagesize": "1"}
     instance = StackExchangeQueryString(params)
     self.assertEqual(3, instance.getSize())
     instance.addPair(
         "site", "meta"
     )  # 'site' explicitly set here, parameter count is still 3...
     self.assertEqual(
         instance.getSize(),
         3), "instance.parameters should have 3 keys."  # ...as proven here.
     self.assertDictContainsSubset({"site": "meta"}, instance.parameters)
Exemplo n.º 5
0
 def test_removeTag(self):
     params = {'tagged': 'ptvs;python;debug'}
     instance = StackExchangeQueryString(params)
     instance.removeTag('ptvs')
     self.assertEqual(instance.retrieve('tagged'), 'python;debug')
     instance.removeTag('python')
     instance.removeTag('debug')
     self.assertFalse(instance.removeTag('noMoreTagsToRemove'))
     self.assertFalse(instance.retrieve('tagged'))
     self.assertEqual(instance._tagCount, 0)
     self.assertEqual(instance.getSize(), 1)
Exemplo n.º 6
0
 def test_constructionWithoutStackExchangeQueryString(self):
     instance = StackExchangeQueryString()
     self.assertEqual(
         instance.getSize(),
         1), "instance.parameters should have default 'site' key."
Exemplo n.º 7
0
 def test_addTag(self):
     params = {"tagged": "ptvs"}
     instance = StackExchangeQueryString(params)
     instance.addTag("ironpython")
     self.assertNotIn("django", instance.retrieve("tagged"))
     self.assertIn("ironpython", instance.retrieve("tagged"))
     instance.addTag("django")
     self.assertIn("django", instance.retrieve("tagged"))
     instance.addTag("fourth")
     instance.addTag("fifth")
     self.assertFalse(
         instance.addTag("sixth"))  # separams.MAX_TAGS currently set to 5
Exemplo n.º 8
0
 def test_retrieve(self):
     params = {"tagged": "ptvs"}
     instance = StackExchangeQueryString(params)
     self.assertEquals("ptvs", instance.retrieve("tagged"))
     self.assertEqual('stackoverflow', instance.retrieve("site"))
Exemplo n.º 9
0
 def test_constructionWithStackExchangeQueryString(self):
     params = {"tagged": "ptvs", "pagesize": "1"}
     instance = StackExchangeQueryString(params)
     self.assertEqual(instance.getSize(),
                      3), "instance.parameters should have 3 keys."
Exemplo n.º 10
0
class StackExchangeQuery(object):
    _API_KEY = 'E3Ht0FN1L57M68Wk56e2RA(('  # API Key for Minerva, from StackExchange.com
    _version = '2.2'
    _netloc = 'api.stackexchange.com'
    _paths = {
        'questions': _version + '/questions',
        'answers': _version + '/answers'
    }

    def __init__(self, site, queryParams=None):
        self.segments = {
            'scheme': 'https',
            'netloc': StackExchangeQuery._netloc,
            'path': None,
            'params':
            None,  # Do not confuse this with query string parameters, which should be put with query as the key.  See urllib.parse module for more.
            'query': None,
            'fragment': None
        }
        # We have a query string object to allow rich functionality in manipulating this query's url.
        self.queryString = StackExchangeQueryString(queryParams)
        self.setSite(site)
        self.queryString.addPair('key', StackExchangeQuery._API_KEY)

    def buildFullUrl(self):
        self.__buildUrlItems()
        return parse.urlunparse(self.__getIterableUrlItems())

    def __getIterableUrlItems(self):
        """Get all values from the segments dictionary in the appropriate order as defined by the urllib.parse module."""
        order = ['scheme', 'netloc', 'path', 'params', 'query', 'fragment']
        segs = []
        for key in order:
            segs.append(self.segments[key])
        return segs

    def __buildUrlItems(self):
        if self.segments['path'] is None:
            # Default to 'questions' when not set explicitly.
            self.segments['path'] = StackExchangeQuery._paths['questions']
        if self.segments['params'] is None:
            self.segments['params'] = ''
        self.segments['query'] = self.queryString.getFullString()
        if self.segments['fragment'] is None:
            self.segments['fragment'] = ''

    def setQueryType(self, type):
        if not type in StackExchangeQuery._paths.keys():
            raise ValueError("%s as a query type is not implemented." % type)
        self.segments['path'] = StackExchangeQuery._paths[type]
        self.idAdded = False  # changing the query type removes added ids.

    def setSite(self, site):
        self.queryString.addPair('site', site)

    def addId(self, id):
        if not self.idAdded:
            self.segments['path'] = self.segments['path'] + '/' + str(id)
            self.idAdded = True
            return True
        return False

    def go(self):
        content = requests.get(self.buildFullUrl())
        self.response = StackExchangeResponse(content)


# Here for testing/debugging.
#queryParams = {"tagged":"ptvs;python", 'pagesize':'1'}
#q = StackExchangeQuery('stackoverflow', queryParams)
#q.go()
#q.response.printAll()

#segments = {
#    'scheme': 'https',
#    'netloc': 'api.stackexchange.com',
#    'path': '2.2/questions',
#    'params': '',
#    'query': 'order=desc&sort=activity&site=stackoverflow',
#    'fragment': ''
#}