1- import com .openai .core .ObjectMappers ;
21import com .openai .core .http .Headers ;
32import com .openai .core .http .HttpRequest ;
43import com .openai .core .http .HttpRequestBody ;
1110import java .nio .file .Path ;
1211import java .nio .file .attribute .PosixFilePermissions ;
1312import java .security .MessageDigest ;
14- import java .util .Comparator ;
1513import java .util .List ;
1614import java .util .Map ;
17- import java .util .stream .Stream ;
1815
1916public class RequestResponseRecord {
2017 /**
@@ -36,7 +33,6 @@ public class RequestResponseRecord {
3633 private static final String END_RESPONSE_BODY = "-- end response body --" ;
3734 private static final String KEY_VALUE_SEP = ": " ;
3835 private static final char LINE_SEP = '\n' ;
39- private static final Object UNPARSEABLE = new Object ();
4036
4137 public final int status ;
4238 public final Map <String , String > headers ;
@@ -75,28 +71,6 @@ public static boolean exists(Path recordsDir, HttpRequest request) {
7571 return filePath .toFile ().exists ();
7672 }
7773
78- // TODO: Codex generated patch to pass under arm64, revisit later.
79- public static Path findEquivalentRequestRecord (
80- Path recordsDir , String method , String path , byte [] requestBody ) {
81- Path targetDir = recordsDir .resolve (path );
82- if (!Files .isDirectory (targetDir )) {
83- return null ;
84- }
85-
86- Object requestJson = tryParseJson (new String (requestBody , StandardCharsets .UTF_8 ));
87- try (Stream <Path > files = Files .list (targetDir )) {
88- return files
89- .filter (Files ::isRegularFile )
90- .filter (file -> file .getFileName ().toString ().endsWith ("." + method + ".rec" ))
91- .sorted (Comparator .comparing (Path ::toString ))
92- .filter (file -> requestBodiesEquivalent (requestJson , requestBody , file ))
93- .findFirst ()
94- .orElse (null );
95- } catch (IOException e ) {
96- throw new RuntimeException (e );
97- }
98- }
99-
10074 private static ByteArrayOutputStream readRequestBody (HttpRequest request ) {
10175 ByteArrayOutputStream requestBodyBytes = new ByteArrayOutputStream ();
10276 try (HttpRequestBody requestBody = request .body ()) {
@@ -215,51 +189,4 @@ public static RequestResponseRecord read(Path recFilePath) {
215189 byte [] body = bodyBuilder .toString ().getBytes (StandardCharsets .UTF_8 );
216190 return new RequestResponseRecord (statusCode , headers , body );
217191 }
218-
219- // TODO: Codex generated patch to pass under arm64, revisit later.
220- public static String readRecordedRequestBody (Path recFilePath ) {
221- StringBuilder bodyBuilder = new StringBuilder ();
222-
223- try {
224- List <String > lines = Files .readAllLines (recFilePath , StandardCharsets .UTF_8 );
225- boolean inRequestBody = false ;
226-
227- for (String line : lines ) {
228- if (line .equals (BEGIN_REQUEST_BODY )) {
229- inRequestBody = true ;
230- } else if (line .equals (END_REQUEST_BODY )) {
231- inRequestBody = false ;
232- } else if (inRequestBody ) {
233- if (bodyBuilder .length () > 0 ) {
234- bodyBuilder .append (LINE_SEP );
235- }
236- bodyBuilder .append (line );
237- }
238- }
239- } catch (IOException e ) {
240- throw new RuntimeException (e );
241- }
242-
243- return bodyBuilder .toString ();
244- }
245-
246- private static boolean requestBodiesEquivalent (
247- Object requestJson , byte [] requestBody , Path file ) {
248- String recordedRequestBody = readRecordedRequestBody (file );
249- Object recordedRequestJson = tryParseJson (recordedRequestBody );
250-
251- if (requestJson != UNPARSEABLE && recordedRequestJson != UNPARSEABLE ) {
252- return requestJson .equals (recordedRequestJson );
253- }
254-
255- return recordedRequestBody .equals (new String (requestBody , StandardCharsets .UTF_8 ));
256- }
257-
258- private static Object tryParseJson (String requestBody ) {
259- try {
260- return ObjectMappers .jsonMapper ().readValue (requestBody , Object .class );
261- } catch (Exception e ) {
262- return UNPARSEABLE ;
263- }
264- }
265192}
0 commit comments