Skip to content

Commit fc12ed3

Browse files
authored
update quickstart for new sdk (#3)
1 parent 5ed02e2 commit fc12ed3

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ The sample is based on the [Python SDK Quickstart guide](https://developer.squar
3535
$ pip3 install squareup
3636
```
3737
38+
1. [optional] Install the legacy Square SDK, to run the legacy quickstart
39+
```
40+
$ pip3 install squareup_legacy
41+
```
42+
3843
1. Run the code
3944
```
4045
$ python3 ./quickstart.py

quickstart/quickstart.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
from square.http.auth.o_auth_2 import BearerAuthCredentials
2-
from square.client import Client
1+
from square import Square
2+
from square.environment import SquareEnvironment
3+
from square.core.api_error import ApiError
34
from dotenv import load_dotenv
45
import os
56

67
load_dotenv()
78

8-
client = Client(
9-
bearer_auth_credentials=BearerAuthCredentials(
10-
access_token=os.environ['SQUARE_ACCESS_TOKEN']
11-
),
12-
environment='sandbox')
9+
client = Square(
10+
environment=SquareEnvironment.SANDBOX,
11+
token=os.environ['SQUARE_ACCESS_TOKEN']
12+
)
1313

14-
result = client.locations.list_locations()
14+
try:
15+
response = client.locations.list()
16+
for location in response.locations:
17+
print(f"{location.id}: ", end="")
18+
print(f"{location.name}, ", end="")
19+
print(f"{location.address}, ", end="")
1520

16-
if result.is_success():
17-
for location in result.body['locations']:
18-
print(f"{location['id']}: ", end="")
19-
print(f"{location['name']}, ", end="")
20-
print(f"{location['address']['address_line_1']}, ", end="")
21-
print(f"{location['address']['locality']}")
22-
23-
elif result.is_error():
24-
for error in result.errors:
25-
print(error['category'])
26-
print(error['code'])
27-
print(error['detail'])
21+
except ApiError as e:
22+
for error in e.errors:
23+
print(error.category)
24+
print(error.code)
25+
print(error.detail)

quickstart/quickstart_legacy.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from square_legacy.http.auth.o_auth_2 import BearerAuthCredentials
2+
from square_legacy.client import Client
3+
from dotenv import load_dotenv
4+
import os
5+
6+
load_dotenv()
7+
8+
client = Client(
9+
bearer_auth_credentials=BearerAuthCredentials(
10+
access_token=os.environ['SQUARE_ACCESS_TOKEN']
11+
),
12+
environment='sandbox')
13+
14+
result = client.locations.list_locations()
15+
16+
if result.is_success():
17+
for location in result.body['locations']:
18+
print(f"{location['id']}: ", end="")
19+
print(f"{location['name']}, ", end="")
20+
print(f"{location['address']['address_line_1']}, ", end="")
21+
print(f"{location['address']['locality']}")
22+
23+
elif result.is_error():
24+
for error in result.errors:
25+
print(error['category'])
26+
print(error['code'])
27+
print(error['detail'])

0 commit comments

Comments
 (0)