Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yildizsc committed Dec 11, 2023
1 parent 962c808 commit b615c97
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/main/java/com/webex/events/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
public class Configuration {
private String accessToken;
private byte timeout = 30;

private byte maxRetries = 5;

public String getAccessToken() {
return accessToken;
}

public Configuration setAccessToken(String accessToken) {
this.accessToken = accessToken;
return this;
}

public String getAccessToken() {
return accessToken;
}

public int getTimeout() {
return timeout;
}
Expand Down
37 changes: 28 additions & 9 deletions src/main/java/com/webex/events/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.regex.Pattern;

public class Helpers {

public static final String UUID_REGEX_PATTERN = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
public static final String UUID_ERROR_MESSAGE = "Idempotency-Key must be UUID format";
public static final String ACCESS_TOKEN_IS_MISSING = "Access token is missing.";
private static String userAgent = null;
private static String introspectionQuery = null;
private static HashMap<String, URI> uris = new HashMap<>();

static void validateIdempotencyKey(Object key) throws InvalidUUIDFormatError {
if (key != null) {
Pattern regex = Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
if (key == null || key.toString().isEmpty()) {
return;
}

if (!regex.matcher(key.toString()).matches()) {
throw new InvalidUUIDFormatError("Idempotency-Key must be UUID format");
}
Pattern regex = Pattern.compile(UUID_REGEX_PATTERN);

if (regex.matcher(key.toString()).matches()) {
return;
}

throw new InvalidUUIDFormatError(UUID_ERROR_MESSAGE);
}

static void validateAccessTokenExistence(String accessToken) throws AccessTokenIsRequiredError {
if (accessToken == null || accessToken.isEmpty()) {
throw new AccessTokenIsRequiredError("Access token is missing.");
throw new AccessTokenIsRequiredError(ACCESS_TOKEN_IS_MISSING);
}
}

Expand All @@ -49,19 +58,29 @@ public static String getUserAgent() {
}

public static URI getUri(String accessToken) {
if (uris.containsKey(accessToken)) {
return uris.get(accessToken);
}

String path = "/graphql";
String url ;
String url;
if (accessToken.startsWith("sk_live")) {
url = "https://public.api.socio.events" + path;
} else {
url = "https://public.sandbox-api.socio.events" + path;
}

return URI.create(url);
URI uri = URI.create(url);
uris.put(accessToken, uri);

return uri;
}

public static String getIntrospectionQuery() {
return """
if (introspectionQuery != null) {
return introspectionQuery;
}
return introspectionQuery = """
query IntrospectionQuery {
__schema {
\s
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/webex/events/com/webex/events/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ Response doRequest() throws Exception {
return Client.query(graphqlQuery, operationName, variables, headers, config);
}

@Test
@DisplayName("It does introspection query")
void doesIntrospectionQuery() throws Exception {
when(httpResponse.statusCode()).thenReturn(200);
when(httpClient.send(any(),any())).thenReturn(httpResponse);
when(httpResponse.body()).thenReturn("introspection");

final Configuration config = new Configuration()
.setAccessToken("sk_test_token_0190101010");
String response = Client.doIntrospectQuery(config);
assertTrue("introspection" == response);
}

@Test
@DisplayName("Should raise AccessTokenIsRequired exception without given access token")
Expand Down

0 comments on commit b615c97

Please sign in to comment.