From 14c110695661ad84b4e94304ea5e034eedb6b6a3 Mon Sep 17 00:00:00 2001 From: Julian Ophals Date: Mon, 25 Nov 2024 17:06:41 -0700 Subject: [PATCH] [CICO-6] Added toml for PyPi preparation, updated README, fixed add_item endpoint --- README.md | 9 +++++++- pyproject.toml | 39 ++++++++++++++++++++++++++++++++++ src/CitesphereConnector.py | 43 ++++++++++++++++++++++++++------------ 3 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 pyproject.toml diff --git a/README.md b/README.md index 7948d5c..cd42d25 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ ## Citesphere Connector -Python library to connect to Citephere using its [API](https://documenter.getpostman.com/view/19365454/UVeMJiyx). \ No newline at end of file +Python library to connect to Citephere using its [API](https://documenter.getpostman.com/view/19365454/UVeMJiyx). + + +## SETUP + +Create a python virtual environment outside of this project's root directory `python3 -m venv env` and activate it `source env/bin/activate` + +Navigate to the project root and download package dependencies `pip install -r requirements.txt` diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5916d71 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,39 @@ +[project] +name = "citesphere-connector" +version = "1.0.0" +dependencies = [ + "cfgv", + "distlib", + "filelock", + "identify", + "nodeenv", + "platformdirs", + "pre_commit", + "PyYAML", + "ruff", + "virtualenv", +] +requires-python = ">= 3.9" +authors = [ + {name = "Julia Damerow", email = "jdamerow@asu.edu"}, + {name = "Julian Ophals", email = "jophals@asu.edu"}, + {name = "Vishnu Vardhan Sanikommu", email = "vrsaniko@asu.edu"}, + {name = "Ajay Yadav", email = "ayadav55@asu.edu"}, +] +maintainers = [ + {name = "Julia Damerow", email = "jdamerow@asu.edu"}, + {name = "Julian Ophals", email = "jophals@asu.edu"}, +] +description = "Connect to Citesphere, an application that enables superior management of Zotero citations" +readme = "README.md" +license = {file = "LICENSE"} +keywords = ["cite", "diging", "citesphere", "sphere", "zotero"] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Research Software Engineers, Researchers, Data Scientists, Developers", + "Topic :: Reserach Software Engineering :: Citation Manager", + "Programming Language :: Python", +] + +[project.urls] +"Citesphere API" = "https://documenter.getpostman.com/view/19365454/UVeMJiyx" diff --git a/src/CitesphereConnector.py b/src/CitesphereConnector.py index 927a2d4..e8301be 100644 --- a/src/CitesphereConnector.py +++ b/src/CitesphereConnector.py @@ -1,6 +1,8 @@ import urllib.request as urllib2 import json import base64 +import requests +import os class CitesphereConnector: @@ -34,16 +36,14 @@ def validate(self): def handle_api_params(self): if self.auth_token_object.authType == "oauth": self.auth_token_object.headers = { - "Authorization": "Bearer {}".format(self.auth_token_object.access_token) + "Authorization": f"Bearer {self.auth_token_object.access_token}", } elif self.auth_token_object.authType == "basic": - auth_str = "{}:{}".format( - self.auth_token_object.username, self.auth_token_object.password + auth_str = ( + f"{self.auth_token_object.username}:{self.auth_token_object.password}" ) auth_b64 = base64.b64encode(auth_str.encode("ascii")) - self.auth_token_object.headers = { - "Authorization": "Basic {}".format(auth_b64) - } + self.auth_token_object.headers = {"Authorization": f"Basic {auth_b64}"} def execute_command(self, url): try: @@ -56,6 +56,18 @@ def execute_command(self, url): except Exception as exc: return {"error_message": str(exc)} + def execute_post_request(self, url, data, files): + try: + response = requests.post( + url, headers=self.auth_token_object.headers, data=data, files=files + ) + print(response.status_code) + # Uncomment for debugging response from Citesphere + # print(response.text) + return response + except Exception as exc: + return {"error_message": str(exc)} + def get_user(self): url = f"{self.api}/v1/user" return self.execute_command(url) @@ -105,10 +117,15 @@ def get_collections_by_collection_id(self, zotero_group_id, collection_id): url = f"{self.api}/groups/{zotero_group_id}/collections/{collection_id}/collections" return self.execute_command(url) - def add_item(self, group_id, file_path): - # with open(file_path, "rb") as file: - # files = {"file": file} - # response = requests.post(url, files=files) - - url = f"{self.api}/v1/groups/{group_id}/items/create" - return self.execute_command(url) + def add_item(self, group_id, data, file_path): + try: + with open(file_path, "rb") as file_obj: + files = [(os.path.basename(file_path), file_obj)] + request_files = [ + ("files", (name, file, "application/pdf")) for name, file in files + ] + url = f"{self.api}/v1/groups/{group_id}/items/create" + + return self.execute_post_request(url, data, request_files) + except Exception as e: + print(f"[ERROR] -------- Error during API request with {file_path}: {e}")