Skip to content

Commit

Permalink
Use org domain URL when making API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Sep 28, 2022
1 parent b26a036 commit dcf3402
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class DataApiImpl implements DataApi {

private final RestApi restApi;

public DataApiImpl(URI salesforceBaseUrl, String apiVersion, String accessToken) {
this.restApi = new RestApi(salesforceBaseUrl, apiVersion, accessToken);
public DataApiImpl(URI orgDomainUrl, String apiVersion, String accessToken) {
this.restApi = new RestApi(orgDomainUrl, apiVersion, accessToken);
}

@Override
Expand Down Expand Up @@ -135,7 +135,7 @@ public Map<ReferenceId, RecordModificationResult> commitUnitOfWork(UnitOfWork un

CompositeGraphRestApiRequest<ModifyRecordResult> request =
new CompositeGraphRestApiRequest<>(
restApi.getSalesforceBaseUrl(), restApi.getApiVersion(), impl.getSubrequests());
restApi.getOrgDomainUrl(), restApi.getApiVersion(), impl.getSubrequests());

Map<String, ModifyRecordResult> result = executeRequest(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class DataApiImpl implements DataApi {

private final RestApi restApi;

public DataApiImpl(URI salesforceBaseUrl, String apiVersion, String accessToken) {
this.restApi = new RestApi(salesforceBaseUrl, apiVersion, accessToken);
public DataApiImpl(URI orgDomainUrl, String apiVersion, String accessToken) {
this.restApi = new RestApi(orgDomainUrl, apiVersion, accessToken);
}

@Override
Expand Down Expand Up @@ -126,7 +126,7 @@ public Map<ReferenceId, RecordModificationResult> commitUnitOfWork(UnitOfWork un

CompositeGraphRestApiRequest<ModifyRecordResult> request =
new CompositeGraphRestApiRequest<>(
restApi.getSalesforceBaseUrl(), restApi.getApiVersion(), impl.getSubrequests());
restApi.getOrgDomainUrl(), restApi.getApiVersion(), impl.getSubrequests());

Map<String, ModifyRecordResult> result = executeRequest(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public OrgImpl(
this.apiVersion = apiVersion;
this.salesforceContext = salesforceContext;
this.dataApi =
new DataApiImpl(this.getBaseUrl(), this.getApiVersion(), functionContext.getAccessToken());
new DataApiImpl(
this.getDomainUrl(), this.getApiVersion(), functionContext.getAccessToken());
this.user = new UserImpl(salesforceContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@
import org.apache.http.util.EntityUtils;

public final class RestApi {
private final URI salesforceBaseUrl;
private final URI orgDomainUrl;
private final String apiVersion;
private final String accessToken;
private final String clientVersion;
private final Gson gson = new Gson();

public RestApi(URI salesforceBaseUrl, String apiVersion, String accessToken) {
this.salesforceBaseUrl = salesforceBaseUrl;
public RestApi(URI orgDomainUrl, String apiVersion, String accessToken) {
this.orgDomainUrl = orgDomainUrl;
this.apiVersion = apiVersion;
this.accessToken = accessToken;
this.clientVersion = readVersionStringFromProperties().orElse("?.?.?-unknown");
}

public URI getSalesforceBaseUrl() {
return salesforceBaseUrl;
public URI getOrgDomainUrl() {
return orgDomainUrl;
}

public String getApiVersion() {
Expand All @@ -60,7 +60,7 @@ public <T> T execute(RestApiRequest<T> apiRequest)
throws RestApiErrorsException, RestApiException, IOException {
URI uri;
try {
uri = apiRequest.createUri(salesforceBaseUrl, apiVersion);
uri = apiRequest.createUri(orgDomainUrl, apiVersion);
} catch (URISyntaxException e) {
throw new RuntimeException("Unexpected URISyntaxException!", e);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public <T> T execute(RestApiRequest<T> apiRequest)
}

public ByteBuffer downloadFile(String relativeUrl) throws URISyntaxException, IOException {
URI uri = new URIBuilder(this.salesforceBaseUrl).setPath(relativeUrl).build();
URI uri = new URIBuilder(this.orgDomainUrl).setPath(relativeUrl).build();

HttpUriRequest request = createBaseHttpRequest(HttpMethod.GET, uri, Optional.empty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void compositeSingleCreate() throws RestApiErrorsException, IOException,

CompositeGraphRestApiRequest<ModifyRecordResult> request =
new CompositeGraphRestApiRequest<>(
restApi.getSalesforceBaseUrl(), restApi.getApiVersion(), subrequests);
restApi.getOrgDomainUrl(), restApi.getApiVersion(), subrequests);
Map<String, ModifyRecordResult> result = restApi.execute(request);

assertThat(
Expand All @@ -63,7 +63,7 @@ public void compositeSingleCreateWithError() throws IOException, RestApiExceptio

CompositeGraphRestApiRequest<ModifyRecordResult> request =
new CompositeGraphRestApiRequest<>(
restApi.getSalesforceBaseUrl(), restApi.getApiVersion(), subrequests);
restApi.getOrgDomainUrl(), restApi.getApiVersion(), subrequests);

try {
restApi.execute(request);
Expand Down Expand Up @@ -93,7 +93,7 @@ public void compositeSingleUpdate() throws RestApiErrorsException, IOException,

CompositeGraphRestApiRequest<ModifyRecordResult> request =
new CompositeGraphRestApiRequest<>(
restApi.getSalesforceBaseUrl(), restApi.getApiVersion(), subrequests);
restApi.getOrgDomainUrl(), restApi.getApiVersion(), subrequests);
Map<String, ModifyRecordResult> result = restApi.execute(request);

assertThat(
Expand Down Expand Up @@ -127,7 +127,7 @@ public void compositeCreateTree() throws RestApiErrorsException, IOException, Re

CompositeGraphRestApiRequest<ModifyRecordResult> request =
new CompositeGraphRestApiRequest<>(
restApi.getSalesforceBaseUrl(), restApi.getApiVersion(), subrequests);
restApi.getOrgDomainUrl(), restApi.getApiVersion(), subrequests);
Map<String, ModifyRecordResult> result = restApi.execute(request);

assertThat(
Expand Down Expand Up @@ -155,7 +155,7 @@ public void compositeDeleteTest() throws RestApiErrorsException, IOException, Re

CompositeGraphRestApiRequest<ModifyRecordResult> request =
new CompositeGraphRestApiRequest<>(
restApi.getSalesforceBaseUrl(), restApi.getApiVersion(), subrequests);
restApi.getOrgDomainUrl(), restApi.getApiVersion(), subrequests);
Map<String, ModifyRecordResult> result = restApi.execute(request);

assertThat(
Expand Down

0 comments on commit dcf3402

Please sign in to comment.