Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit cd42479

Browse files
committed
feat: added concrete implementations of command for newton api
1 parent 27f4422 commit cd42479

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from dataclasses import dataclass
2+
3+
import requests
4+
from command import CmdBase
5+
6+
7+
@dataclass(frozen=True)
8+
class NewtonResponse:
9+
"""Newton API Response."""
10+
operation: str
11+
expression: str
12+
result: str
13+
14+
15+
class NewtonCmdException(Exception):
16+
"""Base class for Newton Command Exceptions."""
17+
18+
19+
class NewtonCommand(CmdBase):
20+
"""Base class for all the Newton API Commands."""
21+
22+
def __init__(self, operation: str):
23+
super().__init__(operation, 'https://newton.now.sh/api/v2')
24+
25+
def command(self, expr: str) -> NewtonResponse:
26+
"""
27+
Command method for NewtonCommand class.
28+
29+
Args:
30+
expr (str): Mathematical expression to be evaluated.
31+
32+
Returns:
33+
NewtonResponse: Object containing the operation, expression, and result of the evaluated expression.
34+
35+
Raises:
36+
NewtonCmdException: If the HTTP request fails or returns a non-success status code, the exception is raised
37+
with the error message.
38+
"""
39+
# Construct the Request URL
40+
expr_encode = self.url_encode(expr) # URL Encode for Expression
41+
request_url = f"{self.base_url}/{self.operation}/{expr_encode}"
42+
43+
# Make the HTTP GET request
44+
response = requests.get(request_url)
45+
46+
# Check if the request was successful (status code 200)
47+
if response.status_code == 200:
48+
# Deserialize the JSON response into a dictionary
49+
response_data = response.json()
50+
51+
# Extract relevant data from the response
52+
operation = response_data['operation']
53+
expression = response_data['expression']
54+
result = response_data['result']
55+
56+
# Create and return a NewtonResponse object
57+
return NewtonResponse(operation=operation, expression=expression, result=result)
58+
else:
59+
raise NewtonCmdException(f'{response.text}')
60+
61+
62+
newton_simplify = NewtonCommand('simplify').command
63+
newton_factor = NewtonCommand('factor').command
64+
newton_derive = NewtonCommand('derive').command
65+
newton_integrate = NewtonCommand('integrate').command
66+
newton_zeroes = NewtonCommand('zeroes').command
67+
newton_tangent = NewtonCommand('tangent').command
68+
newton_area = NewtonCommand('area').command
69+
newton_cos = NewtonCommand('cos').command
70+
newton_sin = NewtonCommand('sin').command
71+
newton_tan = NewtonCommand('tan').command
72+
newton_arc_cos = NewtonCommand('arccos').command
73+
newton_arc_sin = NewtonCommand('arcsin').command
74+
newton_arc_tan = NewtonCommand('arctan').command
75+
newton_abs = NewtonCommand('abs').command
76+
newton_log = NewtonCommand('log').command

0 commit comments

Comments
 (0)