Skip to content

Commit

Permalink
add suite class
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Sep 23, 2024
1 parent 2c10d54 commit 91b9993
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyenzyme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .model import * # noqa: F403
from .sbml import to_sbml, read_sbml # noqa: F401
from .suite import EnzymeMLSuite # noqa: F401
from .tabular import to_pandas, read_csv, read_excel, from_dataframe # noqa: F401

__all__ = [
Expand Down
72 changes: 72 additions & 0 deletions pyenzyme/suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json

import httpx
import rich

import pyenzyme as pe


class EnzymeMLSuite:
"""
A suite for interacting with the EnzymeML service.
Attributes:
client (httpx.Client): The HTTP client for making requests to the EnzymeML service.
"""

def __init__(self):
"""
Initializes the EnzymeMLSuite with a base URL for the EnzymeML service.
"""
self.client = httpx.Client(base_url="http://localhost:13452")

def get_current(self) -> pe.EnzymeMLDocument:
"""
Retrieves the current EnzymeML document from the service.
Returns:
EnzymeMLDocument: The current EnzymeML document.
Raises:
httpx.HTTPStatusError: If the request to the service fails.
"""

try:
response = self.client.get("/docs/:current")
except httpx.ConnectError:
raise ConnectionError(
"Could not connect to the EnzymeML suite. Make sure it is running."
)

response.raise_for_status()

content = response.json()["data"]["content"]

if not isinstance(content["references"], list):
content["references"] = []

return pe.EnzymeMLDocument.model_validate(content)

def update_current(self, doc: pe.EnzymeMLDocument):
"""
Updates the current EnzymeML document on the service.
Args:
doc (EnzymeMLDocument): The EnzymeML document to update.
Raises:
httpx.HTTPStatusError: If the request to the service fails.
"""
try:
response = self.client.put(
"/docs/:current",
json=json.loads(doc.model_dump_json()),
)
except httpx.ConnectError:
raise ConnectionError(
"Could not connect to the EnzymeML suite. Make sure it is running."
)

response.raise_for_status()

rich.print("[bold green]Document updated successfully![/]")
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ loguru = "^0.7.2"
rdflib = "7.0.0"
sympy = "^1.12.1"
pymetadata = "^0.4.4"
httpx = "^0.27.2"

[tool.poetry.group.neo4j.dependencies]
neo4j = "^5.21.0"
Expand Down

0 comments on commit 91b9993

Please sign in to comment.