Skip to content

Commit 442a865

Browse files
IvanLHcopybara-github
authored andcommitted
chore: Add getHeaders method to the ApiResponse
PiperOrigin-RevId: 755967387
1 parent 5075e91 commit 442a865

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/main/java/com/google/genai/ApiResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package com.google.genai;
1818

19+
import org.apache.http.Header;
1920
import org.apache.http.HttpEntity;
2021

22+
2123
/** The API response contains a response to a call to the GenAI APIs. */
2224
abstract class ApiResponse implements AutoCloseable {
2325
/** Gets the HttpEntity. */
2426
public abstract HttpEntity getEntity();
2527

28+
public abstract Header[] getHeaders();
29+
2630
@Override
2731
public abstract void close();
2832
}

src/main/java/com/google/genai/HttpApiResponse.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.genai.errors.ApiException;
2020
import com.google.genai.errors.GenAiIOException;
2121
import java.io.IOException;
22+
import org.apache.http.Header;
2223
import org.apache.http.HttpEntity;
2324
import org.apache.http.client.methods.CloseableHttpResponse;
2425

@@ -39,6 +40,12 @@ public HttpEntity getEntity() {
3940
return response.getEntity();
4041
}
4142

43+
/** Returns all of the headers from the response. */
44+
@Override
45+
public Header[] getHeaders() {
46+
return response.getAllHeaders();
47+
}
48+
4249
/** Closes the Http response. */
4350
@Override
4451
public void close() {

src/main/java/com/google/genai/ReplayApiClient.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
import java.util.Map;
3737
import java.util.Optional;
3838
import java.util.stream.Stream;
39+
import org.apache.http.Header;
3940
import org.apache.http.ProtocolVersion;
4041
import org.apache.http.StatusLine;
4142
import org.apache.http.entity.BasicHttpEntity;
43+
import org.apache.http.message.BasicHeader;
4244
import org.apache.http.message.BasicStatusLine;
4345

4446
// TODO(b/369384123): Currently the ReplayApiClient mirrors the HttpApiClient. We will refactor the
@@ -131,10 +133,17 @@ public ApiResponse request(String httpMethod, String path, String requestJson) {
131133
Map<String, Object> responseMap = (Map<String, Object>) currentMember.get("response");
132134
Integer statusCode = (Integer) responseMap.get("status_code");
133135
List<Object> bodySegments = (List<Object>) responseMap.get("body_segments");
136+
Map<String, String> headerMap = (Map<String, String>) responseMap.get("headers");
134137
StringBuilder responseBody = new StringBuilder();
135138
for (Object bodySegment : bodySegments) {
136139
responseBody.append(bodySegment.toString());
137140
}
141+
142+
Header[] headers = headerMap.entrySet()
143+
.stream()
144+
.map(entry -> new BasicHeader(entry.getKey(), entry.getValue()))
145+
.toArray(Header[]::new);
146+
138147
String responseString = responseBody.toString();
139148

140149
BasicHttpEntity entity = new BasicHttpEntity();
@@ -144,7 +153,7 @@ public ApiResponse request(String httpMethod, String path, String requestJson) {
144153
StatusLine statusLine =
145154
new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), statusCode, "OK");
146155

147-
return new ReplayApiResponse(entity, statusLine);
156+
return new ReplayApiResponse(entity, statusLine, headers);
148157
} else {
149158
// Note that if the client mode is "api", then the ReplayApiClient will not be used.
150159
throw new IllegalArgumentException("Invalid client mode: " + this.clientMode);

src/main/java/com/google/genai/ReplayApiResponse.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.genai;
1818

19+
import org.apache.http.Header;
1920
import org.apache.http.HttpEntity;
2021
import org.apache.http.StatusLine;
2122

@@ -25,17 +26,22 @@ final class ReplayApiResponse extends ApiResponse {
2526

2627
private final HttpEntity entity;
2728
private final StatusLine statusLine;
29+
private final Header[] headers;
2830

29-
public ReplayApiResponse(HttpEntity entity, StatusLine statusLine) {
31+
public ReplayApiResponse(HttpEntity entity, StatusLine statusLine, Header[] headers) {
3032
this.entity = entity;
3133
this.statusLine = statusLine;
34+
this.headers = headers;
3235
}
3336

3437
@Override
3538
public HttpEntity getEntity() {
3639
return this.entity;
3740
}
3841

42+
@Override
43+
public Header[] getHeaders() { return this.headers;}
44+
3945
@Override
4046
public void close() {}
4147
}

0 commit comments

Comments
 (0)