Skip to content

Commit

Permalink
Move Lexigram API key to properties (fixes #17)
Browse files Browse the repository at this point in the history
This ease key manipulation and allow injection via environment variable, which can be used not to skip tests during CI.
  • Loading branch information
michelole committed Jul 24, 2018
1 parent 41744c0 commit 6828282
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/main/java/at/medunigraz/imi/bst/config/TrecConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class TrecConfig {
public static final int ELASTIC_PORT = getInteger("ELASTIC_PORT");
public static final String ELASTIC_CLUSTER = getString("ELASTIC_CLUSTER");


public static final String LEXIGRAM_APIKEY = getString("LEXIGRAM_APIKEY");


/* DATA - MEDLINE */
Expand Down
22 changes: 6 additions & 16 deletions src/main/java/at/medunigraz/imi/bst/lexigram/Lexigram.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package at.medunigraz.imi.bst.lexigram;

import at.medunigraz.imi.bst.config.TrecConfig;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;

Expand All @@ -19,20 +19,6 @@ public class Lexigram {

private static final String ENDPOINT = "https://api.lexigram.io/v1/lexigraph/";

static String API_KEY_FILE = "src/main/resources/apikey.txt";
static String API_KEY;

static {
File apiFile = new File(API_KEY_FILE);

try {
API_KEY = FileUtils.readFileToString(apiFile, "UTF-8").replace("\n", "");
} catch (IOException e) {
System.out.print("Please place your Lexigram API key in the following file: " + API_KEY_FILE);
e.printStackTrace();
}
}

private static class Cache {
private static final String FILENAME = "cache/lexigram.ser";
private static HashMap<String, String> CALLS = new HashMap<>();
Expand Down Expand Up @@ -206,12 +192,16 @@ private static JSONObject get(String url) {
HttpResponse<JsonNode> response = null;
try {
response = Unirest.get(url)
.header("authorization", "Bearer " + API_KEY)
.header("authorization", "Bearer " + TrecConfig.LEXIGRAM_APIKEY)
.asJson();
} catch (UnirestException e) {
throw new RuntimeException(e);
}

if (response.getStatus() == 401) {
throw new RuntimeException("Unauthorized access to Lexigram API. Place your key in the file trec-pm.properties.");
}

JSONObject body = new JSONObject(response.getBody());
String firstArrayObject = body.getJSONArray("array").getJSONObject(0).toString();

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ ELASTIC_BA_INDEX=${elastic.ba.index}
ELASTIC_BA_MEDLINE_TYPE=${elastic.ba.medline.type}
ELASTIC_BA_EXTRA_TYPE=${elastic.ba.extra.type}
ELASTIC_CT_INDEX=${elastic.ct.index}
ELASTIC_CT_TYPE=${elastic.ct.type}
ELASTIC_CT_TYPE=${elastic.ct.type}
LEXIGRAM_APIKEY=${lexigram.apikey}
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
package at.medunigraz.imi.bst.conceptextraction;

import java.io.File;
import java.io.IOException;

import at.medunigraz.imi.bst.config.TrecConfig;
import at.medunigraz.imi.bst.lexigram.Lexigram;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.*;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;


public class ConceptExtractionTest {

String API_KEY_FILE = "src/main/resources/apikey.txt";
String API_KEY;

@Before
public void SetUp() {
File apiFile = new File(API_KEY_FILE);
Assume.assumeTrue(apiFile.exists());

try {
API_KEY = FileUtils.readFileToString(apiFile, "UTF-8").replace("\n", "");
} catch (IOException e) {
System.out.print("Please place your Lexigram API key in the following file: " + API_KEY_FILE);
e.printStackTrace();
}
Assume.assumeFalse(TrecConfig.LEXIGRAM_APIKEY.equalsIgnoreCase("secret")); // placeholder key
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion trec-pm.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ elastic.ba.index=trec
elastic.ba.medline.type=medline
elastic.ba.extra.type=extra
elastic.ct.index=clinicaltrials
elastic.ct.type=clinicaltrials
elastic.ct.type=clinicaltrials
lexigram.apikey=secret

0 comments on commit 6828282

Please sign in to comment.