Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change from static instances to local instances of ApiClient #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions sdk/src/main/java/com/ifountain/opsgenie/client/OpsGenieClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public OpsGenieClient(OpsGenieHttpClient httpClient) {
this.jsonHttpClient.setApiKey(getApiKey());
this.restApiClient = new RestApiClient(httpClient);
this.restApiClient.setApiKey(getApiKey());
this.swaggerApiClient = Configuration.getDefaultApiClient();
this.swaggerApiClient = getApiClient();
innerUserOpsGenieClient = new InnerUserOpsGenieClient(this.jsonHttpClient);
innerGroupOpsGenieClient = new InnerGroupOpsGenieClient(this.jsonHttpClient);
innerTeamOpsGenieClient = new InnerTeamOpsGenieClient(this.jsonHttpClient);
Expand All @@ -160,6 +160,16 @@ public OpsGenieClient(OpsGenieHttpClient httpClient) {

}

private ApiClient getApiClient() {
ApiClient client = new ApiClient();

// Configure API key authorization: GenieKey
ApiKeyAuth genieKey = (ApiKeyAuth) client.getAuthentication("GenieKey");
genieKey.setApiKeyPrefix("GenieKey");

return client;
}

/**
* @see IOpsGenieClient#user()
*/
Expand Down Expand Up @@ -384,15 +394,7 @@ public CopyNotificationRulesResponse copyNotificationRules(CopyNotificationRules
}

public AlertApi alertV2() {
// Configure API key authorization: GenieKey
ApiKeyAuth genieKey = (ApiKeyAuth) swaggerApiClient.getAuthentication("GenieKey");

if (StringUtils.isNotEmpty(getApiKey())) {
genieKey.setApiKey(getApiKey());
}
genieKey.setApiKeyPrefix("GenieKey");

return new AlertApi();
return new AlertApi(swaggerApiClient);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.ifountain.opsgenie.client;

import com.ifountain.opsgenie.client.http.HttpTestRequestListener
import com.ifountain.opsgenie.client.swagger.auth.ApiKeyAuth;
import com.ifountain.opsgenie.client.test.util.OpsGenieClientTestCase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

class AlertV2OpsGenieClientTest extends OpsGenieClientTestCase implements HttpTestRequestListener {
@Test
void testApiKeySetAsExpected() throws Exception {
// Given
def client1 = new OpsGenieClient()
client1.apiKey = "secret-1"

def client2 = new OpsGenieClient()
client2.apiKey = "secret-2"

// When
def api1 = client1.alertV2()
def api2 = client2.alertV2()

// Then
assertEquals("secret-1", ((ApiKeyAuth) api1.getApiClient().getAuthentication("GenieKey")).apiKey)
assertEquals("secret-2", ((ApiKeyAuth) api2.getApiClient().getAuthentication("GenieKey")).apiKey)
}

@Test
void testApplyHeaders() throws Exception {
// Given
def client = new OpsGenieClient()
client.apiKey = "secret"

def headers = new HashMap<String, String>()

// When
((ApiKeyAuth) client.alertV2().apiClient.getAuthentication("GenieKey")).applyToParams([], headers)

// Then
assertEquals("GenieKey secret", headers["Authorization"])
}
}
1 change: 1 addition & 0 deletions test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ dependencies {
compile 'org.apache.directory.studio:org.apache.commons.lang:2.6'
compile 'org.littleshoot:littleproxy:0.5.OG-atlassian-hosted'
compile 'junit:junit:4.12'
compile 'log4j:log4j:1.2.16'
}