Project

General

Profile

1
import os
2
from fastapi import FastAPI, Form
3
import logging
4
from starlette.staticfiles import StaticFiles
5
from starlette.responses import FileResponse
6
from eu.dnetlib.es_connector import DLIESConnector
7
import sys
8

    
9
_CURDIR = os.path.dirname(os.path.abspath(__file__))
10

    
11
app = FastAPI()
12

    
13
app.mount('/static', StaticFiles(directory=os.path.join(_CURDIR, 'static' )))
14
base_dnet_url = "http://aggregator-dli.openaire.eu/dli/"
15
pid_resolver = {
16
    "pdb": "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s",
17
    "ncbi-n": "http://www.ncbi.nlm.nih.gov/gquery/?term=%s",
18
    "pmid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
19
    "pmcid": "http://www.ncbi.nlm.nih.gov/pmc/articles/%s",
20
    "pubmedid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
21
    "doi": "http://dx.doi.org/%s",
22
    "genbank": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
23
    "nuccore": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
24
    "swiss-prot": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
25
    "arrayexpress": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
26
    "biomodels": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
27
    "bmrb": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
28
    "ena": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
29
    "geo": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
30
    "ensembl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
31
    "mgi": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
32
    "bind": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
33
    "pride": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
34
    "ddbj": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
35
    "bioproject": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
36
    "embl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
37
    "sra": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
38
}
39

    
40
log = logging.getLogger("scholexplorer-portal")
41
log.setLevel(logging.INFO)
42
fh = logging.StreamHandler(sys.stdout) 
43
fh.setLevel(logging.INFO)
44
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
45
fh.setFormatter(formatter)
46
log.addHandler(fh)
47
log.info("Scholexplorer Portal Restarted")
48

    
49
@app.get('/')
50
def root():
51
    return FileResponse(os.path.join(os.path.join(_CURDIR, 'static' ),'index.html'))
52

    
53
@app.get("/favicon.ico")
54
def favicon():
55
    return FileResponse(os.path.join(os.path.join(_CURDIR, 'static' ),'favicon.ico'))
56

    
57
@app.get("/api/main_page_stats")
58
def main_page_stats():
59
    connector= DLIESConnector()
60
    return dict(result=connector.get_main_page_stats())
61

    
62
@app.post('/api/queryPid/')
63
def query_pid(*, pid:str= Form(...)):   
64
    print (pid) 
65
    connector = DLIESConnector()    
66
    return dict(result=connector.query_by_id(pid))
67

    
68

    
69

    
70
@app.post('/api/post/')
71
def query_post(*, start:int= Form(default=0), action:str=Form(default="query"), query:str=Form(...),filter:str=Form(default=None)):
72
    filter_key = None
73
    result = {}  
74
    if filter:
75
        filter_key = filter
76
    if action == 'query' and query is not None:
77
        connector = DLIESConnector()
78
        try:
79
            result = connector.simple_query(query, start=start, user_filter=filter_key)
80
        except Exception as e:
81
            log.error(e)
82
        return dict(result=result)
83
    else:
84
        return dict(error={'error_code': 'an error occur during query on server'})
85

    
86

    
87
@app.get('/api/item/{identifier}')
88
@app.post('/api/item/{identifier}')
89
def get_item(identifier:str, object_type:str=Form(default=None), start:int=Form(default=0)):
90
    if identifier:        
91
        connector = DLIESConnector()
92
        return dict(result=connector.item_by_id(identifier, object_type, start))
93
    else:
94
        return None
(3-3/3)