Skip to content

Commit

Permalink
Merge pull request #22 from gettyimages/use-authentication-endpoint
Browse files Browse the repository at this point in the history
Use authentication.gettyimages.com

Additionally:

- Use GetApiClientWithClientCredentials() in README as we want to discourage resource owner
- Refactor tests, placing common mock code in TestBase
  • Loading branch information
ChrisSimmons authored Jun 5, 2023
2 parents c1bfa52 + 47db928 commit 43a4a63
Show file tree
Hide file tree
Showing 15 changed files with 387 additions and 599 deletions.
156 changes: 74 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,102 +46,94 @@ $ mvn install
### Search creative images with phrase, age of people, and page

```java
String apiKey = "API Key";
String apiSecret = "API Secret";
String userName = "Username";
String userPassword = "Password";

ApiClient client = ApiClient.GetApiClientWithResourceOwnerCredentials(apiKey, apiSecret, userName, userPassword);

try {
SearchImagesCreative search = client.searchimagescreative()
.withPhrase("cat")
.withAgeOfPeople(EnumSet.of(AgeOfPeople.CHILD, AgeOfPeople.BABY,AgeOfPeople.ADULT))
.withPage(3);
String result = search.executeAsync();
System.out.print(result);

} catch (SdkException e) {
System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage());
System.exit(-1);
}
String apiKey = "API Key";
String apiSecret = "API Secret";

ApiClient client = ApiClient.GetApiClientWithClientCredentials(apiKey, apiSecret);

try {
SearchImagesCreative search = client.searchimagescreative()
.withPhrase("cat")
.withAgeOfPeople(EnumSet.of(AgeOfPeople.CHILD, AgeOfPeople.BABY,AgeOfPeople.ADULT))
.withPage(3);
String result = search.executeAsync();
System.out.print(result);

} catch (SdkException e) {
System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage());
System.exit(-1);
}
```

### Search editorial videos with phrase, fields, format available, and exclude nudity

```java
String apiKey = "API Key";
String apiSecret = "API Secret";
String userName = "Username";
String userPassword = "Password";

ApiClient client = ApiClient.GetApiClientWithResourceOwnerCredentials(apiKey, apiSecret, userName, userPassword);

try {
SearchVideosEditorial search = client.searchvideoseditorial()
.withPhrase("cat")
.withResponseFields(Arrays.asList("allowed_use","caption"))
.withFormatAvailable(FormatAvailable.HD)
.withExcludeNudity(true);
String result = search.executeAsync();
System.out.print(result);

} catch (SdkException e) {
System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage());
System.exit(-1);
}
String apiKey = "API Key";
String apiSecret = "API Secret";

ApiClient client = ApiClient.GetApiClientWithClientCredentials(apiKey, apiSecret);

try {
SearchVideosEditorial search = client.searchvideoseditorial()
.withPhrase("cat")
.withResponseFields(Arrays.asList("allowed_use","caption"))
.withFormatAvailable(FormatAvailable.HD)
.withExcludeNudity(true);
String result = search.executeAsync();
System.out.print(result);

} catch (SdkException e) {
System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage());
System.exit(-1);
}
```

### Search creative images with phrase, custom parameter, and customer header
```java
String apiKey = "API Key";
String apiSecret = "API Secret";
String userName = "Username";
String userPassword = "Password";

ApiClient client = ApiClient.GetApiClientWithResourceOwnerCredentials(apiKey, apiSecret, userName, userPassword);

try {
SearchImagesCreative search = client.searchimagescreative()
.withPhrase("cat")
.withCustomParameter("safe_search", "true")
.withCustomHeader("gi-country-code", "CAN");
String result = search.executeAsync();
System.out.print(result);

} catch (SdkException e) {
System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage());
System.exit(-1);
}
String apiKey = "API Key";
String apiSecret = "API Secret";

ApiClient client = ApiClient.GetApiClientWithClientCredentials(apiKey, apiSecret);

try {
SearchImagesCreative search = client.searchimagescreative()
.withPhrase("cat")
.withCustomParameter("safe_search", "true")
.withCustomHeader("gi-country-code", "CAN");
String result = search.executeAsync();
System.out.print(result);

} catch (SdkException e) {
System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage());
System.exit(-1);
}
```

