1
- import json
2
1
import requests
3
2
4
3
@@ -23,7 +22,7 @@ class MissingResource(Exception):
23
22
24
23
25
24
class PlainSyntaxError (Exception ):
26
- pass
25
+ pass
27
26
28
27
29
28
class OnlyRelativeLinksAllowed (Exception ):
@@ -35,66 +34,62 @@ class LinkMustHaveTextSpecified(Exception):
35
34
36
35
37
36
class CodeplainAPI :
38
-
37
+
39
38
def __init__ (self , api_key ):
40
39
self .api_key = api_key
41
40
42
-
43
41
@property
44
42
def api_url (self ):
45
43
return self ._api_url
46
44
47
-
48
45
@api_url .setter
49
46
def api_url (self , value ):
50
47
self ._api_url = value
51
48
52
-
53
49
def post_request (self , endpoint_url , headers , payload ):
54
50
response = requests .post (endpoint_url , headers = headers , json = payload )
55
51
56
52
try :
57
53
response_json = response .json ()
58
54
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 } " )
60
56
raise
61
57
62
58
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" ])
86
82
87
83
response .raise_for_status ()
88
84
89
85
return response_json
90
86
91
-
92
87
def get_plain_source_tree (self , plain_source , loaded_templates ):
93
88
"""
94
89
Builds plain source tree from the given plain text source in Markdown format.
95
90
96
91
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.
98
93
loaded_templates (dict): A dictionary containing the loaded templates.
99
94
100
95
Returns:
@@ -104,128 +99,113 @@ def get_plain_source_tree(self, plain_source, loaded_templates):
104
99
Exception: If parsing of plain_source fails.
105
100
"""
106
101
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" }
118
103
104
+ payload = {"plain_source" : plain_source , "loaded_templates" : loaded_templates }
105
+
106
+ return self .post_request (endpoint_url , headers , payload )
119
107
120
108
def render_functional_requirement (self , frid , plain_source_tree , linked_resources , existing_files_content ):
121
109
"""
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,
123
111
plain source tree, and existing files' content.
124
112
125
113
Args:
126
114
frid (str): The unique identifier for the functional requirement to be rendered.
127
115
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
129
117
and the values are the content of those resources.
130
118
existing_files_content (dict): A dictionary where the keys represent code base
131
119
filenames and the values are the content of those files.
132
120
133
121
Returns:
134
- str: A string containing the rendered functional requirement, formatted
122
+ str: A string containing the rendered functional requirement, formatted
135
123
appropriately based on the inputs.
136
124
137
125
Raises:
138
126
ValueError: If the frid is invalid or the necessary plain source tree is not valid.
139
127
"""
140
128
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
+
146
131
payload = {
147
132
"frid" : frid ,
148
133
"plain_source_tree" : plain_source_tree ,
149
134
"linked_resources" : linked_resources ,
150
- "existing_files_content" : existing_files_content
135
+ "existing_files_content" : existing_files_content ,
151
136
}
152
137
153
138
return self .post_request (endpoint_url , headers , payload )
154
139
155
-
156
140
def fix_unittests_issue (self , frid , plain_source_tree , linked_resources , existing_files_content , unittests_issue ):
157
141
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
+
163
144
payload = {
164
145
"frid" : frid ,
165
146
"plain_source_tree" : plain_source_tree ,
166
147
"linked_resources" : linked_resources ,
167
148
"existing_files_content" : existing_files_content ,
168
- "unittests_issue" : unittests_issue
149
+ "unittests_issue" : unittests_issue ,
169
150
}
170
-
171
- return self .post_request (endpoint_url , headers , payload )
172
151
152
+ return self .post_request (endpoint_url , headers , payload )
173
153
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 ):
175
155
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
+
181
158
payload = {
159
+ "frid" : frid ,
182
160
"files_to_check" : list (files_to_check ),
183
- "existing_files_content" : existing_files_content
161
+ "existing_files_content" : existing_files_content ,
184
162
}
185
163
186
164
return self .post_request (endpoint_url , headers , payload )
187
165
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
+ ):
190
169
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
+
196
172
payload = {
197
173
"frid" : frid ,
198
174
"functional_requirement_id" : functional_requirement_id ,
199
175
"plain_source_tree" : plain_source_tree ,
200
176
"linked_resources" : linked_resources ,
201
- "existing_files_content" : existing_files_content
177
+ "existing_files_content" : existing_files_content ,
202
178
}
203
-
204
- return self .post_request (endpoint_url , headers , payload )
205
179
180
+ return self .post_request (endpoint_url , headers , payload )
206
181
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 ):
208
183
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
+
214
186
payload = {
187
+ "frid" : frid ,
215
188
"functional_requirement" : functional_requirement ,
216
- "existing_folder_names" : existing_folder_names
189
+ "existing_folder_names" : existing_folder_names ,
217
190
}
218
-
219
- return self .post_request (endpoint_url , headers , payload )
220
191
192
+ return self .post_request (endpoint_url , headers , payload )
221
193
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
+ ):
223
206
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
+
229
209
payload = {
230
210
"frid" : frid ,
231
211
"functional_requirement_id" : functional_requirement_id ,
@@ -235,7 +215,7 @@ def fix_conformance_tests_issue(self, frid, functional_requirement_id, plain_sou
235
215
"code_diff" : code_diff ,
236
216
"conformance_tests_files" : conformance_tests_files ,
237
217
"conformance_tests_issue" : conformance_tests_issue ,
238
- "implementation_fix_count" : implementation_fix_count
218
+ "implementation_fix_count" : implementation_fix_count ,
239
219
}
240
-
220
+
241
221
return self .post_request (endpoint_url , headers , payload )
0 commit comments