Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion application/app_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Import modules
from flask import Flask
from flask_restplus import Api, Resource
from flask_restx import Api, Resource

# Define the application as a Flask app with the name defined by __name__ (i.e. the name of the current module)
# Most tutorials define application as "app", but I have had issues with this when it comes to deployment,
Expand All @@ -27,5 +27,7 @@ def get(self):

# Allows app to be run in debug mode
if __name__ == '__main__':
#from waitress import serve
application.debug = True # Enable debugging mode
#serve(application, host = "127.0.0.1", port=5000)
application.run(host="127.0.0.1", port=5000) # Specify a host and port fot the app
2 changes: 1 addition & 1 deletion application/app_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Import modules
from flask import Flask
from flask_restplus import Api, Resource
from flask_restx import Api, Resource

# Define the application as a Flask app with the name defined by __name__ (i.e. the name of the current module)
# Most tutorials define application as "app", but I have had issues with this when it comes to deployment,
Expand Down
63 changes: 53 additions & 10 deletions application/app_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"""

# Import modules
from flask import Flask
from flask_restplus import Api, Resource
import _____
from flask import Flask, make_response
from flask_restx import Api, Resource, reqparse
import requests
from dicttoxml import dicttoxml

# Define the application as a Flask app with the name defined by __name__ (i.e. the name of the current module)
# Most tutorials define application as "app", but I have had issues with this when it comes to deployment,
Expand All @@ -15,6 +16,28 @@
# Define the API as api
api = Api(app = application)

# Define a parser:
parser = reqparse.RequestParser()
parser.add_argument('content-type',
type = str,
help = 'Accepted:\n- application/json\n- text/xml')

# Add API representations:
# For xml:
@api.representation('text/xml')
def xml(data, code, headers):
data = dicttoxml(data)
resp = make_response(data, code)
resp.headers['Content-Type'] = 'text/xml'
return resp

# For json:
@api.representation('application/json')
def json(data, code, headers):
resp = make_response(data, code)
resp.headers['Content-Type'] = 'application/json'
return resp

# Define a name-space to be read Swagger UI which is built in to Flask-RESTPlus
# The first variable is the path of the namespace the second variable describes the space
hello_space = api.namespace('hello', description='Simple API that returns a greeting')
Expand All @@ -33,17 +56,37 @@ def get(self, name):
return {
"My name is" : name
}


# Exercise 2 part 1:
vv_space = api.namespace('VariantValidator', description='VariantValidator APIs')
@vv_space.route("/variantvalidator/_____")
@vv_space.route("/variantvalidator/<string:genome_build>/<string:var_desc>/<string:select_transcripts>")
class VariantValidatorClass(Resource):
def get(self, _____):

@api.doc(parser = parser)

def get(self, genome_build, var_desc, select_transcripts):

# Make a request to the curent VariantValidator rest-API
url = _____
validation = _____
content = _____
return _____
url = f"https://rest.variantvalidator.org/{genome_build}/{var_desc}/{select_transcripts}"
validation = requests.get(url)
content = validation.json()

# Collect arguments from the parser:
args = parser.parse_args()

# If content-type specified - return the content object in that format:
if args['content-type'] == 'application/json':
return json(content, 200, None)

elif args['content-type'] == 'text/xml':
return xml(content, 200, None)

# Return in the default format:
else:
return content

# Exercise 2 part 3:


# Allows app to be run in debug mode
if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion application/app_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Import modules
from flask import Flask, make_response
from flask_restplus import Api, Resource
from flask_restx import Api, Resource
import requests
from dicttoxml import dicttoxml

Expand Down
2 changes: 1 addition & 1 deletion application/app_v5.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Import modules
from flask import Flask, make_response
from flask_restplus import Api, Resource, reqparse
from flask_restx import Api, Resource, reqparse
import requests
from dicttoxml import dicttoxml

Expand Down
2 changes: 1 addition & 1 deletion application/app_v6.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Import modules
from flask import Flask, make_response, request
from flask_restplus import Api, Resource, reqparse, fields, abort
from flask_restx import Api, Resource, reqparse, fields, abort
import requests
from requests.exceptions import ConnectionError
from dicttoxml import dicttoxml
Expand Down
91 changes: 91 additions & 0 deletions application/my_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# import the requests module
import requests
import urllib.parse

# Create the class
class MyRequests:

def __init__(self):
self.base_url = None
self.url = None

# method that makes the call to the API using the get method:
def request_data(self, prms = None):
return requests.get(self.url, params = prms)

# method that assembles the url to request data from the hello endpoint:
def hello(self):
self.url = f"{self.base_url}hello"
return self.request_data()

# method to get data from the name endpoint:
def name_get(self, name):
self.url = f"{self.base_url}name/{name}"
return self.request_data()

# method to get data from the VariantValidator endpoint: ## Have been quite confused about what the last two bits of exercise 2 are asking for - is it make changes to app v3 to add requesting diff. content types and then to here?
def VV_get(self, gbuild, vdesc, seltrans, content_type = None):

# If want to request as xml:
if content_type == 'xml':
self.url = f"{self.base_url}VariantValidator/variantvalidator/{gbuild}/{vdesc}/{seltrans}?content-type=text/xml"

# If want to request as json:
elif content_type == 'json':
self.url = f"{self.base_url}VariantValidator/variantvalidator/{gbuild}/{vdesc}/{seltrans}?content-type=application/json"

# Else use default url:
else:
self.url = f"{self.base_url}VariantValidator/variantvalidator/{gbuild}/{vdesc}/{seltrans}"

return self.request_data()



if __name__ == "__main__":
mrq = MyRequests()

# Set the base url
mrq.base_url = "http://127.0.0.1:5000/"

### hello endpoint:
# request the data
response_hello = mrq.hello()

# print the 3 response sections
print(response_hello.status_code)
print(response_hello.headers)
print(response_hello.text)
print(response_hello.json())

### name endpoint:
name = 'Tom'
response_nm = mrq.name_get(name)
print(response_nm.status_code)
print(response_nm.headers)
print(response_nm.text)
print(response_nm.json())

## VariantValidator endpoint - left these blank for now:
gbuild = 'hg19'
#gbuild = 'xxx'
vdesc = 'NM_000088.3:c.589G>T'
#vdesc = 'xxx'
seltrans = 'NM_000088.3'
#seltrans = ''
content_type = ''

# Need to do some url parsing as varint descriptions and transcripts can have reserved characters:
url_vdesc = urllib.parse.quote(vdesc)
url_seltrans = urllib.parse.quote(seltrans)

print(url_seltrans, url_vdesc)

response_VV = mrq.VV_get(gbuild, url_vdesc, url_seltrans, content_type)

print(response_VV.status_code)

if content_type == 'xml':
print(response_VV.text)
else:
print(response_VV.json())
9 changes: 6 additions & 3 deletions application/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
flask==1.1.1
flask_restplus
Werkzeug==0.16.1
flask==2.3.0
flask-restx==1.0.0
Werkzeug==2.3.3
requests==2.31.0
urllib
dicttoxml
3 changes: 2 additions & 1 deletion application/rest_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

# Import modules
from flask import Flask, request
from endpoints import api, representations, exceptions, request_parser
from endpoints import api
from utils import representations, exceptions, request_parser
import logging
from logging import handlers
import time
Expand Down
2 changes: 1 addition & 1 deletion application/rest_api/endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask_restplus import Api
from flask_restx import Api

from .hello import api as ns_hello
from .name import api as ns_name
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions application/rest_api/endpoints/hello.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask_restplus import Namespace, Resource
from . import request_parser
from . import representations
from flask_restx import Namespace, Resource
from utils import request_parser
from utils import representations


"""
Expand Down
6 changes: 3 additions & 3 deletions application/rest_api/endpoints/name.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask_restplus import Namespace, Resource
from . import request_parser
from . import representations
from flask_restx import Namespace, Resource
from utils import request_parser
from utils import representations


"""
Expand Down
8 changes: 4 additions & 4 deletions application/rest_api/endpoints/variantvalidator_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from flask_restplus import Namespace, Resource
from . import request_parser
from . import representations
from flask_restx import Namespace, Resource
from utils import request_parser
from utils import representations
import requests
from requests.exceptions import ConnectionError
from . import exceptions
from utils import exceptions

"""
Create a parser object locally
Expand Down
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask_restplus import reqparse
from flask_restx import reqparse


# Create a RequestParser object to identify specific content-type requests in HTTP URLs
Expand Down
4 changes: 4 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: tl1_vv_env
dependencies:
- python==3.12
- pip
2 changes: 1 addition & 1 deletion rest_variantValidator/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# IMPORT FLASK MODULES
from flask import Flask ,request, jsonify, abort, url_for, g, send_file, redirect, Blueprint #, session, g, redirect, , abort, render_template, flash, make_response, abort
from flask_restful import Resource, Api, reqparse, abort, fields, marshal_with
from flask_restx import Resource, Api, reqparse, abort, fields, marshal_with
from vv_flask_restful_swagger import swagger
from flask_log import Logging
from flask_mail import Mail, Message
Expand Down