Skip to content

Commit

Permalink
#19 GET SupportedLanguages implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jul 4, 2021
1 parent 6353d0e commit d415af1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/io/imagineobjects/linguinai/LinguinAi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #15:60min Implement the GET SupportedLanguages endpoint.
*/
public interface LinguinAi {

Expand All @@ -31,4 +30,10 @@ public interface LinguinAi {
* @return BulkDetection.
*/
BulkDetection bulkDetect(final String... texts);

/**
* List the languages supported by Linguin.ai.
* @return SupportedLanguages.
*/
SupportedLanguages languages();
}
38 changes: 38 additions & 0 deletions src/main/java/io/imagineobjects/linguinai/RestLinguinAi.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,42 @@ public String toString() {
);
}
}

@Override
public SupportedLanguages languages() {
final Resource resource = this.resources.get(
URI.create(this.baseUri + "/languages")
);
if(resource.statusCode() != HttpURLConnection.HTTP_OK) {
throw new IllegalStateException(
"Call to /languages returned status code "
+ resource.statusCode() + ", instead of 200 OK"
);
}
final List<SupportedLanguage> supported = new ArrayList<>();
final JsonObject json = resource.asJsonObject();
for(final String code : json.keySet()) {
supported.add(
new SupportedLanguage() {
@Override
public String code() {
return code;
}

@Override
public String[] englishNames() {
return json.getJsonArray(code)
.getString(0).split(";");
}

@Override
public String[] nativeNames() {
return json.getJsonArray(code)
.getString(1).split(";");
}
}
);
}
return () -> supported.iterator();
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/imagineobjects/linguinai/SupportedLanguage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.imagineobjects.linguinai;

/**
* Language supported by Linguin.ai.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public interface SupportedLanguage {

/**
* Language code.
* @return String.
*/
String code();

/**
* Name(s) of the language in english.
* @return String[].
*/
String[] englishNames();

/**
* Name(s) of the language in said language.
* @return String[].
*/
String[] nativeNames();

}
10 changes: 10 additions & 0 deletions src/main/java/io/imagineobjects/linguinai/SupportedLanguages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.imagineobjects.linguinai;

/**
* Languages supported by Linguin.ai.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public interface SupportedLanguages extends Iterable<SupportedLanguage> {
}

1 comment on commit d415af1

@zoeself
Copy link
Collaborator

@zoeself zoeself commented on d415af1 Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amihaiemil I've closed the Issues [#19] since their to-dos disappeared from the code.

The to-dos may have been removed in an earlier commit, but I've found it just now.

Please sign in to comment.