-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9e44b07
Showing
18 changed files
with
674 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/ | ||
__pycache__ | ||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
InstAL-Rest fiddling repository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import os | ||
from instalrest.v1.api import app, models | ||
import instalrest.instalcelery.instalcelery | ||
def create_default(app): | ||
print("CREATE DEFAULT") | ||
models.setup_tables() | ||
host = os.environ.get("INSTAL_FLASK_REST_HOST","0.0.0.0") | ||
port = os.environ.get("INSTAL_FLASK_REST_PORT","5000") | ||
debug = os.environ.get("INSTAL_FLASK_REST_DEBUG",False) | ||
if debug == "False": | ||
debug = False | ||
threads = os.environ.get("INSTAL_FLASK_REST_THREADS",1) | ||
return app | ||
|
||
app_modified = create_default(app) | ||
|
||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = "1.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
version: '2' | ||
services: | ||
instal-db: | ||
image: postgres | ||
ports: | ||
- "5432:5432" | ||
environment: | ||
- POSTGRES_PASSWORD=instal | ||
- POSTGRES_USER=instal | ||
- POSTGRES_DB=instal-rest-v1 | ||
|
||
instal-rabbit: | ||
image: rabbitmq | ||
ports: | ||
- "5672:5672" | ||
environment: | ||
- RABBITMQ_DEFAULT_USER=instal | ||
- RABBITMQ_DEFAULT_PASS=instal | ||
|
||
instal-rest: | ||
image: instal-rest | ||
ports: | ||
- "5000:5000" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from celery import Celery | ||
from instalrest.v1 import models | ||
from tempfile import NamedTemporaryFile | ||
import instal | ||
import simplejson as json | ||
celery_app = Celery('instal-rest', broker='amqp://instal:instal@instal-rabbit:5672//') | ||
|
||
@celery_app.task | ||
def execute_instal_async(instalQuery): | ||
query = models.InstALQuery.get(id=instalQuery) | ||
query.execute_instal() | ||
|
||
@celery_app.task | ||
def get_instal_trace_text(as_json): | ||
json_file = NamedTemporaryFile("w+t") | ||
json_file.write(json.dumps(as_json)) | ||
json_file.seek(0) | ||
text_out = NamedTemporaryFile("w+t") | ||
instal.instaltrace.instal_trace_keyword(json_file=json_file.name, text_file=text_out.name) | ||
text_out.seek(0) | ||
return text_out.read() | ||
|
||
@celery_app.task | ||
def get_instal_inspect(instalModel): | ||
model = models.InstALModel.get(id=instalModel) | ||
return model.get_inspect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from unittest import TestCase | ||
import requests | ||
import simplejson as json | ||
import time | ||
|
||
INSTAL_URL = "http://127.0.0.1:5000" | ||
class Basic(TestCase): | ||
def test_query_execute(self): | ||
model_response = requests.post(INSTAL_URL + "/model/",headers={'Content-Type': 'application/json'}, | ||
data=json.dumps({"institutions": ["institution a; exogenous event e; initially pow(e);" ] })) | ||
assert(model_response.status_code==201) | ||
model_response_json = json.loads(model_response.content) | ||
assert(model_response_json.get("id",0)) | ||
|
||
grounding_response = requests.post(INSTAL_URL + "/model/{}/grounding/".format(model_response_json["id"]),data=json.dumps({}),headers={'Content-Type': 'application/json'}) | ||
assert(grounding_response.status_code==201) | ||
grounding_response_json = json.loads(grounding_response.content) | ||
assert(grounding_response_json.get("id",0)) | ||
|
||
query_response = requests.post(INSTAL_URL + "/model/{}/grounding/{}/query/".format(model_response_json["id"],grounding_response_json["id"]), | ||
headers={'Content-Type': 'application/json'},data=json.dumps({"query" : ["observed(e)"]})) | ||
assert(query_response.status_code==201) | ||
query_response_json = json.loads(query_response.content) | ||
assert(query_response_json.get("id",0)) | ||
#assert(query_response_json.get("json_out",{})) | ||
|
||
time.sleep(3) | ||
response = requests.get(INSTAL_URL + "/model/{}/grounding/{}/query/{}/output/1/".format( | ||
query_response_json.get("grounding", {}).get("model", {}).get("id"), query_response_json.get("grounding", {}).get("id"), | ||
query_response_json.get("id") | ||
) | ||
, | ||
data=json.dumps( | ||
{"type": "json"} | ||
), | ||
headers={'Content-Type': 'application/json'}) | ||
|
||
assert (response.status_code == 200) | ||
|
||
def test_query_more_complicated(self): | ||
model_response = requests.post(INSTAL_URL + "/model/", | ||
data=json.dumps({"institutions": ["institution a; type A; exogenous event ex_a(A); inst event in_a(A); initially pow(ex_a(A)), perm(ex_a(A)), perm(in_a(A));", | ||
"institution c; type A; exogenous event ex_b(A); initially pow(ex_b(A));"], | ||
"bridges" : ["bridge b; source a; sink c; in_a(A) xgenerates ex_b(A); initially gpow(a, ex_b(A), c);"]}), | ||
headers={'Content-Type': 'application/json'}) | ||
assert(model_response.status_code==201) | ||
model_response_json = json.loads(model_response.content) | ||
assert(model_response_json.get("id",0)) | ||
|
||
grounding_response = requests.post(INSTAL_URL + "/model/{}/grounding/".format(model_response_json["id"]), data=json.dumps({"types" : {"A" : ["alpha", "beta"]}}), | ||
headers={'Content-Type': 'application/json'}) | ||
assert(grounding_response.status_code==201) | ||
grounding_response_json = json.loads(grounding_response.content) | ||
assert(grounding_response_json.get("id",0)) | ||
|
||
query_response = requests.post(INSTAL_URL + "/model/{}/grounding/{}/query/".format(model_response_json["id"],grounding_response_json["id"]), | ||
data=json.dumps({"query" : []}), | ||
headers = {'Content-Type': 'application/json'}) | ||
assert(query_response.status_code==201) | ||
query_response_json = json.loads(query_response.content) | ||
assert(query_response_json.get("id",0)) | ||
assert(query_response_json.get("json_out",{})) | ||
time.sleep(3) | ||
response = requests.get(INSTAL_URL + "/model/{}/grounding/{}/query/{}/output/1/".format( | ||
query_response_json.get("grounding", {}).get("model", {}).get("id"), query_response_json.get("grounding", {}).get("id"), | ||
query_response_json.get("id") | ||
) | ||
, | ||
data=json.dumps( | ||
{"type": "json"} | ||
), | ||
headers={'Content-Type': 'application/json'}) | ||
|
||
assert (response.status_code == 200) | ||
|
||
def test_new_basic(self): | ||
query_response = requests.post(INSTAL_URL + "/new/", | ||
data = json.dumps({ | ||
"institutions": [ | ||
"institution a; type A; exogenous event ex_a(A); inst event in_a(A); initially pow(ex_a(A)), perm(ex_a(A)), perm(in_a(A));", | ||
"institution c; type A; exogenous event ex_b(A); initially pow(ex_b(A));"], | ||
"bridges": [ | ||
"bridge b; source a; sink c; in_a(A) xgenerates ex_b(A); initially gpow(a, ex_b(A), c);"], | ||
"types": {"A": ["alpha", "beta"]}, | ||
"query": ["ex_a(alpha)", "ex_a(beta)"] | ||
}), | ||
headers= {'Content-Type' : 'application/json'}) | ||
assert(query_response.status_code==201) | ||
|
||
json_resp = query_response.json() | ||
time.sleep(3) | ||
response = requests.get(INSTAL_URL + "/model/{}/grounding/{}/query/{}/output/1/".format( | ||
json_resp.get("grounding", {}).get("model", {}).get("id"), json_resp.get("grounding", {}).get("id"), | ||
json_resp.get("id") | ||
) | ||
, | ||
headers={'Accept' : 'application/json'}) | ||
|
||
assert (response.status_code == 200) | ||
|
||
def test_get_text(self): | ||
query_response = requests.post(INSTAL_URL + "/new/", | ||
data=json.dumps({ | ||
"institutions": [ | ||
"institution a; type A; exogenous event ex_a(A); inst event in_a(A); initially pow(ex_a(A)), perm(ex_a(A)), perm(in_a(A));", | ||
"institution c; type A; exogenous event ex_b(A); initially pow(ex_b(A));"], | ||
"bridges": [ | ||
"bridge b; source a; sink c; in_a(A) xgenerates ex_b(A); initially gpow(a, ex_b(A), c);"], | ||
"types": {"A": ["alpha", "beta"]}, | ||
"query": ["ex_a(alpha)", "ex_a(beta)"] | ||
}), | ||
headers={'Content-Type': 'application/json'}) | ||
assert (query_response.status_code == 201) | ||
|
||
json_resp = query_response.json() | ||
time.sleep(3) | ||
response = requests.get(INSTAL_URL + "/model/{}/grounding/{}/query/{}/output/1/".format( | ||
json_resp.get("grounding", {}).get("model", {}).get("id"), json_resp.get("grounding", {}).get("id"), | ||
json_resp.get("id") | ||
) | ||
, | ||
headers={'Accept': 'text/plain'}) | ||
|
||
assert (response.status_code == 200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
INSTAL_URL = "http://127.0.0.1:5000" | ||
from unittest import TestCase | ||
import requests | ||
import simplejson as json | ||
|
||
class Smoke(TestCase): | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM python:3.5.3 | ||
COPY . /code | ||
WORKDIR /code | ||
RUN apt-get update && apt-get install -y \ | ||
libpq-dev \ | ||
libstdc++6 \ | ||
libgcc-4.9-dev \ | ||
g++ \ | ||
gcc | ||
RUN pip install . | ||
RUN pip install -i http://127.0.0.1:6789/ instal | ||
RUN pip install -r instalrest/requirements.txt | ||
CMD ["python", "instalrest/v1/api.py"] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import os | ||
from flask import Flask, request, make_response | ||
from flask_restful import Resource, Api, abort | ||
from playhouse.shortcuts import model_to_dict | ||
from instalrest.v1 import models | ||
import simplejson as json | ||
import instalrest | ||
from instalrest.instalcelery.instalcelery import execute_instal_async, get_instal_inspect, celery_app | ||
from peewee import DoesNotExist | ||
app = Flask(__name__) | ||
api = Api(app) | ||
|
||
def make_response_json(data, code): | ||
rq = make_response(data,code) | ||
rq.headers["Content-Type"] = 'application/json' | ||
return rq | ||
|
||
def make_response_text(data, code): | ||
rq = make_response(data, code) | ||
rq.headers["Content-Type"] = 'text/plain' | ||
return rq | ||
|
||
class InstALModelList(Resource): | ||
@api.representation('application/json') | ||
def post(self): | ||
model = models.InstALModel.new_from_form_data(request.get_json()) | ||
return make_response_json(model.to_json(), 201) | ||
|
||
@api.representation('application/json') | ||
def get(self): | ||
model = list(map(model_to_dict, models.InstALModel.select())) | ||
return make_response_json(json.dumps(model), 200) | ||
|
||
class InstALModelInspect(Resource): | ||
@api.representation('application/json') | ||
def get(self, model_id): | ||
try: | ||
model = models.InstALModel.get(id=model_id) | ||
return make_response_json(json.dumps(get_instal_inspect(model_id)),200) | ||
except DoesNotExist as e: | ||
abort(404) | ||
|
||
|
||
class InstALModel(Resource): | ||
@api.representation('application/json') | ||
def get(self, model_id): | ||
try: | ||
model = models.InstALModel.get(id=model_id) | ||
return make_response_json(model.to_json(), 200) | ||
except DoesNotExist as e: | ||
abort(404) | ||
|
||
class InstALGroundingList(Resource): | ||
@api.representation('application/json') | ||
def get(self, model_id): | ||
try: | ||
grounding = list(map(model_to_dict, models.InstALGrounding.select().where( | ||
models.InstALGrounding.model == model_id))) | ||
return make_response_json(json.dumps(grounding),200) | ||
except DoesNotExist as e: | ||
abort(404) | ||
|
||
@api.representation('application/json') | ||
def post(self, model_id): | ||
try: | ||
grounding = models.InstALGrounding.new_from_form_data(request.get_json(), model_id) | ||
return make_response_json(grounding.to_json(),201) | ||
except DoesNotExist as e: | ||
abort(404) | ||
|
||
|
||
class InstALGrounding(Resource): | ||
@api.representation('application/json') | ||
def get(self, model_id, grounding_id): | ||
try: | ||
grounding = models.InstALGrounding.get(id=grounding_id) | ||
return make_response_json(grounding.to_json(),201) | ||
except DoesNotExist as e: | ||
abort(404) | ||
|
||
class InstALQueryList(Resource): | ||
@api.representation('application/json') | ||
def get(self, model_id, grounding_id): | ||
query = list(map(model_to_dict, models.InstALQuery.select().where( | ||
models.InstALQuery.grounding == grounding_id))) | ||
return make_response_json(json.dumps(query),200) | ||
|
||
@api.representation('application/json') | ||
def post(self, model_id, grounding_id): | ||
new_query = models.InstALQuery.new_from_form_data(request.get_json(), grounding_id) | ||
execute_instal_async.delay(new_query.id) | ||
return make_response_json(new_query.to_json(),201) | ||
|
||
|
||
class InstALQuery(Resource): | ||
@api.representation('application/json') | ||
def get(self, model_id, grounding_id, query_id): | ||
try: | ||
query = models.InstALQuery.get(id=query_id) | ||
return make_response_json(query.to_json(),200) | ||
except DoesNotExist as e: | ||
abort(404) | ||
|
||
class InstALOutput(Resource): | ||
@api.representation('application/json') | ||
def get(self, model_id, grounding_id, query_id, answer_set_id): | ||
query = models.InstALQuery.get(id=query_id) | ||
try: | ||
data, mimetype = query.output_from_form_data(request, answer_set_number=answer_set_id) | ||
except query.AnswerSetNotFound as e: | ||
abort(404) | ||
return | ||
except query.AnswerSetRepresentationNotFound as e: | ||
abort(417) | ||
return | ||
if mimetype == 'application/json': | ||
rq = make_response_json(data, 200) | ||
elif mimetype == 'text/plain': | ||
rq = make_response_text(data,200) | ||
else: | ||
abort(417) | ||
return | ||
return rq | ||
|
||
class InstALNew(Resource): | ||
@api.representation('application/json') | ||
def post(self): | ||
new_model = models.InstALModel.new_from_form_data(request.get_json()) | ||
new_grounding = models.InstALGrounding.new_from_form_data(request.get_json(),new_model.id) | ||
new_query = models.InstALQuery.new_from_form_data(request.get_json(),new_grounding.id) | ||
execute_instal_async.delay(new_query.id) | ||
return make_response_json(json.dumps(model_to_dict(new_query)),201) | ||
|
||
|
||
class Up(Resource): | ||
@api.representation('application/json') | ||
def get(self): | ||
return make_response_json(json.dumps({"status" : "ok", | ||
"instalrest_version" : instalrest.__version__, | ||
"total_queries" : models.InstALQuery.select().count(), | ||
"total_groundings" : models.InstALGrounding.select().count(), | ||
"total_models" : models.InstALModel.select().count() | ||
}), 200) | ||
|
||
def add_resources(api): | ||
api.add_resource(Up, '/_up') | ||
|
||
api.add_resource(InstALModelList, '/model/') | ||
api.add_resource(InstALModel, '/model/<int:model_id>/') | ||
api.add_resource(InstALModelInspect, '/model/<int:model_id>/inspect/') | ||
api.add_resource(InstALGroundingList, '/model/<int:model_id>/grounding/') | ||
api.add_resource(InstALGrounding, '/model/<int:model_id>/grounding/<int:grounding_id>/') | ||
api.add_resource(InstALQueryList, '/model/<int:model_id>/grounding/<int:grounding_id>/query/') | ||
api.add_resource(InstALQuery, '/model/<int:model_id>/grounding/<int:grounding_id>/query/<int:query_id>/') | ||
api.add_resource(InstALOutput, | ||
'/model/<int:model_id>/grounding/<int:grounding_id>/query/<int:query_id>/output/<int:answer_set_id>/') | ||
|
||
api.add_resource(InstALNew, '/new/') | ||
api = add_resources(api) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
docker run --name instal-rest -e POSTGRES_PASSWORD=instal -e POSTGRES_USER=instal -p 5432:5432 -e POSTGRES_DB=instal-rest-v1 postgres | ||
|
Oops, something went wrong.