Skip to content

Commit

Permalink
Add method to list invoice line items (#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-stripe committed May 24, 2024
1 parent c5e2eab commit e559fde
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
56 changes: 56 additions & 0 deletions stripe/_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from stripe._expandable_field import ExpandableField
from stripe._list_object import ListObject
from stripe._listable_api_resource import ListableAPIResource
from stripe._nested_resource_class_methods import nested_resource_class_methods
from stripe._request_options import RequestOptions
from stripe._search_result_object import SearchResultObject
from stripe._searchable_api_resource import SearchableAPIResource
Expand Down Expand Up @@ -51,6 +52,7 @@
from stripe.test_helpers._test_clock import TestClock


@nested_resource_class_methods("line")
class Invoice(
CreateableAPIResource["Invoice"],
DeletableAPIResource["Invoice"],
Expand Down Expand Up @@ -2581,6 +2583,24 @@ class FinalizeInvoiceParams(RequestOptions):
Specifies which fields in the response should be expanded.
"""

class ListLinesParams(RequestOptions):
ending_before: NotRequired[str]
"""
A cursor for use in pagination. `ending_before` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, starting with `obj_bar`, your subsequent call can include `ending_before=obj_bar` in order to fetch the previous page of the list.
"""
expand: NotRequired[List[str]]
"""
Specifies which fields in the response should be expanded.
"""
limit: NotRequired[int]
"""
A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 10.
"""
starting_after: NotRequired[str]
"""
A cursor for use in pagination. `starting_after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with `obj_foo`, your subsequent call can include `starting_after=obj_foo` in order to fetch the next page of the list.
"""

class ListParams(RequestOptions):
collection_method: NotRequired[
Literal["charge_automatically", "send_invoice"]
Expand Down Expand Up @@ -6883,6 +6903,42 @@ async def search_auto_paging_iter_async(
) -> AsyncIterator["Invoice"]:
return (await cls.search_async(*args, **kwargs)).auto_paging_iter()

@classmethod
def list_lines(
cls, invoice: str, **params: Unpack["Invoice.ListLinesParams"]
) -> ListObject["InvoiceLineItem"]:
"""
When retrieving an invoice, you'll get a lines property containing the total count of line items and the first handful of those items. There is also a URL where you can retrieve the full (paginated) list of line items.
"""
return cast(
ListObject["InvoiceLineItem"],
cls._static_request(
"get",
"/v1/invoices/{invoice}/lines".format(
invoice=sanitize_id(invoice)
),
params=params,
),
)

@classmethod
async def list_lines_async(
cls, invoice: str, **params: Unpack["Invoice.ListLinesParams"]
) -> ListObject["InvoiceLineItem"]:
"""
When retrieving an invoice, you'll get a lines property containing the total count of line items and the first handful of those items. There is also a URL where you can retrieve the full (paginated) list of line items.
"""
return cast(
ListObject["InvoiceLineItem"],
await cls._static_request_async(
"get",
"/v1/invoices/{invoice}/lines".format(
invoice=sanitize_id(invoice)
),
params=params,
),
)

_inner_class_types = {
"automatic_tax": AutomaticTax,
"custom_fields": CustomField,
Expand Down
5 changes: 5 additions & 0 deletions tests/api_resources/test_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,8 @@ def test_can_iterate_lines(self):
seen = [item["id"] for item in resource.lines.auto_paging_iter()]

assert seen.__len__() > 0

def test_can_list_line_items(self):
resource = stripe.Invoice.list_lines(TEST_RESOURCE_ID)
assert isinstance(resource.data, list)
assert isinstance(resource.data[0], stripe.InvoiceLineItem)

0 comments on commit e559fde

Please sign in to comment.