Skip to content

Commit 3bd6cce

Browse files
authored
Merge pull request #5 from Codeplain-ai/feat/update-api-expectations-and-formatter
feat: Update API expectations for methods `refactor_source_files_if_needed` and `generate_folder_name_from_functional_requirement`
2 parents b30acad + 55bacfc commit 3bd6cce

File tree

5 files changed

+462
-297
lines changed

5 files changed

+462
-297
lines changed

codeplain_REST_api.py

+75-95
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import requests
32

43

@@ -23,7 +22,7 @@ class MissingResource(Exception):
2322

2423

2524
class PlainSyntaxError(Exception):
26-
pass
25+
pass
2726

2827

2928
class OnlyRelativeLinksAllowed(Exception):
@@ -35,66 +34,62 @@ class LinkMustHaveTextSpecified(Exception):
3534

3635

3736
class CodeplainAPI:
38-
37+
3938
def __init__(self, api_key):
4039
self.api_key = api_key
4140

42-
4341
@property
4442
def api_url(self):
4543
return self._api_url
4644

47-
4845
@api_url.setter
4946
def api_url(self, value):
5047
self._api_url = value
5148

52-
5349
def post_request(self, endpoint_url, headers, payload):
5450
response = requests.post(endpoint_url, headers=headers, json=payload)
5551

5652
try:
5753
response_json = response.json()
5854
except requests.exceptions.JSONDecodeError as e:
59-
print(f"Failed to decode JSON response. Response text: {response.text}")
55+
print(f"Failed to decode JSON response: {e}. Response text: {response.text}")
6056
raise
6157

6258
if response.status_code == requests.codes.bad_request and "error_code" in response_json:
63-
if response_json["error_code"] == 'FunctionalRequirementTooComplex':
64-
raise FunctionalRequirementTooComplex(response_json['message'])
65-
66-
if response_json["error_code"] == 'ConflictingRequirements':
67-
raise ConflictingRequirements(response_json['message'])
68-
69-
if response_json["error_code"] == 'CreditBalanceTooLow':
70-
raise CreditBalanceTooLow(response_json['message'])
71-
72-
if response_json["error_code"] == 'LLMOverloadedError':
73-
raise LLMOverloadedError(response_json['message'])
74-
75-
if response_json["error_code"] == 'MissingResource':
76-
raise MissingResource(response_json['message'])
77-
78-
if response_json["error_code"] == 'PlainSyntaxError':
79-
raise PlainSyntaxError(response_json['message'])
80-
81-
if response_json["error_code"] == 'OnlyRelativeLinksAllowed':
82-
raise OnlyRelativeLinksAllowed(response_json['message'])
83-
84-
if response_json["error_code"] == 'LinkMustHaveTextSpecified':
85-
raise LinkMustHaveTextSpecified(response_json['message'])
59+
if response_json["error_code"] == "FunctionalRequirementTooComplex":
60+
raise FunctionalRequirementTooComplex(response_json["message"])
61+
62+
if response_json["error_code"] == "ConflictingRequirements":
63+
raise ConflictingRequirements(response_json["message"])
64+
65+
if response_json["error_code"] == "CreditBalanceTooLow":
66+
raise CreditBalanceTooLow(response_json["message"])
67+
68+
if response_json["error_code"] == "LLMOverloadedError":
69+
raise LLMOverloadedError(response_json["message"])
70+
71+
if response_json["error_code"] == "MissingResource":
72+
raise MissingResource(response_json["message"])
73+
74+
if response_json["error_code"] == "PlainSyntaxError":
75+
raise PlainSyntaxError(response_json["message"])
76+
77+
if response_json["error_code"] == "OnlyRelativeLinksAllowed":
78+
raise OnlyRelativeLinksAllowed(response_json["message"])
79+
80+
if response_json["error_code"] == "LinkMustHaveTextSpecified":
81+
raise LinkMustHaveTextSpecified(response_json["message"])
8682

8783
response.raise_for_status()
8884

8985
return response_json
9086

