Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(verbose xml upload): use v option to print verbose output in XML…
… upload (DSP-1797) (#70) * Use v option to print imported resources * Add comments and reformat code
- Loading branch information
irinaschubert
committed
Jul 26, 2021
1 parent
c255845
commit b1f56a1
Showing
4 changed files
with
340 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,49 @@ | ||
import os | ||
|
||
import requests | ||
|
||
from .helpers import BaseError | ||
|
||
import os | ||
|
||
class Sipi: | ||
def on_api_error(res): | ||
""" | ||
Checks for any API errors | ||
Args: | ||
res: the response from the API which is checked, usually in JSON format | ||
def __init__(self, sipiserver: str, token: str): | ||
self.sipiserver = sipiserver | ||
self.token = token | ||
Returns: | ||
Knora Error that is being raised | ||
""" | ||
|
||
def on_api_error(self, res): | ||
""" | ||
Method to check for any API errors | ||
:param res: The input to check, usually JSON format | ||
:return: Possible KnoraError that is being raised | ||
""" | ||
if res.status_code != 200: | ||
raise BaseError("SIPI-ERROR: status code=" + str(res.status_code) + "\nMessage:" + res.text) | ||
|
||
if res.status_code != 200: | ||
raise BaseError("SIPI-ERROR: status code=" + str(res.status_code) + "\nMessage:" + res.text) | ||
if 'error' in res: | ||
raise BaseError("SIPI-ERROR: API error: " + res.error) | ||
|
||
if 'error' in res: | ||
raise BaseError("SIPI-ERROR: API error: " + res.error) | ||
|
||
class Sipi: | ||
"""Represents the Sipi instance""" | ||
|
||
def __init__(self, sipi_server: str, token: str): | ||
self.sipi_server = sipi_server | ||
self.token = token | ||
|
||
def upload_bitstream(self, filepath): | ||
print(f"filepath=${os.path.basename(filepath)} (${filepath})") | ||
with open(filepath, 'rb') as bitstreamfile: | ||
files = { | ||
'file': (os.path.basename(filepath), bitstreamfile), | ||
} | ||
req = requests.post(self.sipiserver + "/upload?token=" + self.token, | ||
files=files) | ||
self.on_api_error(req) | ||
""" | ||
Uploads a bitstream to the Sipi server | ||
Args: | ||
filepath: path to the file, could be either absolute or relative | ||
Returns: | ||
API response | ||
""" | ||
with open(filepath, 'rb') as bitstream_file: | ||
files = {'file': (os.path.basename(filepath), bitstream_file), } | ||
req = requests.post(self.sipi_server + "/upload?token=" + self.token, files=files) | ||
on_api_error(req) | ||
print(f'Uploaded file {filepath}') | ||
res = req.json() | ||
return res |
Oops, something went wrong.