Skip to content

Commit

Permalink
TDL-23235 add payouts stream (#109)
Browse files Browse the repository at this point in the history
* add payouts stream

* Removed settlements stream

* bump version changes to 2.1.0
  • Loading branch information
rdeshmukh15 authored Aug 9, 2023
1 parent 6296e02 commit 64734b3
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 130 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [v2.1.0] (2023-06-08)

[Full Changelog](https://github.com/singer-io/tap-square/compare/v2.0.0...v2.1.0)

* Replaces the deprecated `settlements` stream with the `payouts` stream [#109](https://github.com/singer-io/tap-square/pull/109)

## [v2.0.0](https://github.com/singer-io/tap-square/tree/v1.w.0) (2023-06-08)

[Full Changelog](https://github.com/singer-io/tap-square/compare/v1.3.1...v2.0.0)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ This tap:
* BankAccounts
* Refunds
* Payments
* Payouts
* ModifierLists
* Inventories
* Orders
* Roles
* Shifts
* CashDrawerShifts
* Settlements
* Customers

* Includes a schema for each resource reflecting most recent tested data retrieved using the api. See [the schema folder](https://github.com/singer-io/tap-square/tree/master/tap_square/schemas) for details.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup

setup(name='tap-square',
version='2.0.0',
version='2.1.0',
description='Singer.io tap for extracting data from the Square API',
author='Stitch',
url='http://singer.io',
Expand Down
58 changes: 28 additions & 30 deletions tap_square/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,33 +348,31 @@ def get_roles(self, bookmarked_cursor):
bookmarked_cursor,
)

def get_settlements(self, location_id, start_time, bookmarked_cursor):
url = 'https://connect.squareup.com/v1/{}/settlements'.format(location_id)

now = utils.now()
start_time_dt = utils.strptime_to_utc(start_time)
end_time_dt = now

# Parameter `begin_time` cannot be before 1 Jan 2013 00:00:00Z
# Doc: https://developer.squareup.com/reference/square/settlements-api/v1-list-settlements
if start_time_dt < utils.strptime_to_utc("2013-01-01T00:00:00Z"):
raise Exception("Start Date for Settlements stream cannot come before `2013-01-01T00:00:00Z`, current start_date: {}".format(start_time))

while start_time_dt < now:
params = {
'limit': 200,
'begin_time': utils.strftime(start_time_dt),
}
# If query range is over a year, shorten to a year
if now - start_time_dt > timedelta(weeks=52):
end_time_dt = start_time_dt + timedelta(weeks=52)
params['end_time'] = utils.strftime(end_time_dt)
yield from self._get_v1_objects(
url,
params,
'settlements',
bookmarked_cursor,
)
# Attempt again to sync til "now"
start_time_dt = end_time_dt
end_time_dt = now
def get_payouts(self, location_id, start_time, bookmarked_cursor):
if bookmarked_cursor:
cursor = bookmarked_cursor
else:
cursor = '__initial__' # initial value so while loop is always entered one time

end_time = utils.strftime(utils.now(), utils.DATETIME_PARSE)
while cursor:
if cursor == '__initial__':
# initial text was needed to go into the while loop, but api needs
# it to be a valid bookmarked cursor or None
cursor = bookmarked_cursor

with singer.http_request_timer('GET payouts details'):
result = self._retryable_v2_method(
lambda bdy: self._client.payouts.list_payouts(
location_id=location_id,
begin_time=start_time,
end_time=end_time,
cursor=cursor,
limit=100,
),
None,
)

yield (result.body.get('items', []), result.body.get('cursor'))

cursor = result.body.get('cursor')
2 changes: 1 addition & 1 deletion tap_square/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def get_abs_path(path):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), path)

# NB: These streams cannot be queried using Sandbox OAuth credentials
PRODUCTION_ONLY_STREAMS = {'roles', 'bank_accounts', 'settlements'}
PRODUCTION_ONLY_STREAMS = {'roles', 'bank_accounts', 'payouts'}

def get_schemas(sandbox):

Expand Down
152 changes: 152 additions & 0 deletions tap_square/schemas/payouts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"type": [
"null",
"object"
],
"properties": {
"id": {
"type": [
"null",
"string"
]
},
"status": {
"type": [
"null",
"string"
]
},
"location_id": {
"type": [
"null",
"string"
]
},
"created_at": {
"type": [
"null",
"string"
],
"format": "date-time"
},
"updated_at": {
"type": [
"null",
"string"
],
"format": "date-time"
},
"amount_money": {
"type": [
"null",
"object"
],
"properties": {
"amount": {
"type": [
"null",
"integer"
]
},
"currency": {
"type": [
"null",
"string"
]
}
}
},
"destination": {
"type": [
"null",
"object"
],
"properties": {
"type": {
"type": [
"null",
"string"
]
},
"id": {
"type": [
"null",
"string"
]
}
}
},
"version": {
"type": [
"null",
"integer"
]
},
"type": {
"type": [
"null",
"string"
]
},
"payout_fee": {
"type": [
"null",
"array"
],
"items": {
"type": [
"null",
"object"
],
"properties": {
"effective_at": {
"type": [
"null",
"string"
],
"format": "date-time"
},
"type": {
"type": [
"null",
"string"
]
},
"amount_money": {
"type": [
"null",
"object"
],
"properties": {
"amount": {
"type": [
"null",
"integer"
]
},
"currency": {
"type": [
"null",
"string"
]
}
}
}
}
}
},
"arrival_date": {
"type": [
"null",
"string"
],
"format": "date-time"
},
"end_to_end_id": {
"type": [
"null",
"string"
]
}
}
}
62 changes: 0 additions & 62 deletions tap_square/schemas/settlements.json

This file was deleted.

10 changes: 5 additions & 5 deletions tap_square/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,17 @@ def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_cash_drawer_shifts(location_id, start_time, bookmarked_cursor)


class Settlements(FullTableStream):
tap_stream_id = 'settlements'
class Payouts(FullTableStream):
tap_stream_id = 'payouts'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None

def get_pages(self, bookmarked_cursor, start_time):
for location_id in Locations.get_all_location_ids(self.client):
# Settlements requests can only take up to 1 location_id at a time
yield from self.client.get_settlements(location_id, start_time, bookmarked_cursor)
# payouts requests can only take up to 1 location_id at a time
yield from self.client.get_payouts(location_id, start_time, bookmarked_cursor)

class TeamMembers(Stream):
tap_stream_id = 'team_members'
Expand Down Expand Up @@ -399,13 +399,13 @@ def sync(self, state, stream_schema, stream_metadata, config, transformer):
'bank_accounts': BankAccounts,
'refunds': Refunds,
'payments': Payments,
'payouts': Payouts,
'modifier_lists': ModifierLists,
'inventories': Inventories,
'orders': Orders,
'roles': Roles,
'shifts': Shifts,
'cash_drawer_shifts': CashDrawerShifts,
'settlements': Settlements,
'team_members': TeamMembers,
'customers': Customers
}
Loading

0 comments on commit 64734b3

Please sign in to comment.