Skip to content

Commit

Permalink
feat: Expose method for making custom HTTP requests (box/box-codegen#610
Browse files Browse the repository at this point in the history
) (#84)
  • Loading branch information
box-sdk-build authored Nov 28, 2024
1 parent 8cc9535 commit 7476b1b
Show file tree
Hide file tree
Showing 83 changed files with 1,394 additions and 1,055 deletions.
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "1c0a1c0", "specHash": "544d370", "version": "0.1.1" }
{ "engineHash": "35ca680", "specHash": "544d370", "version": "0.1.1" }
4 changes: 3 additions & 1 deletion src/main/java/com/box/sdkgen/box/errors/BoxAPIError.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public static BoxAPIError fromAPICall(Request request, Response response) {
"%d %s; Request ID: %s",
response.code(),
response.message(),
responseInfo.getBody().get("request_id").asText("")),
responseInfo.getBody().get("request_id") != null
? responseInfo.getBody().get("request_id").asText()
: ""),
requestInfo,
responseInfo)
.timestamp(LocalDateTime.now().toString())
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/box/sdkgen/box/errors/ResponseInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import okhttp3.Response;
Expand Down Expand Up @@ -141,7 +140,7 @@ public static ResponseInfo fromResponse(Response response) {
rawBody = response.body().string();
body = jsonToSerializedData(rawBody);
}
} catch (IOException ignored) {
} catch (Exception ignored) {
}

return new ResponseInfo.ResponseInfoBuilder(
Expand All @@ -150,10 +149,10 @@ public static ResponseInfo fromResponse(Response response) {
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0))))
.body(body)
.rawBody(rawBody)
.code(body.get("code").asText(""))
.code(body.get("code") != null ? body.get("code").asText("") : null)
.contextInfo(body.get("context_info") != null ? body.get("context_info") : null)
.requestId(body.get("request_id").asText(""))
.helpUrl(body.get("help_url").asText(""))
.requestId(body.get("request_id") != null ? body.get("request_id").asText() : null)
.helpUrl(body.get("help_url") != null ? body.get("help_url").asText() : null)
.build();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/box/sdkgen/client/BoxClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.box.sdkgen.internal.utils.UtilsManager.entryOf;
import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
import static com.box.sdkgen.networking.fetch.FetchManager.fetch;

import com.box.sdkgen.managers.ai.AiManager;
import com.box.sdkgen.managers.appitemassociations.AppItemAssociationsManager;
Expand Down Expand Up @@ -75,6 +76,8 @@
import com.box.sdkgen.managers.zipdownloads.ZipDownloadsManager;
import com.box.sdkgen.networking.auth.Authentication;
import com.box.sdkgen.networking.baseurls.BaseUrls;
import com.box.sdkgen.networking.fetchoptions.FetchOptions;
import com.box.sdkgen.networking.fetchresponse.FetchResponse;
import com.box.sdkgen.networking.network.NetworkSession;
import java.util.Map;

Expand Down Expand Up @@ -942,6 +945,27 @@ protected BoxClient(BoxClientBuilder builder) {
.build();
}

public FetchResponse makeRequest(FetchOptions fetchOptions) {
Authentication auth = (fetchOptions.getAuth() == null ? this.auth : fetchOptions.getAuth());
NetworkSession networkSession =
(fetchOptions.getNetworkSession() == null
? this.networkSession
: fetchOptions.getNetworkSession());
FetchOptions enrichedFetchOptions =
new FetchOptions.FetchOptionsBuilder(fetchOptions.getUrl(), fetchOptions.getMethod())
.params(fetchOptions.getParams())
.headers(fetchOptions.getHeaders())
.data(fetchOptions.getData())
.fileStream(fetchOptions.getFileStream())
.multipartData(fetchOptions.getMultipartData())
.contentType(fetchOptions.getContentType())
.responseFormat(fetchOptions.getResponseFormat())
.auth(auth)
.networkSession(networkSession)
.build();
return fetch(enrichedFetchOptions);
}

