66from typing import Dict , List
77
88
9- class Crossref ():
10- """Crossref compatibilty layer """
11- def __init__ (self , doi : str ):
12- self .works = Works ()
9+ class Db ():
10+ """Base Db class to derive specialised database classes from """
11+ def __init__ (self , doi : str ) -> None :
12+ self .db = self . init_db ()
1313 self .doi = urljoin ('https://doi.org/' , doi )
1414
15+ def init_db (self ):
16+ """Init database object"""
17+ raise NotImplementedError
18+
19+ def get_book (self ):
20+ """Return book data"""
21+ raise NotImplementedError
22+
23+ def get_chapters (self ):
24+ """Return chapters data"""
25+ raise NotImplementedError
26+
27+
28+ class Crossref (Db ):
29+ """Crossref compatibility layer"""
30+ def init_db (self ):
31+ """Init database object"""
32+ return Works ()
33+
1534 def get_book (self ) -> Dict :
16- """Return the book data associated to the supplied ISBN"""
17- query = self .works .doi (self .doi )
35+ """Return book data"""
36+ query = self .db .doi (self .doi )
37+
38+ if not query :
39+ raise ValueError (f"No book data associated to the DOI { self .doi } "
40+ "found on the database Crossref" )
41+
1842 data = {"title" : query .get ("title" )[0 ],
1943 "doi" : query .get ("DOI" )}
2044 return data
2145
2246 def get_chapters (self , book : Dict ) -> List :
23- """Returns a chapter data related to the book"""
24- query = self .works .filter (container_title = book .get ("title" ),
25- type = 'book-chapter' ) \
26- .select ('DOI' , 'license' , 'author' ,
27- 'title' , 'type' , 'page' ,
28- 'publisher' , 'abstract' )
29-
30- # Assert that at least one DOI have been discovered
47+ """Return chapters data"""
48+ query = self .db .filter (container_title = book .get ("title" ),
49+ type = 'book-chapter' ) \
50+ .select ('DOI' , 'license' , 'author' ,
51+ 'title' , 'type' , 'page' ,
52+ 'publisher' , 'abstract' )
53+
3154 if not query :
32- raise AssertionError ( 'Couldn \' t find any chapter-level DOIs'
33- + ' for the supplied --isbn value' )
55+ raise ValueError ( "No chapter data associated to the DOI"
56+ f" { self . doi } found on the database Crossref" )
3457
3558 chapters = []
3659 for chapter in query :
@@ -57,21 +80,23 @@ def join_author_names(self, chapter_data: Dict) -> str:
5780 return '; ' .join (author_list )
5881
5982
60- class Thoth ():
61- """Thoth compatibilty layer"""
62- def __init__ (self , doi : str ):
63- self . thoth = ThothClient ()
64- self . doi_url = urljoin ( 'https://doi.org/' , doi )
83+ class Thoth (Db ):
84+ """Thoth compatibility layer"""
85+ def init_db (self ):
86+ """Init database object"""
87+ return ThothClient ( )
6588
6689 def get_book (self ) -> Dict :
67- work = self .thoth .work_by_doi (doi = self .doi_url , raw = True )
90+ """Return book data"""
91+ work = self .db .work_by_doi (doi = self .doi , raw = True )
6892 work_dict = json .loads (work )['data' ]['workByDoi' ]
6993
7094 data = {"title" : work_dict .get ("fullTitle" ),
71- "doi" : self .doi_url }
95+ "doi" : self .doi }
7296 return data
7397
7498 def get_chapters (self , book : Dict ) -> List :
99+ """Return chapters data"""
75100 # TODO replace this with a Thoth library method when available
76101 url = 'https://api.thoth.pub/graphql'
77102 query = {"query" : """{ workByDoi (doi: "%s") {
0 commit comments