91-
9287
def get_plain_source_tree(self, plain_source, loaded_templates):
9388
"""
9489
Builds plain source tree from the given plain text source in Markdown format.
9590
9691
Args:
97-
plain_source (str): A string containing the plain text source to be parsed.
92+
plain_source (str): A string containing the plain text source to be parsed.
9893
loaded_templates (dict): A dictionary containing the loaded templates.
9994
10095
Returns:
@@ -104,128 +99,113 @@ def get_plain_source_tree(self, plain_source, loaded_templates):
10499
Exception: If parsing of plain_source fails.
105100
"""
106101
endpoint_url = f"{self.api_url}/plain_source_tree"
107-
headers = {
108-
"X-API-Key": self.api_key,
109-
"Content-Type": "application/json"
110-
}
111-
112-
payload = {
113-
"plain_source": plain_source,
114-
"loaded_templates": loaded_templates
115-
}
116-
117-
return self.post_request(endpoint_url, headers, payload)
102+
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
118103

104+
payload = {"plain_source": plain_source, "loaded_templates": loaded_templates}
105+
106+
return self.post_request(endpoint_url, headers, payload)
119107

120108
def render_functional_requirement(self, frid, plain_source_tree, linked_resources, existing_files_content):
121109
"""
122-
Renders the content of a functional requirement based on the provided ID,
110+
Renders the content of a functional requirement based on the provided ID,
123111
plain source tree, and existing files' content.
124112
125113
Args:
126114
frid (str): The unique identifier for the functional requirement to be rendered.
127115
plain_source_tree (dict): A dictionary containing the plain source tree.
128-
linked_resources (dict): A dictionary where the keys represent resource names
116+
linked_resources (dict): A dictionary where the keys represent resource names
129117
and the values are the content of those resources.
130118
existing_files_content (dict): A dictionary where the keys represent code base
131119
filenames and the values are the content of those files.
132120
133121
Returns:
134-
str: A string containing the rendered functional requirement, formatted
122+
str: A string containing the rendered functional requirement, formatted
135123
appropriately based on the inputs.
136124
137125
Raises:
138126
ValueError: If the frid is invalid or the necessary plain source tree is not valid.
139127
"""
140128
endpoint_url = f"{self.api_url}/render_functional_requirement"
141-
headers = {
142-
"X-API-Key": self.api_key,
143-
"Content-Type": "application/json"
144-
}
145-
129+
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
130+
146131
payload = {
147132
"frid": frid,
148133
"plain_source_tree": plain_source_tree,
149134
"linked_resources": linked_resources,
150-
"existing_files_content": existing_files_content
135+
"existing_files_content": existing_files_content,
151136
}
152137

153138
return self.post_request(endpoint_url, headers, payload)
154139

155-
156140
def fix_unittests_issue(self, frid, plain_source_tree, linked_resources, existing_files_content, unittests_issue):
157141
endpoint_url = f"{self.api_url}/fix_unittests_issue"
158-
headers = {
159-
"X-API-Key": self.api_key,
160-
"Content-Type": "application/json"
161-
}
162-
142+
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
143+
163144
payload = {
164145
"frid": frid,
165146
"plain_source_tree": plain_source_tree,
166147
"linked_resources": linked_resources,
167148
"existing_files_content": existing_files_content,
168-
"unittests_issue": unittests_issue
149+
"unittests_issue": unittests_issue,
169150
}
170-
171-
return self.post_request(endpoint_url, headers, payload)
172151

152+
return self.post_request(endpoint_url, headers, payload)
173153

174-
def refactor_source_files_if_needed(self, files_to_check, existing_files_content):
154+
def refactor_source_files_if_needed(self, frid, files_to_check, existing_files_content):
175155
endpoint_url = f"{self.api_url}/refactor_source_files_if_needed"
176-
headers = {
177-
"X-API-Key": self.api_key,
178-
"Content-Type": "application/json"
179-
}
180-
156+
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
157+
181158
payload = {
159+
"frid": frid,
182160
"files_to_check": list(files_to_check),
183-
"existing_files_content": existing_files_content
161+
"existing_files_content": existing_files_content,
184162
}
185163