### Custom Request to search images with phrase, fields, and age of people

```java
String apiKey = "API Key";
String apiSecret = "API Secret";
String userName = "Username";
String userPassword = "Password";

ApiClient client = ApiClient.GetApiClientWithResourceOwnerCredentials(apiKey, apiSecret, userName, userPassword);

Map params = new HashMap();
params.put("phrase", "cat");
params.put("fields", Arrays.asList("artist", "id"));
params.put("age_of_people", EnumSet.of(AgeOfPeople.NEWBORN,AgeOfPeople.BABY,AgeOfPeople.CHILD));

try {
CustomRequest search = client.customrequest()
.withMethod("GET")
.withRoute("/search/images")
.withQueryParameters(params);
String result = search.executeAsync();
System.out.print(result);

} catch (SdkException e) {
System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage());
System.exit(-1);
}
String apiKey = "API Key";
String apiSecret = "API Secret";

ApiClient client = ApiClient.GetApiClientWithClientCredentials(apiKey, apiSecret);

Map params = new HashMap();
params.put("phrase", "cat");
params.put("fields", Arrays.asList("artist", "id"));
params.put("age_of_people", EnumSet.of(AgeOfPeople.NEWBORN,AgeOfPeople.BABY,AgeOfPeople.CHILD));

try {
CustomRequest search = client.customrequest()
.withMethod("GET")
.withRoute("/search/images")
.withQueryParameters(params);
String result = search.executeAsync();
System.out.print(result);

} catch (SdkException e) {
System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage());
System.exit(-1);
}
```

For more examples, see unittests package.
34 changes: 15 additions & 19 deletions src/main/java/com/gettyimages/api/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ public class ApiClient {
public static String Version="2.1.1";
private String Slash = "/";
private Credentials credentials;
private static String baseUrl = "https://api.gettyimages.com/v3";
private static String apiBaseUrl = "https://api.gettyimages.com/v3";
private static String authenticationBaseUrl = "https://authentication.gettyimages.com";

private ApiClient(String apiKey, String apiSecret) {
credentials = Credentials.GetInstance(apiKey, apiSecret, GetOAuthBaseUrl());
credentials = Credentials.GetInstance(apiKey, apiSecret, authenticationBaseUrl);
}

private ApiClient(String apiKey, String apiSecret, String userName, String userPassword) {
credentials = Credentials.GetInstance(apiKey, apiSecret, userName, userPassword, GetOAuthBaseUrl());
credentials = Credentials.GetInstance(apiKey, apiSecret, userName, userPassword, authenticationBaseUrl);
}

public static ApiClient GetApiClientWithClientCredentials(String apiKey, String apiSecret)
Expand All @@ -34,54 +35,49 @@ public static ApiClient GetApiClientWithResourceOwnerCredentials(String apiKey,
return new ApiClient(apiKey, apiSecret, userName, password);
}

private String GetOAuthBaseUrl() {
String oAuthBaseUrl = baseUrl.substring(0, baseUrl.lastIndexOf(Slash));
return oAuthBaseUrl;
}

public Images images()
{
return Images.GetInstance(credentials, baseUrl);
return Images.GetInstance(credentials, apiBaseUrl);
}

public Videos videos()
{
return Videos.GetInstance(credentials, baseUrl);
return Videos.GetInstance(credentials, apiBaseUrl);
}

public SearchImages searchimages() {
return SearchImages.GetInstance(credentials, baseUrl);
return SearchImages.GetInstance(credentials, apiBaseUrl);
}

public SearchImagesCreative searchimagescreative() {
return SearchImagesCreative.GetInstance(credentials, baseUrl);
return SearchImagesCreative.GetInstance(credentials, apiBaseUrl);
}

public SearchImagesEditorial searchimageseditorial() {
return SearchImagesEditorial.GetInstance(credentials, baseUrl);
return SearchImagesEditorial.GetInstance(credentials, apiBaseUrl);
}

public SearchVideos searchvideos() {
return SearchVideos.GetInstance(credentials, baseUrl);
return SearchVideos.GetInstance(credentials, apiBaseUrl);
}

public SearchVideosCreative searchvideoscreative() {
return SearchVideosCreative.GetInstance(credentials, baseUrl);
return SearchVideosCreative.GetInstance(credentials, apiBaseUrl);
}

public SearchVideosEditorial searchvideoseditorial() {
return SearchVideosEditorial.GetInstance(credentials, baseUrl);
return SearchVideosEditorial.GetInstance(credentials, apiBaseUrl);
}

public DownloadVideos downloadvideos() {
return DownloadVideos.GetInstance(credentials, baseUrl);
return DownloadVideos.GetInstance(credentials, apiBaseUrl);
}

public DownloadImages downloadimages() {
return DownloadImages.GetInstance(credentials, baseUrl);
return DownloadImages.GetInstance(credentials, apiBaseUrl);
}

public CustomRequest customrequest() {
return CustomRequest.GetInstance(credentials, baseUrl);
return CustomRequest.GetInstance(credentials, apiBaseUrl);
}
}
32 changes: 10 additions & 22 deletions src/test/java/AuthFailureTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import java.lang.reflect.Field;

