implementing more strategy db query code

This commit is contained in:
Gert Wohlgemuth 2018-05-20 15:13:54 -07:00
parent 7320b60343
commit ae5230cf45
3 changed files with 100 additions and 10 deletions

View File

@ -4,6 +4,7 @@ from base64 import urlsafe_b64decode
import boto3
import simplejson as json
from boto3.dynamodb.conditions import Key, Attr
from jsonschema import validate
from freqtrade.aws.schemas import __SUBMIT_STRATEGY_SCHEMA__
@ -51,12 +52,39 @@ def performance(event, context):
def get(event, context):
"""
loads a strategy
returns the code of the requested strategy, if it's public
:param event:
:param context:
:return:
"""
pass
assert 'pathParameters' in event
assert 'user' in event['pathParameters']
assert 'name' in event['pathParameters']
table = db.Table(os.environ['strategyTable'])
response = table.query(
KeyConditionExpression=Key('user').eq(event['pathParameters']['user']) &
Key('name').eq(event['pathParameters']['name'])
)
if "Items" in response and len(response['Items']) > 0:
item = response['Items'][0]
item.pop('content')
return {
"statusCode": response['ResponseMetadata']['HTTPStatusCode'],
"body": json.dumps(item)
}
else:
return {
"statusCode": response['ResponseMetadata']['HTTPStatusCode'],
"body": json.dumps(response)
}
def code(event, context):
"""
@ -65,7 +93,39 @@ def code(event, context):
:param context:
:return:
"""
pass
assert 'pathParameters' in event
assert 'user' in event['pathParameters']
assert 'name' in event['pathParameters']
table = db.Table(os.environ['strategyTable'])
response = table.query(
KeyConditionExpression=Key('user').eq(event['pathParameters']['user']) &
Key('name').eq(event['pathParameters']['name'])
)
if "Items" in response and len(response['Items']) > 0:
if response['Items'][0]["public"]:
content = urlsafe_b64decode(response['Items'][0]['content'])
return {
"statusCode": response['ResponseMetadata']['HTTPStatusCode'],
"body": content
}
else:
return {
"statusCode": 403,
"body": json.dumps({"success": False, "reason": "Denied"})
}
else:
return {
"statusCode": response['ResponseMetadata']['HTTPStatusCode'],
"body": json.dumps(response)
}
def submit(event, context):
@ -76,11 +136,11 @@ def submit(event, context):
:return:
"""
print(event)
# get data
data = json.loads(event['body'])
# print("received data")
# print(data)
# validate against schema
result = validate(data, __SUBMIT_STRATEGY_SCHEMA__)
@ -88,6 +148,9 @@ def submit(event, context):
# print("data are validated");
# print(result)
# validate that the user is an Isaac User
# ToDo
strategy = urlsafe_b64decode(data['content']).decode('utf-8')
# print("loaded strategy")

View File

@ -4,6 +4,12 @@ import freqtrade.aws.strategy as aws
def test_strategy(lambda_context):
"""
very uggly long test
:param lambda_context:
:return:
"""
content = """# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from typing import Dict, List
@ -98,19 +104,37 @@ class TestStrategy(IStrategy):
assert (len(json.loads(aws.names({}, {})['body'])) == 2)
# we need to be able to get the code of the strategy
code = aws.code({'pathParameters': {
# we need to be able to get a strategy ( code cannot be included )
strategy = aws.get({'pathParameters': {
"name": "TestStrategy",
"user": "GCU4LW2XXZW3A3FM2XZJTEJHNWHTWDKY2DIJLCZJ5ULVZ4K7LZ7D23TH"
}}, {})
# code should equal our initial content
assert code == content
print(strategy)
strategy = json.loads(strategy['body'])
# we need to be able to get a strategy ( code cannot be included )
strategy = json.loads(aws.get({}, {}))
assert "content" not in strategy
assert "user" in strategy
assert "name" in strategy
assert "description" in strategy
assert "public" in strategy
assert "content" not in strategy
# we need to be able to get the code of the strategy
code = aws.code({'pathParameters': {
"name": "TestStrategy",
"user": "GCU4LW2XXZW3A3FM2XZJTEJHNWHTWDKY2DIJLCZJ5ULVZ4K7LZ7D23TH"
}}, {})['body']
# code should equal our initial content
assert code == content
# we are not allowed to load a private strategy
code = aws.code({'pathParameters': {
"name": "TestStrategy",
"user": "GCU4LW2XXZW3A3FM2XZJTEJHNWHTWDKY2DIJLCZJ5ULVZ4K7LZ7D23TG"
}}, {})
# code should equal our initial content
assert code['statusCode'] == 403
assert json.loads(code['body']) == {"success": False, "reason": "Denied"}

View File

@ -132,6 +132,9 @@ functions:
parameter:
paths:
name: true
response:
headers:
Content-Type: "'text/plain'"
environment:
strategyTable: ${self:custom.strategyTable}