示例#1
0
    def list_invoices(self,
                      page: int = 1,
                      page_size: int = 10,
                      total_required: bool = True,
                      fields: List[str] = []) -> PaypalPage[Invoice]:
        """Calls the paypal API to get an invoice page.
        
        Arguments:
            page {int} -- current page
            page_size {int} -- page size 
            total_required {bool} -- total count required
            fields {List[str]} -- fields to be searched

        Returns:
            PaypalPage -- The paged elements in paypal API paged response 
        """
        query_params = {
            'page': page,
            'page_size': page_size,
            'total_required': total_required
        }

        if fields:
            query_params['fields'] = ','.join(fields)

        response = self._session.get(parse_url(self._base_url, 'invoices'),
                                     query_params)

        if response.status_code != 200:
            return PaypalPage.error(response)

        json_response = response.json()
        items = [
            Invoice.serialize_from_json(x)
            for x in json_response.get('items', [])
        ]
        links = [
            ActionLink(x['href'], x['rel'], x.get('method', 'GET'))
            for x in json_response.get('links', [])
        ]

        return PaypalPage.success(response, json_response.get('total_items'),
                                  json_response.get('total_pages'), items,
                                  links)
示例#2
0
    def search_invoices(self, page: int, page_size: int, total_required: bool,
                        search: InvoiceSearchRequest) -> PaypalPage[Invoice]:
        """Searches for and lists invoices that match search criteria. 
           If you pass multiple criteria, the response lists invoices 
           that match all criteria.

        Arguments:
            page {int} -- current page
            page_size {int} -- page size
            total_required {bool} -- total count required
            search {InvoiceSearchRequest} -- search criteria to be matched
        
        Returns:
            PaypalPage[Invoice] -- The paged elements in paypal API paged response 
        """
        query_params = {
            page: page,
            page_size: page_size,
            total_required: total_required
        }

        response = self._session.post(parse_url(self._base_url,
                                                'search-invoices'),
                                      json.dumps(search.to_dict()),
                                      params=query_params)

        if response.status_code != 200:
            return PaypalPage.error(response)

        json_response = response.json()
        items = [
            Invoice.serialize_from_json(x) for x in json_response['items']
        ]
        links = [
            ActionLink(x['href'], x['rel'], x.get('method', 'GET'))
            for x in json_response['links']
        ]

        return PaypalPage.success(response, json_response.get('total_items'),
                                  json_response.get('total_pages'), items,
                                  links)