Revision 61471
Added by Sandro La Bruzzo over 2 years ago
modules/dli-api/branches/ES_7/v3/responseModel.py | ||
---|---|---|
1 |
from pydantic import BaseModel, Schema |
|
2 |
from typing import Dict, List |
|
3 |
from datetime import datetime |
|
4 |
from fastapi import HTTPException |
|
5 |
import logging |
|
6 |
from time import time |
|
7 |
|
|
8 |
rels = dict(issupplementto="IsSupplementTo", issupplementedby="IsSupplementedBy", references="References", |
|
9 |
isreferencedby="IsReferencedBy") |
|
10 |
|
|
11 |
class LinkProvider(BaseModel): |
|
12 |
name:str = Schema(None, title= "The name of the Provider that provides the links", max_length= 300) |
|
13 |
totalRelationships:int=Schema(None, title= "The number of links that It provides") |
|
14 |
|
|
15 |
class LinkPublisher(BaseModel): |
|
16 |
name:str = Schema(None, title= "The name of the Publisher that provides the links", max_length= 300) |
|
17 |
totalRelationships:int=Schema(None, title= "The number of links that It provides") |
|
18 |
|
|
19 |
|
|
20 |
class IdentifierType(BaseModel): |
|
21 |
ID: str = None |
|
22 |
IDScheme:str = None |
|
23 |
IDURL:str= None |
|
24 |
|
|
25 |
class ScholixProviderType(BaseModel): |
|
26 |
name:str = None |
|
27 |
identifier:List[IdentifierType] = [] |
|
28 |
|
|
29 |
class RelationshipType(BaseModel): |
|
30 |
Name:str |
|
31 |
SubType:str = None |
|
32 |
SubTypeSchema:str = None |
|
33 |
|
|
34 |
class CreatorType(BaseModel): |
|
35 |
Name: str |
|
36 |
Identifier:List[IdentifierType] = [] |
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
class ScholixItemType(BaseModel): |
|
41 |
Identifier:IdentifierType = None |
|
42 |
Title:str = None |
|
43 |
Type:str |
|
44 |
Creator:List[CreatorType] = [] |
|
45 |
PublicationDate:str = None |
|
46 |
Publisher:List[ScholixProviderType] =[] |
|
47 |
|
|
48 |
class ScholixType(BaseModel): |
|
49 |
HarvestDate:str=None |
|
50 |
LicenseURL:str=None |
|
51 |
LinkProvider:List[ScholixProviderType] =[] |
|
52 |
LinkPublicationDate: str = None |
|
53 |
RelationshipType:RelationshipType |
|
54 |
source:ScholixItemType |
|
55 |
target:ScholixItemType |
|
56 |
|
|
57 |
|
|
58 |
class PageResultType(BaseModel): |
|
59 |
currentPage:int |
|
60 |
totalLinks:int |
|
61 |
totalPages:int |
|
62 |
result:List[ScholixType] = [] |
|
63 |
|
|
64 |
|
|
65 |
def get_scholix_resource(item): |
|
66 |
title = '' |
|
67 |
if 'title' in item: |
|
68 |
title = item.title |
|
69 |
if title is not None and len(title): |
|
70 |
if title[0] == '"' and title[-1] == '"': |
|
71 |
title = title[1:-1] |
|
72 |
identifier = [dict(ID=x.identifier, IDScheme=x.schema, IDURL=x.url) for x in |
|
73 |
item.identifier] |
|
74 |
identifier = identifier[0] |
|
75 |
creator = [] |
|
76 |
if 'creator' in item and item.creator is not None: |
|
77 |
creator = [dict(Name=x.name) for x in item.creator] |
|
78 |
publicationDate = None |
|
79 |
if 'publicationDate' in item: |
|
80 |
publicationDate = item.publicationDate |
|
81 |
publisher = [] |
|
82 |
if 'publisher' in item and item.publisher is not None: |
|
83 |
publisher = [dict(name= x.name) for x in item.publisher if x.name is not None] |
|
84 |
c_type = item.objectType |
|
85 |
if item.objectType == 'publication': |
|
86 |
c_type = 'literature' |
|
87 |
|
|
88 |
resource = dict(Title=title, Identifier=identifier, Creator=creator, PublicationDate= publicationDate, Publisher = publisher, Type= c_type) |
|
89 |
|
|
90 |
return resource |
|
91 |
|
|
92 |
|
|
93 |
def convert_response(response): |
|
94 |
now = datetime.now() |
|
95 |
log = logging.getLogger("scholexplorer") |
|
96 |
start = time() |
|
97 |
for item in response.hits: |
|
98 |
current_item = {'LinkPublicationDate': item.publicationDate, 'HarvestDate': item.publicationDate, |
|
99 |
"LinkProvider": []} |
|
100 |
for linkProvider in item.linkprovider: |
|
101 |
current_item['LinkProvider'].append(ScholixProviderType(name=linkProvider.name, |
|
102 |
identifier=[IdentifierType(ID=x.identifier, IDScheme=x.schema) for x in |
|
103 |
linkProvider.identifiers])) |
|
104 |
|
|
105 |
rel_sub_type = rels.get(item.relationship.name.lower(), "IsRelatedTo") |
|
106 |
current_item['RelationshipType'] = dict(Name=rel_sub_type, SubType=item.relationship.name, |
|
107 |
SubTypeSchema=item.relationship.schema) |
|
108 |
|
|
109 |
current_item['source'] = get_scholix_resource(item.source) |
|
110 |
current_item['target'] = get_scholix_resource(item.target) |
|
111 |
|
|
112 |
yield current_item |
|
113 |
end = time() |
|
114 |
log.debug("response converted in {} ms".format(end-start)) |
|
115 |
|
|
116 |
def create_response(response, current_page): |
|
117 |
log = logging.getLogger("scholexplorer") |
|
118 |
if current_page > 9999: |
|
119 |
raise HTTPException(status_code=400,detail="MAX NUMBER OF PAGE REACHED") |
|
120 |
|
|
121 |
start = time() |
|
122 |
|
|
123 |
|
|
124 |
result = {'totalLinks': response.hits.total.value, 'currentPage': current_page /100, |
|
125 |
'totalPages': 1 + response.hits.total.value / 100, 'result': []} |
|
126 |
result['result'] = convert_response(response) |
|
127 |
end = time() |
|
128 |
|
|
129 |
log.debug("response created in {} ms".format(end-start)) |
|
130 |
return result |
modules/dli-api/branches/ES_7/v3/api_v3.py | ||
---|---|---|
1 |
from fastapi import FastAPI, Query,HTTPException |
|
2 |
from pydantic import BaseModel, Schema |
|
3 |
from typing import Dict, List |
|
4 |
from v3.responseModel import * |
|
5 |
from eu.dnetlib.ScholixConnector import ScholixConnector |
|
6 |
from datetime import datetime |
|
7 |
import time |
|
8 |
import logging |
|
9 |
|
|
10 |
|
|
11 |
subapi_v3 = FastAPI(title="Scholexplorer API 3.0", |
|
12 |
version="3.0.0", |
|
13 |
description="scholexplorer API version 3.0",openapi_prefix="/v3", docs_url="/ui/") |
|
14 |
|
|
15 |
req_fields = ['sourcePid', 'targetPid', 'sourcePublisher', 'targetPublisher', 'linkProvider'] |
|
16 |
|
|
17 |
@subapi_v3.get("/LinkProvider", response_model=List[LinkProvider], tags=['LinkProvider : Operation related to the Link Provider'], description="Return a list of link provider and relative number of relations",summary="Get All Link Providers") |
|
18 |
def linkProvider(name: str = Query(None, title="name", description="Filter the link provider by a name")): |
|
19 |
s = ScholixConnector() |
|
20 |
|
|
21 |
return s.list_datasources(name) |
|
22 |
|
|
23 |
@subapi_v3.get("/LinkPublisher/inSource", response_model=List[LinkPublisher], |
|
24 |
tags=['LinkPublisher : Operation related to the Link Publisher'], |
|
25 |
description="Return a List of all Publishers that provide source objects in Scholix links and the total number of links where the source object comes from this publisher", |
|
26 |
summary="Get All Publishers that provide source object") |
|
27 |
def linkPublisherInSource(name: str = Query(None, title="name", description="Filter the link publisher by a name")): |
|
28 |
s = ScholixConnector() |
|
29 |
return s.list_publisher('source',name) |
|
30 |
|
|
31 |
@subapi_v3.get("/LinkPublisher/inTarget", response_model=List[LinkPublisher], |
|
32 |
tags=['LinkPublisher : Operation related to the Link Publisher'], |
|
33 |
description="Return a List of all Publishers that provide target objects in Scholix links and the total number of links where the target object comes from this publisher", |
|
34 |
summary="Get All Publishers that provide target object") |
|
35 |
def linkPublisherInTarget(name: str = Query(None, title="name", description="Filter the link publisher by a name")): |
|
36 |
s = ScholixConnector() |
|
37 |
return s.list_publisher('target',name) |
|
38 |
|
|
39 |
|
|
40 |
@subapi_v3.get("/Links", response_model=PageResultType, tags=['Links : Operation related to the Scholix Links'], |
|
41 |
response_description="A list of List of scholix Links following the schema <a href='https://github.com/scholix/schema/tree/master/json/v3/schema.json'>https://github.com/scholix/schema/tree/master/json/v3/schema.json</a>", |
|
42 |
description="""Return a List of scholix Links, this method <b>require one of the following parameters </b> (<b>sourcePid, targetPid, sourcePublisher,\ |
|
43 |
\ targetPublisher, linkProvider</b>) all parameters can be combined""",summary="Get Scholix Links") |
|
44 |
def links( |
|
45 |
linkProvider: str = Query(None, title="linkProvider", description="Filter Scholix relationships collected from a LinkProvider"), |
|
46 |
targetPid: str = Query(None, title="targetPid", description="Filter Scholix relationships having a target pid"), |
|
47 |
targetPublisher: str = Query(None, title="targetPublisher", description="Filter Scholix relationships having a target published in a Publisher named targetPublisher"), |
|
48 |
targetPidType: str = Query(None, title="targetPidType", description="Filter Scholix relationships having a target pid type"), |
|
49 |
targetType: str = Query(None, title="targetType", description="Filter Scholix relationships having a target type (literature, dataset, unknown)"), |
|
50 |
sourceType: str = Query(None, title="sourceType", description="Filter Scholix relationships having a source type (literature, dataset, unknown)"), |
|
51 |
sourcePid: str = Query(None, title="sourcePid", description="Filter Scholix relationships having a source pid"), |
|
52 |
sourcePublisher: str = Query(None, title="sourcePublisher", description="Filter Scholix relationships having a source published in a Publisher named sourcePublisher"), |
|
53 |
sourcePidType: str = Query(None, title="sourcePidType", description="Filter Scholix relationships having a source pid type"), |
|
54 |
harvestedAfter: str = Query(None, title="harvestedAfter", description="Filter scholix Links having collected after this date"), |
|
55 |
page:int= Query(None, title="page", description="select page of result"), |
|
56 |
): |
|
57 |
|
|
58 |
log = logging.getLogger("scholexplorer") |
|
59 |
|
|
60 |
log.debug("request Link page") |
|
61 |
if not (sourcePid or targetPid or sourcePublisher or targetPublisher or linkProvider): |
|
62 |
raise HTTPException(status_code=400,detail='The method requires one of the following parameters: sourcePid, targetPid, sourcePublisher, targetPublisher, linkProvider') |
|
63 |
|
|
64 |
s = ScholixConnector() |
|
65 |
m_page = 0 |
|
66 |
if page: |
|
67 |
m_page= page*100 |
|
68 |
|
|
69 |
return create_response(s.links(provider = linkProvider, s_pid=sourcePid, t_pid=targetPid, s_publisher=sourcePublisher, |
|
70 |
t_publisher=targetPublisher, s_pid_type=sourcePidType, |
|
71 |
t_pid_type=targetPidType, page=m_page, target_Type=targetType, source_Type=sourceType),m_page) |
|
72 |
|
Also available in: Unified diff
added api v3 version to be fully compliant with scholix 3.0 data format