Skip to content

Commit

Permalink
API create transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
redfast00 committed Apr 12, 2019
1 parent 0e9b607 commit 9a4be12
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
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|
respond_to do |format|
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def transaction_params
{
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,
issuer: current_client || current_user,
issuer: authenticate_user_or_client!,
amount: (t[:euros].to_f * 100 + t[:cents].to_f).to_i,
message: t[:message],
}.merge(current_client ? { id_at_client: t[:id_at_client] } : {})
Expand Down
20 changes: 20 additions & 0 deletions python_api_example/transactions.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9a4be12

Please sign in to comment.