-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (52 loc) · 1.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json
import os
import upwork
from dotenv import load_dotenv
from flask import Flask, request
from flask_cors import CORS
from oauthlib.oauth2.rfc6749.errors import InvalidClientIdError
from upwork.routers import auth, payments
app = Flask(__name__)
CORS(app)
load_dotenv()
def create_client():
with open('token.json', 'r') as f:
token_string = f.read()
token = json.loads(token_string)
config = upwork.Config({
"client_id": os.getenv("CLIENT_ID"),
"client_secret": os.getenv("CLIENT_SECRET"),
"token": token
})
client = upwork.Client(config)
return client
@app.route('/callback', methods=['POST'])
def auth_callback():
return
@app.route('/payBonus', methods=['POST'])
def pay_bonus():
client = create_client()
data = request.json
params = {
'engagement__reference': data['engagement__reference'],
'comments': data['description'],
'charge_amount': round(data['payment'], 2)
}
print(params)
try:
response = payments.Api(client).submit_bonus(data['team__reference'], params)
except InvalidClientIdError:
authorization_url, state = client.get_authorization_url()
authz_code = input("Please enter the full callback URL you get "
"following this link:\n{0}\n\n> ".format(authorization_url))
print("Retrieving access and refresh tokens.... ")
token = client.get_access_token(authz_code)
token_str = json.dumps(token)
with open('token.json', 'w') as f:
f.write(token_str)
client = create_client() # recreate client with new token
response = payments.Api(client).submit_bonus(data['team__reference'], params)
print(response)
return response
if __name__ == '__main__':
app.run(port=5001)