186164
return self.post_request(endpoint_url, headers, payload)
187165

188-
189-
def render_conformance_tests(self, frid, functional_requirement_id, plain_source_tree, linked_resources, existing_files_content):
166+
def render_conformance_tests(
167+
self, frid, functional_requirement_id, plain_source_tree, linked_resources, existing_files_content
168+
):
190169
endpoint_url = f"{self.api_url}/render_conformance_tests"
191-
headers = {
192-
"X-API-Key": self.api_key,
193-
"Content-Type": "application/json"
194-
}
195-
170+
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
171+
196172
payload = {
197173
"frid": frid,
198174
"functional_requirement_id": functional_requirement_id,
199175
"plain_source_tree": plain_source_tree,
200176
"linked_resources": linked_resources,
201-
"existing_files_content": existing_files_content
177+
"existing_files_content": existing_files_content,
202178
}
203-
204-
return self.post_request(endpoint_url, headers, payload)
205179

180+
return self.post_request(endpoint_url, headers, payload)
206181

207-
def generate_folder_name_from_functional_requirement(self, functional_requirement, existing_folder_names):
182+
def generate_folder_name_from_functional_requirement(self, frid, functional_requirement, existing_folder_names):
208183
endpoint_url = f"{self.api_url}/generate_folder_name_from_functional_requirement"
209-
headers = {
210-
"X-API-Key": self.api_key,
211-
"Content-Type": "application/json"
212-
}
213-
184+
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
185+
214186
payload = {
187+
"frid": frid,
215188
"functional_requirement": functional_requirement,
216-
"existing_folder_names": existing_folder_names
189+
"existing_folder_names": existing_folder_names,
217190
}
218-
219-
return self.post_request(endpoint_url, headers, payload)
220191

192+
return self.post_request(endpoint_url, headers, payload)
221193

222-
def fix_conformance_tests_issue(self, frid, functional_requirement_id, plain_source_tree, linked_resources, existing_files_content, code_diff, conformance_tests_files, conformance_tests_issue, implementation_fix_count):
194+
def fix_conformance_tests_issue(
195+
self,
196+
frid,
197+
functional_requirement_id,
198+
plain_source_tree,
199+
linked_resources,
200+
existing_files_content,
201+
code_diff,
202+
conformance_tests_files,
203+
conformance_tests_issue,
204+
implementation_fix_count,
205+
):
223206
endpoint_url = f"{self.api_url}/fix_conformance_tests_issue"
224-
headers = {
225-
"X-API-Key": self.api_key,
226-
"Content-Type": "application/json"
227-
}
228-
207+
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
208+
229209
payload = {
230210
"frid": frid,
231211
"functional_requirement_id": functional_requirement_id,
@@ -235,7 +215,7 @@ def fix_conformance_tests_issue(self, frid, functional_requirement_id, plain_sou
235215
"code_diff": code_diff,
236216
"conformance_tests_files": conformance_tests_files,
237217
"conformance_tests_issue": conformance_tests_issue,
238-
"implementation_fix_count": implementation_fix_count
218+
"implementation_fix_count": implementation_fix_count,
239219
}
240-
220+
241221
return self.post_request(endpoint_url, headers, payload)
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env python3
22

3-
import unittest
43
import subprocess
4+
import unittest
5+
56

67
class TestHelloWorld(unittest.TestCase):
78
def test_hello_world_output(self):
89
# Run the hello_world.py script and capture its output
9-
result = subprocess.run(['python3', 'hello_world.py'], capture_output=True, text=True)
10-
10+
result = subprocess.run(["python3", "hello_world.py"], capture_output=True, text=True)
11+
1112
# Check if the output matches the expected string
1213
self.assertEqual(result.stdout.strip(), "hello, world")
1314

14-
if __name__ == '__main__':
15-
unittest.main()
15+
16+
if __name__ == "__main__":
17+
unittest.main()

0 commit comments

Comments
 (0)