Skip to content

Commit

Permalink
Add transactions to web API and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielwong159 committed Jun 27, 2019
1 parent 36feac3 commit 996d5ec
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
3 changes: 1 addition & 2 deletions apis/db/psql.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ def get_amount(username, password):
headers = {'Content-Type': 'application/json'}
data = json.dumps({'username': username, 'password': password})
req = requests.get(url, headers=headers, data=data)
response = json.loads(req.text)
return response['amount']
return float(req.text)


def execute_and_commit(query):
Expand Down
23 changes: 17 additions & 6 deletions apis/web/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
from flask import Flask, request
from operator import itemgetter
from web import login_valid, get_amount
from web import login_valid, get_amount, get_transactions

app = Flask(__name__)


@app.route('/validate')
@app.route('/validate', methods=['POST'])
def validate():
body = request.get_json()
username, password = itemgetter('username', 'password')(body)
Expand All @@ -15,7 +15,7 @@ def validate():
})


@app.route('/credit', methods=['GET'])
@app.route('/credit', methods=['POST'])
def credit():
body = request.get_json()
username, password = itemgetter('username', 'password')(body)
Expand All @@ -25,9 +25,20 @@ def credit():
except AssertionError:
return f'Error on account: {username}', 404

return json.dumps({
'amount': amount
})
return str(amount)


@app.route('/transaction', methods=['POST'])
def transaction():
body = request.get_json()
username, password = itemgetter('username', 'password')(body)

try:
transactions = get_transactions(username, password)
except AssertionError:
return f'Error on account: {username}', 404

return json.dumps(transactions)


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion apis/web/web/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .utils import get_request_params
from .login import login_valid
from .balance import get_amount
from .balance import get_amount
from .transaction import get_transactions
2 changes: 1 addition & 1 deletion hosts/telebot/handlers/conv/add_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def validate_credentials(username, password):
url = f'http://{WEB_API_HOST}:{WEB_API_PORT}/validate'
headers = {'Content-Type': 'application/json'}
data = json.dumps({'username': username, 'password': password})
req = requests.get(url, headers=headers, data=data)
req = requests.post(url, headers=headers, data=data)
response = json.loads(req.text)
return response['valid']

Expand Down
6 changes: 3 additions & 3 deletions services/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def get_amount(account):
url = f'http://{WEB_API_HOST}:{WEB_API_PORT}/credit'
headers = {'Content-Type': 'application/json'}
data = json.dumps({'username': account.username, 'password': account.password})
req = requests.get(url, headers=headers, data=data)
req = requests.post(url, headers=headers, data=data)

if not req.ok:
raise RetrieveError(username)
raise RetrieveError(account.username)

amount = json.loads(req.text)['amount']
amount = float(req.text)
return amount


Expand Down

0 comments on commit 996d5ec

Please sign in to comment.