A simple API that reads key value config from Google Sheets.
- Create a new Google project. https://console.developers.google.com
- Enable Google Sheets API for you project.
- Add credentials to your project.
You need to add an authorized redirect URI like
http://example.com:4040/callback. - Download credentials file and save it as
credentials.json. go get github.com/doorbash/remote-config- Edit
main.goand setSPREADSHEETconst as your spreadsheet id. go build- Put
credentials.jsonnext tomain.go. ./remote-config- Visit
http://example.com:4040/loginand login with your Google account.
Put your data in two columns: (A=key, B=value)
http://example.com:4040/Sheet1
{
"key1":"value1",
"key10":"t",
"key11":false,
"key2":3.14,
"key3":4,
"key4":true,
"key5":0,
"key6":1,
"key7":"",
"key8":null,
"key9":"\"true\""
}
http://example.com:4040/Sheet1?key=key4
true
http://example.com:4040/Sheet1/metrics
remote_config{key="key2"} 3.14
remote_config{key="key3"} 4
remote_config{key="key5"} 0
remote_config{key="key11"} 0
remote_config{key="key4"} 1
remote_config{key="key6"} 1
private class GetConfigAsyncTask extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... urls) {
try {
Log.d(TAG, "sending get request...");
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.connect();
int status = connection.getResponseCode();
Log.d(TAG, "status code is " + status);
if (status == HttpURLConnection.HTTP_OK) {
InputStream is = connection.getInputStream();
return new BufferedReader(new InputStreamReader(is)).readLine();
} else {
InputStream is = connection.getErrorStream();
throw new Exception(new BufferedReader(new InputStreamReader(is)).readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String result) {
if (result != null) {
try {
Log.d(TAG, "result is " + result);
SharedPreferences.Editor editor = getApplicationContext()
.getSharedPreferences("MyPref", 0).edit();
JSONObject jo = new JSONObject(result);
Iterator<String> it = jo.keys();
while (it.hasNext()) {
String key = it.next();
Object value = jo.get(key);
if (value instanceof Boolean) {
editor.putBoolean(key, (boolean) value);
} else if (value instanceof Integer) {
editor.putInt(key, (int) value);
} else if (value instanceof Long) {
editor.putLong(key, (long) value);
} else if (value instanceof Float) {
editor.putFloat(key, (float) value);
} else if (value instanceof Double) {
editor.putFloat(key, ((Double) value).floatValue());
} else if (value instanceof String) {
editor.putString(key, (String) value);
} else if(value.equals(JSONObject.NULL)){
editor.remove(key);
}
}
editor.apply();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}new GetConfigAsyncTask().execute("http://example.com:4040/Sheet1");MIT
