22import requests
33
44from dataverse import Dataverse
5- from exceptions import UnauthorizedError , ConnectionError
5+ import exceptions
66from utils import get_elements
77
88
99class Connection (object ):
1010
1111 def __init__ (self , host , token ):
12- # Connection Properties
1312 self .token = token
1413 self .host = host
1514 self .sd_uri = "https://{host}/dvn/api/data-deposit/v1.1/swordv2/service-document" .format (host = self .host )
@@ -25,12 +24,41 @@ def connect(self):
2524 resp = requests .get (self .sd_uri , auth = self .auth )
2625
2726 if resp .status_code == 403 :
28- raise UnauthorizedError ('The credentials provided are invalid.' )
27+ raise exceptions . UnauthorizedError ('The credentials provided are invalid.' )
2928 elif resp .status_code != 200 :
30- raise ConnectionError ('Could not connect to the Dataverse' )
29+ raise exceptions . ConnectionError ('Could not connect to the Dataverse' )
3130
3231 self .service_document = etree .XML (resp .content )
33-
32+
33+ def create_dataverse (self , alias , name , email , parent = ':root' ):
34+ resp = requests .post (
35+ 'https://{0}/api/dataverses/{1}' .format (self .host , parent ),
36+ json = {
37+ 'alias' : alias ,
38+ 'name' : name ,
39+ 'dataverseContacts' : [{'contactEmail' : email }],
40+ },
41+ params = {'key' : self .token },
42+ )
43+
44+ if resp .status_code == 404 :
45+ raise exceptions .DataverseNotFoundError ('Dataverse {0} was not found.' .format (parent ))
46+ elif resp .status_code != 201 :
47+ raise exceptions .OperationFailedError ('{0} Dataverse could not be created.' .format (name ))
48+
49+ def delete_dataverse (self , alias ):
50+ resp = requests .delete (
51+ 'https://{0}/api/dataverses/{1}' .format (self .host , alias ),
52+ params = {'key' : self .token },
53+ )
54+
55+ if resp .status_code == 401 :
56+ raise exceptions .UnauthorizedError ('Delete Dataverse unauthorized.' )
57+ elif resp .status_code == 404 :
58+ raise exceptions .DataverseNotFoundError ('Dataverse {0} was not found.' .format (alias ))
59+ elif resp .status_code != 200 :
60+ raise exceptions .OperationFailedError ('Dataverse {0} could not be deleted.' .format (alias ))
61+
3462 def get_dataverses (self , refresh = False ):
3563 if refresh :
3664 self .connect ()
0 commit comments