public BoxClient withAsUserHeader(String userId) {
return new BoxClient.BoxClientBuilder(this.auth)
.networkSession(
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/com/box/sdkgen/managers/ai/AiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import static com.box.sdkgen.networking.fetch.FetchManager.fetch;

import com.box.sdkgen.networking.auth.Authentication;
import com.box.sdkgen.networking.fetch.FetchOptions;
import com.box.sdkgen.networking.fetch.FetchResponse;
import com.box.sdkgen.networking.fetchoptions.FetchOptions;
import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
import com.box.sdkgen.networking.fetchresponse.FetchResponse;
import com.box.sdkgen.networking.network.NetworkSession;
import com.box.sdkgen.schemas.aiagentaskoraiagentextractoraiagentextractstructuredoraiagenttextgen.AiAgentAskOrAiAgentExtractOrAiAgentExtractStructuredOrAiAgentTextGen;
import com.box.sdkgen.schemas.aiask.AiAsk;
Expand Down Expand Up @@ -46,12 +47,12 @@ public AiResponseFull createAiAsk(AiAsk requestBody, CreateAiAskHeaders headers)
FetchResponse response =
fetch(
new FetchOptions.FetchOptionsBuilder(
String.join("", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/ask"))
.method("POST")
String.join("", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/ask"),
"POST")
.headers(headersMap)
.data(JsonManager.serialize(requestBody))
.contentType("application/json")
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand All @@ -68,12 +69,12 @@ public AiResponse createAiTextGen(AiTextGen requestBody, CreateAiTextGenHeaders
fetch(
new FetchOptions.FetchOptionsBuilder(
String.join(
"", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/text_gen"))
.method("POST")
"", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/text_gen"),
"POST")
.headers(headersMap)
.data(JsonManager.serialize(requestBody))
.contentType("application/json")
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand Down Expand Up @@ -101,11 +102,11 @@ public AiResponse createAiTextGen(AiTextGen requestBody, CreateAiTextGenHeaders
String.join(
"",
this.networkSession.getBaseUrls().getBaseUrl(),
"/2.0/ai_agent_default"))
.method("GET")
"/2.0/ai_agent_default"),
"GET")
.params(queryParamsMap)
.headers(headersMap)
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand All @@ -124,12 +125,12 @@ public AiResponse createAiExtract(AiExtract requestBody, CreateAiExtractHeaders
fetch(
new FetchOptions.FetchOptionsBuilder(
String.join(
"", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/extract"))
.method("POST")
"", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/extract"),
"POST")
.headers(headersMap)
.data(JsonManager.serialize(requestBody))
.contentType("application/json")
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand All @@ -149,12 +150,12 @@ public AiExtractResponse createAiExtractStructured(
String.join(
"",
this.networkSession.getBaseUrls().getBaseUrl(),
"/2.0/ai/extract_structured"))
.method("POST")
"/2.0/ai/extract_structured"),
"POST")
.headers(headersMap)
.data(JsonManager.serialize(requestBody))
.contentType("application/json")
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import static com.box.sdkgen.networking.fetch.FetchManager.fetch;

import com.box.sdkgen.networking.auth.Authentication;
import com.box.sdkgen.networking.fetch.FetchOptions;
import com.box.sdkgen.networking.fetch.FetchResponse;
import com.box.sdkgen.networking.fetchoptions.FetchOptions;
import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
import com.box.sdkgen.networking.fetchresponse.FetchResponse;
import com.box.sdkgen.networking.network.NetworkSession;
import com.box.sdkgen.schemas.appitemassociations.AppItemAssociations;
import com.box.sdkgen.serialization.json.JsonManager;
Expand Down Expand Up @@ -66,11 +67,11 @@ public AppItemAssociations getFileAppItemAssociations(
this.networkSession.getBaseUrls().getBaseUrl(),
"/2.0/files/",
convertToString(fileId),
"/app_item_associations"))
.method("GET")
"/app_item_associations"),
"GET")
.params(queryParamsMap)
.headers(headersMap)
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand Down Expand Up @@ -115,11 +116,11 @@ public AppItemAssociations getFolderAppItemAssociations(
this.networkSession.getBaseUrls().getBaseUrl(),
"/2.0/folders/",
convertToString(folderId),
"/app_item_associations"))
.method("GET")
"/app_item_associations"),
"GET")
.params(queryParamsMap)
.headers(headersMap)
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import static com.box.sdkgen.networking.fetch.FetchManager.fetch;

import com.box.sdkgen.networking.auth.Authentication;
import com.box.sdkgen.networking.fetch.FetchOptions;
import com.box.sdkgen.networking.fetch.FetchResponse;
import com.box.sdkgen.networking.fetchoptions.FetchOptions;
import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
import com.box.sdkgen.networking.fetchresponse.FetchResponse;
import com.box.sdkgen.networking.network.NetworkSession;
import com.box.sdkgen.schemas.accesstoken.AccessToken;
import com.box.sdkgen.schemas.postoauth2revoke.PostOAuth2Revoke;
Expand Down Expand Up @@ -50,11 +51,11 @@ public void authorizeUser(AuthorizeUserQueryParams queryParams, AuthorizeUserHea
FetchResponse response =
fetch(
new FetchOptions.FetchOptionsBuilder(
String.join("", this.networkSession.getBaseUrls().getOauth2Url(), "/authorize"))
.method("GET")
String.join("", this.networkSession.getBaseUrls().getOauth2Url(), "/authorize"),
"GET")
.params(queryParamsMap)
.headers(headersMap)
.responseFormat(null)
.responseFormat(ResponseFormat.NO_CONTENT)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand All @@ -71,12 +72,12 @@ public AccessToken requestAccessToken(
fetch(
new FetchOptions.FetchOptionsBuilder(
String.join(
"", this.networkSession.getBaseUrls().getBaseUrl(), "/oauth2/token"))
.method("POST")
"", this.networkSession.getBaseUrls().getBaseUrl(), "/oauth2/token"),
"POST")
.headers(headersMap)
.data(JsonManager.serialize(requestBody))
.contentType("application/x-www-form-urlencoded")
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand All @@ -96,12 +97,12 @@ public AccessToken refreshAccessToken(
String.join(
"",
this.networkSession.getBaseUrls().getBaseUrl(),
"/oauth2/token#refresh"))
.method("POST")
"/oauth2/token#refresh"),
"POST")
.headers(headersMap)
.data(JsonManager.serialize(requestBody))
.contentType("application/x-www-form-urlencoded")
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand All @@ -118,12 +119,12 @@ public void revokeAccessToken(PostOAuth2Revoke requestBody, RevokeAccessTokenHea
fetch(
new FetchOptions.FetchOptionsBuilder(
String.join(
"", this.networkSession.getBaseUrls().getBaseUrl(), "/oauth2/revoke"))
.method("POST")
"", this.networkSession.getBaseUrls().getBaseUrl(), "/oauth2/revoke"),
"POST")
.headers(headersMap)
.data(JsonManager.serialize(requestBody))
.contentType("application/x-www-form-urlencoded")
.responseFormat(null)
.responseFormat(ResponseFormat.NO_CONTENT)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/box/sdkgen/managers/avatars/AvatarsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import static com.box.sdkgen.networking.fetch.FetchManager.fetch;

