Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ public enum Cause {
HTTP_ERROR, FORBIDDEN, INTERNAL_ERROR, MAPPING
}

private String message;

public SteamApiException(String message) {
super(message);
}

public SteamApiException(String message, Throwable cause) {
super(message, cause);
}
private final String message;

public SteamApiException(Cause cause, Throwable exceptionCause) {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.lukaspradel.steamapi.core.SteamApiRequestHandler;
import com.lukaspradel.steamapi.core.exception.SteamApiException;
import com.lukaspradel.steamapi.core.exception.SteamApiKeyException;

import java.io.IOException;
import java.net.URI;
Expand All @@ -14,6 +13,7 @@
import java.util.Map;
import java.util.stream.Stream;

import static com.lukaspradel.steamapi.core.exception.SteamApiException.Cause.INTERNAL_ERROR;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;

Expand All @@ -29,7 +29,7 @@ public String getWebApiResponse(SteamWebApiRequest request) throws SteamApiExcep
return getWebApiResponse(getRequestUrl(request));
}

URI getRequestUrl(SteamWebApiRequest request) throws SteamApiException {
URI getRequestUrl(SteamWebApiRequest request) {
String scheme = getProtocol();
String host = request.getBaseUrl();
String path = getRequestPath(request);
Expand All @@ -49,14 +49,14 @@ String getRequestPath(SteamWebApiRequest request) {
.collect(joining("/", "/", ""));
}

String getRequestQuery(Map<String, String> parameters) throws SteamApiException {
String getRequestQuery(Map<String, String> parameters) {
if (getKey() == null) {
throw new SteamApiKeyException("Steam API key is not present or null");
throw new IllegalArgumentException("Steam API key is not present or null");
}

for (var e : parameters.entrySet()) {
if (e.getKey() == null) {
throw new SteamApiException("The key of the parameter with the value '" + e.getValue() + "' is null");
throw new IllegalArgumentException("The key of the parameter with the value '" + e.getValue() + "' is null");
}
}

Expand All @@ -69,7 +69,7 @@ String getRequestQuery(Map<String, String> parameters) throws SteamApiException
.collect(joining("&"));
}

URI getRequestUri(String scheme, String host, String path, String query) throws SteamApiException {
URI getRequestUri(String scheme, String host, String path, String query) {
var uri = new StringBuilder();

uri.append(scheme);
Expand All @@ -82,8 +82,8 @@ URI getRequestUri(String scheme, String host, String path, String query) throws
try {
return new URI(uri.toString());
} catch (URISyntaxException e) {
throw new SteamApiException(
"Failed to process the Web API request due to the following error: " + e.getMessage(), e);
throw new IllegalArgumentException(
"Failed to construct a valid request URI due to the following error: " + e.getMessage(), e);
}
}

Expand All @@ -103,8 +103,7 @@ String getWebApiResponse(URI requestUrl) throws SteamApiException {
throw new SteamApiException(SteamApiException.Cause.HTTP_ERROR, statusCode);
}
} catch (IOException | InterruptedException e) {
throw new SteamApiException(
"The Web API request failed due to the following error: " + e.getMessage(), e);
throw new SteamApiException(INTERNAL_ERROR, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.lukaspradel.steamapi.webapi.request;

import com.lukaspradel.steamapi.webapi.core.BaseTest;
import com.lukaspradel.steamapi.core.exception.SteamApiException;
import com.lukaspradel.steamapi.core.exception.SteamApiKeyException;
import com.lukaspradel.steamapi.webapi.core.BaseTest;
import com.lukaspradel.steamapi.webapi.core.SteamWebApiInterface;
import com.lukaspradel.steamapi.webapi.core.SteamWebApiInterfaceMethod;
import com.lukaspradel.steamapi.webapi.core.SteamWebApiVersion;
Expand All @@ -19,6 +18,7 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
Expand All @@ -34,14 +34,14 @@ public class SteamWebApiRequestHandlerTest extends BaseTest {
private static final int UNAUTHORIZED = 401;
private static final int INTERNAL_SERVER_ERROR = 500;

private String key = "12345";
private final String key = "12345";

private URI uri = URI.create("http://localhost:80");
private final URI uri = URI.create("http://localhost:80");

private SteamWebApiRequestHandler requestHandlerHttps = new SteamWebApiRequestHandler(
private final SteamWebApiRequestHandler requestHandlerHttps = new SteamWebApiRequestHandler(
true, key);

private SteamWebApiRequestHandler requestHandlerHttpsSpy = spy(requestHandlerHttps);
private final SteamWebApiRequestHandler requestHandlerHttpsSpy = spy(requestHandlerHttps);

@Mock
private SteamWebApiRequest requestMock;
Expand All @@ -52,10 +52,10 @@ public class SteamWebApiRequestHandlerTest extends BaseTest {
@Mock
private HttpResponse<String> httpResponseMock;

private ArgumentMatcher<BodyHandler<String>> bodyHandlerMatcher = arg -> arg != null ? true : false;
private final ArgumentMatcher<BodyHandler<String>> bodyHandlerMatcher = Objects::nonNull;

@Test
public void testGetRequestUrl() throws SteamApiException {
public void testGetRequestUrl() {

Map<String, String> parameters = new HashMap<String, String>();

Expand Down Expand Up @@ -83,7 +83,7 @@ public void testGetRequestUrl() throws SteamApiException {
}

@Test
public void testGetRequestQuery() throws SteamApiException {
public void testGetRequestQuery() {
var parameters = new LinkedHashMap<String, String>();
parameters.put("key", null); // prepopulate the key, it gets set in the getRequestQuery method
parameters.put("test-parameter", "test-value");
Expand All @@ -95,20 +95,20 @@ public void testGetRequestQuery() throws SteamApiException {
assertEquals(query, "key=12345&test-parameter=test-value&format=json&input_json=%7B%22steamid%22%3A%2276561198039505218%22%7D");
}

@Test(expectedExceptions = SteamApiKeyException.class)
@Test(expectedExceptions = IllegalArgumentException.class)
public void testGetRequestQueryApiKeyIsNull() throws SteamApiException {
var reqHandler = new SteamWebApiRequestHandler(true, null);
reqHandler.getRequestQuery(null);
}

@Test(expectedExceptions = SteamApiException.class)
public void testGetRequestQueryParameterKeyIsNull() throws SteamApiException {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testGetRequestQueryParameterKeyIsNull() {
var params = new HashMap<String, String>();
params.put(null, "test-value");

try {
requestHandlerHttps.getRequestQuery(params);
} catch (SteamApiException e) {
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "The key of the parameter with the value 'test-value' is null");
throw e;
}
Expand All @@ -130,7 +130,7 @@ public void testGetRequestPath() {
}

@Test
public void testGetRequestUri() throws SteamApiException {
public void testGetRequestUri() {
String scheme = "https";
String host = "api.steampowered.com";
String path = "/IPlayerService/GetOwnedGames/v0001";
Expand All @@ -152,8 +152,8 @@ public void testGetRequestUri() throws SteamApiException {
"https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001?key=12345&format=json&input_json=%7B%22steamid%22%3A%2276561198039505218%22%7D");
}

@Test(expectedExceptions = SteamApiException.class)
public void testGetRequestUriWithInvalidUri() throws SteamApiException {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testGetRequestUriWithInvalidUri() {
String scheme = "";
String host = "api.steampowered.com";
String path = "/IPlayerService/GetOwnedGames/v0001";
Expand Down
Loading