Skip to content

Commit

Permalink
adapter modifications 1/9/21
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinrajesh8 committed Jan 9, 2021
1 parent 5830bd4 commit ca50677
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# SWIFT-EA
Chainlink external adapter that allows SWIFT financial messages to be accessed by the blockchain or smart contract


# Solidity Example Integration:
Declare UETR ID for desired Transaction
req.add("UETR", "d2ecb184-b622-11e9-a2a3-2a2ae2dbcce4")

GET status via HTTP request from options listed on: https://developer.swift.com/content/tracker-reference#tag/Get-Payment-Transaction-Details
req.add("status", "transactions") or "changed/transactions"

Declare your OAuth Basic 2.0
req.add("oauth", "YourSWIFT-APIOauthToken")

# Example JSON Response
74 changes: 74 additions & 0 deletions adapter_two.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from bridge import Bridge


class Adapter:
base_url = 'https://sandbox.swift.com/swift-apitracker/v4/payments' # '/{uetr}/status' needs to be appended
from_params = ['base', 'UETR', 'transaction']
action_params = ['cancellation', 'status', 'transactions']
oauth_token = ['oauth', 'token', 'oauth_token']

def __init__(self, input):
self.id = input.get('id', '1')
self.request_data = input.get('data')
if self.validate_request_data():
self.bridge = Bridge()
self.set_params()
self.create_request()
else:
self.result_error('No data provided')

def validate_request_data(self):
if self.request_data is None:
return False
if self.request_data == {}:
return False
return True

def set_params(self):
for param in self.from_params:
self.from_param = self.request_data.get(param)
if self.from_param is not None:
break
for param in self.action_params:
self.to_param = self.request_data.get(param)
if self.to_param is not None:
break
for param in self.oauth_token:
self.oauth = self.request_data.get(param)
if self.oauth is not None:
break

def create_request(self):
try:
#
headers = {"Authorization": "Basic "+self.oauth}
self.base_url += "/"+self.from_param+"/"+self.to_param
#
response = self.bridge.request(self.base_url, headers)
data = response.json()

# self.result = data[self.to_param]
self.result = data["return"] # might need to switch to ''
data['result'] = self.result
self.result_success(data)

except Exception as e:
self.result_error(e)
finally:
self.bridge.close()

def result_success(self, data):
self.result = {
'jobRunID': self.id,
'data': data,
'result': self.result,
'statusCode': 200,
}

def result_error(self, error):
self.result = {
'jobRunID': self.id,
'status': 'errored',
'error': f'There was an error: {error}',
'statusCode': 500,
}
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def call_adapter():
if data == '':
data = {}
adapter = Adapter(data)
# print(jsonify(adapter.result))
return jsonify(adapter.result)


Expand Down
3 changes: 2 additions & 1 deletion bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ def __init__(
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
self.session.mount('http://', adapter)
self.session.mount('http://', adapter) # mounts custom adapter to schema
self.session.mount('https://', adapter)

def request(self, url, params={}, headers={}, timeout=15):
try:
print(url+" ") # testing +params+" "+headers+" "+timeout
return self.session.get(url,
params=params,
headers=headers,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_adapter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# to execute pytest: pipenv run pytest -rP

import pytest
import adapter

Expand Down

0 comments on commit ca50677

Please sign in to comment.