import com.gettyimages.api.ApiClient;
import com.gettyimages.api.HttpClientErrorException;
import com.gettyimages.api.HttpSystemErrorException;
Expand All @@ -15,18 +12,12 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockserver.client.server.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.Parameter;

public class AuthFailureTest {
private ClientAndServer mockServer;

public class AuthFailureTest extends TestBase {
@BeforeEach
public void startProxy() throws Exception {
final Field field = ApiClient.class.getDeclaredField("baseUrl");
field.setAccessible(true);
field.set(null, "http://127.0.0.1:1080/");
mockServer = startClientAndServer(1080);
startMockServers();
}

@Test
Expand All @@ -38,7 +29,7 @@ void clientErrorOnAuthentication() throws Exception {
.withPhrase("cat");

final HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () -> { search.executeAsync(); } );
assertEquals(exception.getStatusCode(), statusCode);
assertEquals(statusCode, exception.getStatusCode());
}

@Test
Expand All @@ -50,22 +41,19 @@ void systemErrorOnAuthentication() throws Exception {
.withPhrase("cat");

final HttpSystemErrorException exception = assertThrows(HttpSystemErrorException.class, () -> { search.executeAsync(); } );
assertEquals(exception.getStatusCode(), statusCode);
assertEquals(statusCode, exception.getStatusCode());
}


@AfterEach
public void stopProxy() {
mockServer.stop();
stopMockServers();
}

private void createMock(final int statusCode) {
final MockServerClient client = new MockServerClient("127.0.0.1", 1080);

client.when(request().withMethod("POST").withPath("/oauth2/token"))
.respond(response().withStatusCode(statusCode));
client.when(request().withMethod("GET").withPath("/search/images")
.withQueryStringParameters(new Parameter("phrase", "cat")))
.respond(response().withStatusCode(200).withBody("success"));
authClientMock.when(request().withMethod("POST").withPath("/oauth2/token"))
.respond(response().withStatusCode(statusCode));
apiClientMock.when(request().withMethod("GET").withPath("/search/images")
.withQueryStringParameters(new Parameter("phrase", "cat")))
.respond(response().withStatusCode(200).withBody("success"));
}
}
Loading

0 comments on commit 43a4a63

Please sign in to comment.