import com.box.sdkgen.networking.auth.Authentication;
import com.box.sdkgen.networking.fetch.FetchOptions;
import com.box.sdkgen.networking.fetch.FetchResponse;
import com.box.sdkgen.networking.fetch.MultipartItem;
import com.box.sdkgen.networking.fetchoptions.FetchOptions;
import com.box.sdkgen.networking.fetchoptions.MultipartItem;
import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
import com.box.sdkgen.networking.fetchresponse.FetchResponse;
import com.box.sdkgen.networking.network.NetworkSession;
import com.box.sdkgen.schemas.useravatar.UserAvatar;
import com.box.sdkgen.serialization.json.JsonManager;
Expand Down Expand Up @@ -46,10 +47,10 @@ public InputStream getUserAvatar(String userId, GetUserAvatarHeaders headers) {
this.networkSession.getBaseUrls().getBaseUrl(),
"/2.0/users/",
convertToString(userId),
"/avatar"))
.method("GET")
"/avatar"),
"GET")
.headers(headersMap)
.responseFormat("binary")
.responseFormat(ResponseFormat.BINARY)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand All @@ -71,8 +72,8 @@ public UserAvatar createUserAvatar(
this.networkSession.getBaseUrls().getBaseUrl(),
"/2.0/users/",
convertToString(userId),
"/avatar"))
.method("POST")
"/avatar"),
"POST")
.headers(headersMap)
.multipartData(
Arrays.asList(
Expand All @@ -82,7 +83,7 @@ public UserAvatar createUserAvatar(
.contentType(requestBody.getPicContentType())
.build()))
.contentType("multipart/form-data")
.responseFormat("json")
.responseFormat(ResponseFormat.JSON)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand All @@ -103,10 +104,10 @@ public void deleteUserAvatar(String userId, DeleteUserAvatarHeaders headers) {
this.networkSession.getBaseUrls().getBaseUrl(),
"/2.0/users/",
convertToString(userId),
"/avatar"))
.method("DELETE")
"/avatar"),
"DELETE")
.headers(headersMap)
.responseFormat(null)
.responseFormat(ResponseFormat.NO_CONTENT)
.auth(this.auth)
.networkSession(this.networkSession)
.build());
Expand Down
Loading

0 comments on commit 7476b1b

Please sign in to comment.