Project

General

Profile

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

    
9

    
10
class LinkProvider(BaseModel):
11
    name:str = Schema(None, title= "The name of the Provider that provides the links", max_length= 300)
12
    totalRelationships:int=Schema(None, title= "The number of links that It provides")
13

    
14
class LinkPublisher(BaseModel):
15
    name:str = Schema(None, title= "The name of the Publisher that provides the links", max_length= 300)
16
    totalRelationships:int=Schema(None, title= "The number of links that It provides")
17

    
18

    
19
class IdentifierType(BaseModel):
20
    identifier: str = None
21
    schema_str:str = Schema(None, alias="schema")
22
    
23

    
24
class ScholixProviderType(BaseModel):
25
    name:str
26
    identifiers:List[IdentifierType] = []
27

    
28
class RelationshipType(BaseModel):
29
    name:str
30
    schema_str:str = Schema(None, alias="schema")
31
    inverseRelationship:str = None
32

    
33
class CreatorType(BaseModel):
34
    name: str
35
    identifiers:List[IdentifierType] = []
36
    
37
class ScholixObjectType(BaseModel):
38
    subtype:str = None
39
    type:str = None
40

    
41
class ScholixItemType(BaseModel):
42
    identifiers:List[IdentifierType] = []
43
    title:str = None
44
    objectType:str 
45
    creators:List[CreatorType] = []    
46
    publisher:List[ScholixProviderType] =[] 
47
    objectProvider: List[ScholixProviderType] =[]  
48

    
49
class ScholixType(BaseModel):
50
    linkProvider:List[ScholixProviderType] =[]    
51
    publicationDate:str =None
52
    relationship:RelationshipType=None
53
    source:ScholixItemType=None
54
    target:ScholixItemType=None
55
    
56
def convert_response(response):
57
    log = logging.getLogger("scholexplorer")
58

    
59
    
60
    for item in response.hits:
61
        result = item.__dict__['_d_']
62
        result['linkProvider'] = result.pop('linkprovider')
63
        if 'creator' in result['source']:
64
            result['source']['creators']=result['source'].pop('creator')
65
        result['source']['identifiers']=result['source'].pop('identifier')
66
        result['source']['objectProvider'] = [s['provider'] for s in result['source'].get('collectedFrom',[])]
67
        if 'creator' in result['target']:
68
            result['target']['creators']=result['target'].pop('creator')
69
        result['target']['identifiers']=result['target'].pop('identifier')
70
        if 'collectedFrom' in result['target']:
71
            result['target']['objectProvider'] = [s['provider'] for s in result['target'].get('collectedFrom',[])]
72
        else:
73
            result['target']['objectProvider'] = []
74
        yield  result
(3-3/3)