1+ import com .openai .core .ObjectMappers ;
12import com .openai .core .http .Headers ;
23import com .openai .core .http .HttpRequest ;
34import com .openai .core .http .HttpRequestBody ;
1011import java .nio .file .Path ;
1112import java .nio .file .attribute .PosixFilePermissions ;
1213import java .security .MessageDigest ;
14+ import java .util .Comparator ;
1315import java .util .List ;
1416import java .util .Map ;
17+ import java .util .stream .Stream ;
1518
1619public class RequestResponseRecord {
1720 /**
@@ -33,6 +36,7 @@ public class RequestResponseRecord {
3336 private static final String END_RESPONSE_BODY = "-- end response body --" ;
3437 private static final String KEY_VALUE_SEP = ": " ;
3538 private static final char LINE_SEP = '\n' ;
39+ private static final Object UNPARSEABLE = new Object ();
3640
3741 public final int status ;
3842 public final Map <String , String > headers ;
@@ -71,6 +75,28 @@ public static boolean exists(Path recordsDir, HttpRequest request) {
7175 return filePath .toFile ().exists ();
7276 }
7377
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+
74100 private static ByteArrayOutputStream readRequestBody (HttpRequest request ) {
75101 ByteArrayOutputStream requestBodyBytes = new ByteArrayOutputStream ();
76102 try (HttpRequestBody requestBody = request .body ()) {
@@ -189,4 +215,51 @@ public static RequestResponseRecord read(Path recFilePath) {
189215 byte [] body = bodyBuilder .toString ().getBytes (StandardCharsets .UTF_8 );
190216 return new RequestResponseRecord (statusCode , headers , body );
191217 }
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+ }
192265}
0 commit comments