-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 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 |
---|---|---|
@@ -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![/]") |
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