Skip to content

Commit

Permalink
Endpoint URL is now hold in RequestFactory class
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Mar 13, 2022
1 parent 30d123f commit 0ad3799
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 32 deletions.
21 changes: 3 additions & 18 deletions src/main/java/tokyo/northside/oxfordapi/OxfordClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
* @author Hiroshi Miura
*/
public class OxfordClient extends OxfordClientBase {

private static final String BASE_URL = "https://od-api.oxforddictionaries.com/";
private final String endpointUrl;
private final String appId;
private final String appKey;

Expand All @@ -35,22 +32,10 @@ public class OxfordClient extends OxfordClientBase {
*
* @param appId AppId of the OD API credentials.
* @param appKey AppKey of the OD API credentials.
* @param baseUrl base URL of the OD API v2.
*/
public OxfordClient(final String appId, final String appKey, final String baseUrl) {
public OxfordClient(final String appId, final String appKey) {
this.appId = appId;
this.appKey = appKey;
endpointUrl = baseUrl;
}

/**
* Construcotr with default v2 URL.
*
* @param appId AppId of the OD API credentials.
* @param appKey AppKey of the OD API credentials.
*/
public OxfordClient(final String appId, final String appKey) {
this(appId, appKey, BASE_URL);
}

@Override
Expand All @@ -76,7 +61,7 @@ public Map<String, List<Result>> queryTranslations(final Collection<String> word
public List<Result> queryTranslation(final String word, final String source, final String target)
throws OxfordClientException {
Set<String> fields = new HashSet<>(Arrays.asList("definitions", "pronunciations"));
RequestFactory f = new RequestFactory(appId, appKey, endpointUrl)
RequestFactory f = new RequestFactory(appId, appKey)
.setType(RequestFactory.QueryType.TRANSLATIONS)
.setSourceLanguage(source)
.setTargetLanguage(target)
Expand Down Expand Up @@ -107,7 +92,7 @@ public Map<String, List<Result>> queryEntries(final Collection<String> words, fi
@Override
public List<Result> queryEntry(final String word, final String language, final boolean strict)
throws OxfordClientException {
RequestFactory f = new RequestFactory(appId, appKey, endpointUrl)
RequestFactory f = new RequestFactory(appId, appKey)
.setType(RequestFactory.QueryType.ENTRIES)
.setLanguage(language)
.setQueryWord(word)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* @author Hiroshi Miura
*/
public class OxfordThreadClient extends OxfordClientBase {
private static final String ENDPOINT_URL = "https://od-api.oxforddictionaries.com/api/v2/";
private final String appId;
private final String appKey;

Expand All @@ -42,7 +41,7 @@ public Map<String, List<Result>> queryEntries(final Collection<String> words, fi
.build()) {
final List<RequestFactory> requests = new ArrayList<>();
for (final String query : words) {
RequestFactory factory = new RequestFactory(appId, appKey, ENDPOINT_URL);
RequestFactory factory = new RequestFactory(appId, appKey);
factory.setQueryWord(query).setLanguage(language).setStrictMatch(strict);
requests.add(factory);
}
Expand All @@ -63,7 +62,7 @@ public Map<String, List<Result>> queryTranslations(final Collection<String> word
.build()) {
List<RequestFactory> requests = new ArrayList<>();
for (final String query : words) {
RequestFactory factory = new RequestFactory(appId, appKey, ENDPOINT_URL);
RequestFactory factory = new RequestFactory(appId, appKey);
factory.setQueryWord(query).setSourceLanguage(source).setTargetLanguage(target);
requests.add(factory);
}
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/tokyo/northside/oxfordapi/RequestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public enum QueryType {
TRANSLATIONS,
}

private static final String ENDPOINT_URL = "https://od-api.oxforddictionaries.com";
private static final String BASE_PATH = "/api/v2/";
private final String endpointUrl;
private final String appId;
private final String appKey;

Expand All @@ -39,12 +39,7 @@ public enum QueryType {
*/
private Set<String> fields;

public RequestFactory(final String appId, final String appKey, final String endpointHost) {
if (endpointHost.endsWith("/")) {
this.endpointUrl = endpointHost.substring(0, endpointHost.length() - 1);
} else {
this.endpointUrl = endpointHost;
}
public RequestFactory(final String appId, final String appKey) {
this.appId = appId;
this.appKey = appKey;
strictMatch = "false";
Expand Down Expand Up @@ -159,7 +154,7 @@ public String getUrl() throws OxfordClientException {
if (queryWord == null) {
throw new OxfordClientException("Query word is mandatory");
}
return String.format("%s%s?%s", endpointUrl, getPath(), getQueryString());
return String.format("%s%s?%s", ENDPOINT_URL, getPath(), getQueryString());
}

public String getQueryWord() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ import tokyo.northside.oxfordapi.dtd.Result
import static org.junit.Assert.assertEquals

class OxfordClientTest {
def ENDPOINT_URL = "https://od-api.oxforddictionaries.com/"
def appId = System.properties.getProperty("oxfordId")
def appKey = System.properties.getProperty("oxfordKey")

@Test
void simpleQueryTest() {
OxfordClient client = new OxfordClient(appId, appKey, ENDPOINT_URL)
OxfordClient client = new OxfordClient(appId, appKey)
List<Result> result = client.queryEntry("ace", "en-GB", true)
assertEquals(2, result.size())
client.close()
}

@Test
void queryResultTest() {
OxfordClient client = new OxfordClient(appId, appKey, ENDPOINT_URL)
OxfordClient client = new OxfordClient(appId, appKey)
List<OxfordDictionaryEntry> result = client.getDefinitions(Collections.singletonList("ace"), "en-GB", true)
assertEquals(5, result.size())
client.close()
Expand Down

0 comments on commit 0ad3799

Please sign in to comment.