Skip to content

Commit

Permalink
[CICO-6] Added toml for PyPi preparation, updated README, fixed add_i…
Browse files Browse the repository at this point in the history
…tem endpoint
  • Loading branch information
jophals committed Nov 26, 2024
1 parent 0039256 commit 14c1106
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Citesphere Connector

Python library to connect to Citephere using its [API](https://documenter.getpostman.com/view/19365454/UVeMJiyx).
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`
39 changes: 39 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"},
{name = "Julian Ophals", email = "[email protected]"},
{name = "Vishnu Vardhan Sanikommu", email = "[email protected]"},
{name = "Ajay Yadav", email = "[email protected]"},
]
maintainers = [
{name = "Julia Damerow", email = "[email protected]"},
{name = "Julian Ophals", email = "[email protected]"},
]
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"
43 changes: 30 additions & 13 deletions src/CitesphereConnector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import urllib.request as urllib2
import json
import base64
import requests
import os


class CitesphereConnector:
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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}")

0 comments on commit 14c1106

Please sign in to comment.