예제 #1
0
파일: core.py 프로젝트: tate11/WooGenerator
    def get_iterator(self, endpoint=None):
        """
        Get an iterator to the pages in the API
        """
        if not endpoint:
            endpoint = self.endpoint_plural

        endpoint_queries = UrlUtils.get_query_dict_singular(endpoint)
        if self.pagination_limit_key not in endpoint_queries \
        and self.limit is not None:
            endpoint_queries[self.pagination_limit_key] = self.limit

        if self.pagination_offset_key not in endpoint_queries \
        and self.offset is not None:
            endpoint_queries[self.pagination_offset_key] = self.offset
        if endpoint_queries:
            endpoint = UrlUtils.substitute_query(endpoint, urlencode(endpoint_queries))
        return self.ApiIterator(
            self.service,
            endpoint,
            pagination_limit_key=self.pagination_limit_key,
            pagination_number_key=self.pagination_number_key,
            pagination_offset_key=self.pagination_offset_key,
            total_pages_key=self.total_pages_key,
            total_items_key=self.total_items_key,
        )
예제 #2
0
파일: core.py 프로젝트: tate11/WooGenerator
        def __init__(self, service, endpoint, **kwargs):
            assert isinstance(service, WPAPIService)
            self.service = service
            self.next_endpoint = endpoint
            self.prev_response = None
            self.total_pages = None
            self.total_items = None
            self.pagination_limit_key = kwargs.get('pagination_limit_key')
            self.pagination_number_key = kwargs.get('pagination_number_key')
            self.pagination_offset_key = kwargs.get('pagination_offset_key')
            self.total_pages_key = kwargs.get('total_pages_key')
            self.total_items_key = kwargs.get('total_items_key')
            self.progress_counter = None

            endpoint_queries = UrlUtils.get_query_dict_singular(endpoint)

            self.next_page = None
            if self.pagination_number_key in endpoint_queries:
                self.next_page = int(endpoint_queries[self.pagination_number_key])
            self.limit = 10
            if self.pagination_limit_key in endpoint_queries:
                self.limit = int(endpoint_queries[self.pagination_limit_key])
            self.offset = None
            if self.pagination_offset_key in endpoint_queries:
                self.offset = int(endpoint_queries[self.pagination_offset_key])
예제 #3
0
    def get_iterator(self, endpoint=None, **kwargs):
        """Get an iterator to the pages in the API."""
        if not endpoint:
            endpoint = self.endpoint_plural

        endpoint_queries = UrlUtils.get_query_dict_singular(endpoint)
        if (self.pagination_limit_key not in endpoint_queries
                and self.limit is not None):
            endpoint_queries[self.pagination_limit_key] = self.limit

        if (self.pagination_offset_key not in endpoint_queries
                and self.offset is not None):
            endpoint_queries[self.pagination_offset_key] = self.offset
        if endpoint_queries:
            endpoint = UrlUtils.substitute_query(endpoint,
                                                 urlencode(endpoint_queries))

        defaults = {
            'pagination_limit_key': self.pagination_limit_key,
            'pagination_number_key': self.pagination_number_key,
            'pagination_offset_key': self.pagination_offset_key,
            'total_pages_key': self.total_pages_key,
            'total_items_key': self.total_items_key,
        }

        kwargs = SeqUtils.combine_ordered_dicts(
            defaults, kwargs
        )

        return self.ApiIterator(
            self.service,
            endpoint,
            **kwargs
        )
예제 #4
0
 def get_auth_url(self, endpoint_url, method, **kwargs):
     if self.query_string_auth:
         endpoint_params = UrlUtils.get_query_dict_singular(endpoint_url)
         endpoint_params.update({
             "consumer_key": self.consumer_key,
             "consumer_secret": self.consumer_secret
         })
         endpoint_url = UrlUtils.substitute_query(
             endpoint_url, UrlUtils.flatten_params(endpoint_params))
     return endpoint_url
예제 #5
0
 def test_url_get_query_dict_singular(self):
     result = UrlUtils.get_query_dict_singular(self.test_url)
     self.assertEquals(
         result, {
             'filter[limit]': '2',
             'oauth_nonce': 'c4f2920b0213c43f2e8d3d3333168ec4a22222d1',
             'oauth_timestamp': '1481601370',
             'oauth_consumer_key':
             'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
             'oauth_signature_method': 'HMAC-SHA1',
             'oauth_signature': '3ibOjMuhj6JGnI43BQZGniigHh8=',
             'page': '2'
         })
예제 #6
0
    def test_APIPutWithSimpleQuery(self):
        wcapi = API(**self.api_params)
        response = wcapi.get('products')
        first_product = (response.json())['products'][0]
        original_title = first_product['title']
        product_id = first_product['id']

        nonce = str(random.random())
        response = wcapi.put('products/%s?filter%%5Blimit%%5D=5' % (product_id), {"product":{"title":str(nonce)}})
        request_params = UrlUtils.get_query_dict_singular(response.request.url)
        response_obj = response.json()
        self.assertEqual(response_obj['product']['title'], str(nonce))
        self.assertEqual(request_params['filter[limit]'], str(5))

        wcapi.put('products/%s' % (product_id), {"product":{"title":original_title}})
예제 #7
0
 def test_APIPutWithSimpleQuery(self):
     wcapi = API(**self.apiParams)
     nonce = str(random.random())
     response = wcapi.put('products/633?filter%5Blimit%5D=5',
                          {"product": {
                              "title": str(nonce)
                          }})
     request_params = UrlUtils.get_query_dict_singular(response.request.url)
     # print "\ntest_APIPutWithSimpleQuery"
     # print "request url", response.request.url
     # print "response", UrlUtils.beautify_response(response)
     response_obj = response.json()
     # print "response obj", response_obj
     self.assertEqual(response_obj['product']['title'], str(nonce))
     self.assertEqual(request_params['filter[limit]'], str(5))
예제 #8
0
    def test_APIPutWithSimpleQuery(self):
        wcapi = API(**self.api_params)
        response = wcapi.get('products')
        first_product = (response.json())[0]
        # from pprint import pformat
        # print "first product %s" % pformat(response.json())
        original_title = first_product['name']
        product_id = first_product['id']

        nonce = str(random.random())
        response = wcapi.put('products/%s?page=2&per_page=5' % (product_id), {"name":str(nonce)})
        request_params = UrlUtils.get_query_dict_singular(response.request.url)
        response_obj = response.json()
        self.assertEqual(response_obj['name'], str(nonce))
        self.assertEqual(request_params['per_page'], '5')

        wcapi.put('products/%s' % (product_id), {"name":original_title})
예제 #9
0
        def __init__(self, service, endpoint):
            assert isinstance(service, WPAPI_Service)
            self.service = service
            self.next_endpoint = endpoint
            self.prev_response = None
            self.total_pages = None
            self.total_items = None
            # self.progressCounter = None

            endpoint_queries = UrlUtils.get_query_dict_singular(endpoint)
            # print "endpoint_queries:", endpoint_queries
            self.next_page = None
            if 'page' in endpoint_queries:
                self.next_page = int(endpoint_queries['page'])
            self.limit = 10
            if 'fliter[limit]' in endpoint_queries:
                self.limit = int(endpoint_queries['filter[limit]'])
            # print "slave limit set to to ", self.limit
            self.offset = None
            if 'filter[offset]' in endpoint_queries:
                self.offset = int(endpoint_queries['filter[offset]'])