Skip to content

Commit

Permalink
feat: add varargs methods for Set query params
Browse files Browse the repository at this point in the history
  • Loading branch information
astappiev committed Sep 22, 2023
1 parent 409f510 commit 0f46255
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void search() {
SearchQuery query = new SearchQuery();
query.setQuery("hello world");
query.setPerPage(20);
query.addContentType(ContentType.webpage);
query.setContentTypes(ContentType.webpage);
query.setDateFrom(LocalDate.of(2009, 1, 1));
query.setDateTo(LocalDate.of(2010, 6, 1));

Expand All @@ -67,7 +67,7 @@ void search() {
void searchImages() {
SearchQuery query = new SearchQuery();
query.setQuery("hannover");
query.addContentType(ContentType.image);
query.setContentTypes(ContentType.image);
query.setPerPage(30);
query.setPage(2);

Expand All @@ -88,7 +88,7 @@ void searchImages() {
void searchVideos() {
SearchQuery query = new SearchQuery();
query.setQuery("hannover");
query.addContentType(ContentType.video);
query.setContentTypes(ContentType.video);
query.setPerPage(30);

SearchConnectorResults queryResult = connector.search(query).await().indefinitely();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class FlickrConnectorTest {
void search() throws ConnectorException {
SearchQuery query = new SearchQuery();
query.setQuery("hello world");
query.addContentType(ContentType.image);
// query.addContentType(ContentType.video);
query.setContentTypes(ContentType.image);
// query.setContentTypes(ContentType.video);
query.setPerPage(50);
// query.setDateFrom("2009-01-01 00:00:00");
// query.setDateTo("2009-06-01 00:00:00");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GiphyConnectorTest {
void search() throws ConnectorException {
SearchQuery query = new SearchQuery();
query.setQuery("hello world");
query.addContentType(ContentType.image);
query.setContentTypes(ContentType.image);

SearchConnectorResults queryResult = connector.search(query).await().indefinitely();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class IpernityConnectorTest {
void search() throws ConnectorException {
SearchQuery query = new SearchQuery();
query.setQuery("tree");
query.addContentType(ContentType.image);
query.setContentTypes(ContentType.image);
query.setPerPage(20);
// query.setDateFrom("2009-01-01 00:00:00");
// query.setDateTo("2009-06-01 00:00:00");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ class SlideShareConnectorTest {
void search() throws ConnectorException {
SearchQuery query = new SearchQuery();
query.setQuery("hello world");
query.addContentType(ContentType.video);
query.addContentType(ContentType.image);
query.addContentType(ContentType.webpage);
query.addContentType(ContentType.presentation);
query.addContentType(ContentType.audio);
query.setContentTypes(ContentType.video, ContentType.image, ContentType.webpage, ContentType.presentation, ContentType.audio);
query.setPerPage(5);
// query.setDateFrom("2009-01-01 00:00:00");
// query.setDateTo("2009-06-01 00:00:00");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class VimeoConnectorTest {
void search() throws ConnectorException {
SearchQuery query = new SearchQuery();
query.setQuery("hello world");
query.addContentType(ContentType.video);
query.setContentTypes(ContentType.video);
query.setPerPage(20);
// query.setDateFrom("2009-01-01 00:00:00");
// query.setDateTo("2009-06-01 00:00:00");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ class YouTubeConnectorTest {
void search() throws ConnectorException {
SearchQuery query = new SearchQuery();
query.setQuery("hannover");
query.addContentType(ContentType.video);
query.setContentTypes(ContentType.video);
query.setPerPage(10);
// query.setDateFrom("2009-01-01 00:00:00");
// query.setDateTo("2009-06-01 00:00:00");
query.setSort(SearchSort.relevance);
// query.addExtra(SearchExtra.statistics);
query.addExtra(SearchExtra.duration);
query.addExtra(SearchExtra.tags);
query.setExtras(SearchExtra.duration, SearchExtra.tags);

SearchConnectorResults page = connector.search(query).await().indefinitely();
assertEquals(10, page.getItems().size());
Expand All @@ -51,7 +50,7 @@ void search() throws ConnectorException {
void searchChannel() throws ConnectorException {
SearchQuery query = new SearchQuery();
query.setQuery("user::ukrainernet kharkiv");
query.addContentType(ContentType.video);
query.setContentTypes(ContentType.video);
query.setPerPage(10);

SearchConnectorResults page = connector.search(query).await().indefinitely();
Expand All @@ -68,7 +67,7 @@ void searchPaging() throws ConnectorException {
SearchQuery query = new SearchQuery();
query.setQuery("hannover");
query.setPerPage(10);
query.addContentType(ContentType.video);
query.setContentTypes(ContentType.video);

for (int i = 1; i < 4; ++i) {
query.setPage(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.junit.jupiter.api.Assertions.*;

import java.util.Set;

import jakarta.inject.Inject;

import io.quarkus.test.junit.QuarkusTest;
Expand Down Expand Up @@ -38,11 +36,11 @@ void searchTest() throws InterwebException {
SearchQuery query = new SearchQuery();
query.setQuery("hannover");
query.setLanguage("en");
query.setContentTypes(Set.of(ContentType.video));
query.setServices(Set.of("Vimeo", "YouTube"));
query.setContentTypes(ContentType.video);
query.setServices("Vimeo", "YouTube");
query.setPerPage(32);
query.setPage(1);
query.setExtras(Set.of(SearchExtra.duration, SearchExtra.tags));
query.setExtras(SearchExtra.duration, SearchExtra.tags);

SearchResults response = interweb.search(query);
assertEquals(response.getResults().size(), 2);
Expand Down Expand Up @@ -82,7 +80,7 @@ void describeTest() throws InterwebException {
DescribeQuery query = new DescribeQuery();
// query.setLink("https://vimeo.com/524933864");
query.setId("524933864");
query.setServices(Set.of("vimeo"));
query.setServices("vimeo");

DescribeResults response = interweb.describe(query);

Expand Down
4 changes: 4 additions & 0 deletions interweb-core/src/main/java/de/l3s/interweb/core/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public void setServices(Set<String> services) {
this.services = services;
}

public void setServices(String ...services) {
this.services = Set.of(services);
}

public Set<String> getServices() {
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ public void setContentTypes(final Set<ContentType> contentTypes) {
this.contentTypes = contentTypes;
}

public void addContentType(ContentType contentType) {
contentTypes.add(contentType);
@JsonIgnore
public void setContentTypes(ContentType ...contentTypes) {
if (contentTypes == null) this.contentTypes = new HashSet<>();
else this.contentTypes = Set.of(contentTypes);
}

public Set<SearchExtra> getExtras() {
Expand All @@ -106,14 +108,10 @@ public void setExtras(final Set<SearchExtra> extras) {

@JsonIgnore
public void setExtras(final SearchExtra ...extras) {
if (extras == null || extras.length == 0) this.extras = new HashSet<>();
if (extras == null) this.extras = new HashSet<>();
else this.extras = Set.of(extras);
}

public void addExtra(SearchExtra part) {
extras.add(part);
}

public int getPage() {
if (page == null) return 1;
return page;
Expand All @@ -130,6 +128,10 @@ public int getPerPage(int fallback) {
return perPage == null ? fallback : Math.min(perPage, fallback);
}

/**
* @param perPage a desired number of results per page, the actual number of results per page may be less depending on the service.
* Prefer to use bigger values if you need second page, as it reduces API quotas usage. No value fallbacks to max value per service.
*/
public void setPerPage(final Integer perPage) {
this.perPage = perPage;
}
Expand Down Expand Up @@ -162,6 +164,9 @@ public Integer getTimeout() {
return timeout;
}

/**
* @param timeout in milliseconds used for the API call, default is 10 seconds
*/
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import jakarta.inject.Inject;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -47,7 +46,7 @@ public Uni<SearchResults> search(@Parameter(description = "The search query", ex
@Parameter(description = "An external request timeout in ms", example = "1000") @RestQuery("timeout") @Max(500) Integer timeout) {
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(query.trim());
searchQuery.setContentTypes(Set.of(contentTypes));
searchQuery.setContentTypes(contentTypes);
searchQuery.setServices(StringUtils.toIdSet(services));
searchQuery.setExtras(extras);
searchQuery.setDateFrom(dateFrom);
Expand Down

0 comments on commit 0f46255

Please sign in to comment.