Merge pull request #46 from ZeusWPI/feature/api-create-transaction
API create transaction
This commit is contained in:
commit
98f60701ec
3 changed files with 27 additions and 2 deletions
|
@ -1,7 +1,12 @@
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
# Prevent CSRF attacks by raising an exception.
|
# Prevent CSRF attacks by raising an exception.
|
||||||
# For APIs, you may want to use :null_session instead.
|
|
||||||
protect_from_forgery with: :exception
|
protect_from_forgery with: :exception
|
||||||
|
# Don't verfiy authenticity token (protects against CSRF) for API requests
|
||||||
|
skip_before_action :verify_authenticity_token, if: :api_request?
|
||||||
|
|
||||||
|
def api_request?
|
||||||
|
user_token && request.format.json?
|
||||||
|
end
|
||||||
|
|
||||||
rescue_from CanCan::AccessDenied do |exception|
|
rescue_from CanCan::AccessDenied do |exception|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
|
|
@ -51,7 +51,7 @@ class TransactionsController < ApplicationController
|
||||||
{
|
{
|
||||||
debtor: t[:debtor] ? User.find_or_create_by(name: t[:debtor]) : User.zeus,
|
debtor: t[:debtor] ? User.find_or_create_by(name: t[:debtor]) : User.zeus,
|
||||||
creditor: t[:creditor] ? User.find_or_create_by(name: t[:creditor]) : User.zeus,
|
creditor: t[:creditor] ? User.find_or_create_by(name: t[:creditor]) : User.zeus,
|
||||||
issuer: current_client || current_user,
|
issuer: authenticate_user_or_client!,
|
||||||
amount: (t[:euros].to_f * 100 + t[:cents].to_f).to_i,
|
amount: (t[:euros].to_f * 100 + t[:cents].to_f).to_i,
|
||||||
message: t[:message],
|
message: t[:message],
|
||||||
}.merge(current_client ? { id_at_client: t[:id_at_client] } : {})
|
}.merge(current_client ? { id_at_client: t[:id_at_client] } : {})
|
||||||
|
|
20
python_api_example/transactions.py
Normal file
20
python_api_example/transactions.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
base_url = 'http://localhost:3000'
|
||||||
|
user = 'j'
|
||||||
|
user_token = '1idEG5bFVVjUcl+15Y1DsQ=='
|
||||||
|
|
||||||
|
headers = {'Authorization': f'Token token={user_token}'}
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'transaction': {
|
||||||
|
'euros': 5.0,
|
||||||
|
'message': 'Transaction from Python',
|
||||||
|
'debtor': user,
|
||||||
|
'creditor': 'Zeus'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r = requests.post((base_url + f'/transactions.json'), headers=headers, json=data)
|
||||||
|
print(r.text)
|
Loading…
Reference in a new issue