Skip to content

Commit

Permalink
android: Update examples to display errors with a red background (#166)
Browse files Browse the repository at this point in the history
This is just a change to allow us to more easily visualize when Envoy fails. We're repurposing the examples here to also display this. This way others can also install the example application and be able to visualize the success/failures of Envoy in the wild.

Changes:
1. An error view item background is: #ff3651
1. Added a request count on the top of the item view (Screen below)
1. Moved all the duplicated classes for java and kotlin examples under a shared lib

Related to #129: App lifecycle & network condition handling.
For an explanation of how to fill out the fields, please see the relevant section
in [PULL_REQUESTS.md](https://github.com/envoyproxy/envoy/blob/master/PULL_REQUESTS.md)

Description: Update examples to display errors with a red background
Risk Level: low
Testing: manual
Docs Changes: n/a
Release Notes: na/
[Optional Fixes #Issue]
[Optional Deprecated:]

![Screenshot_20190621-164638](https://user-images.githubusercontent.com/7771562/59956309-74d03580-9444-11e9-8c0e-9c73c9beddc1.png)
  • Loading branch information
Alan Chiu authored Jun 25, 2019
1 parent 4012c3e commit a5916eb
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 173 deletions.
9 changes: 5 additions & 4 deletions examples/java/hello_world/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ android_binary(
name = "hello_envoy",
srcs = [
"MainActivity.java",
"Response.java",
"ResponseRecyclerViewAdapter.java",
"ResponseViewHolder.java",
],
custom_package = "io.envoyproxy.envoymobile.helloenvoy",
manifest = "AndroidManifest.xml",
resource_files = glob(["res/**"]),
resource_files = [
"res/layout/activity_main.xml",
"res/raw/config.yaml",
],
deps = [
"//dist:envoy_mobile_android",
"//examples/kotlin/shared:hello_envoy_shared_lib",
"@androidsdk//com.android.support:appcompat-v7-25.0.0",
"@androidsdk//com.android.support:recyclerview-v7-25.0.0",
],
Expand Down
52 changes: 27 additions & 25 deletions examples/java/hello_world/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import io.envoyproxy.envoymobile.Envoy;
import io.envoyproxy.envoymobile.shared.Failure;
import io.envoyproxy.envoymobile.shared.Response;
import io.envoyproxy.envoymobile.shared.ResponseRecyclerViewAdapter;
import io.envoyproxy.envoymobile.shared.Success;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -19,8 +24,6 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.envoyproxy.envoymobile.Envoy;

public class MainActivity extends Activity {
private static final String ENDPOINT =
"http://0.0.0.0:9001/api.lyft.com/static/demo/hello_world.txt";
Expand All @@ -29,12 +32,11 @@ public class MainActivity extends Activity {

private static final String REQUEST_HANDLER_THREAD_NAME = "hello_envoy_java";

private Envoy envoy;
private RecyclerView recyclerView;

private HandlerThread thread = new HandlerThread(REQUEST_HANDLER_THREAD_NAME);

private Envoy envoy;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -44,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
Envoy.load(context);

// Create envoy instance with config.
String config = null;
String config;
try {
config = loadEnvoyConfig(getBaseContext(), R.raw.config);
} catch (RuntimeException e) {
Expand All @@ -68,12 +70,8 @@ protected void onCreate(Bundle savedInstanceState) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
final Response response = makeRequest();
recyclerView.post((Runnable)() -> adapter.add(response));
} catch (IOException e) {
Log.d("MainActivity", "exception making request.", e);
}
final Response response = makeRequest();
recyclerView.post((Runnable)() -> adapter.add(response));

// Make a call again
handler.postDelayed(this, TimeUnit.SECONDS.toMillis(1));
Expand All @@ -86,21 +84,25 @@ protected void onDestroy() {
thread.quit();
}

private Response makeRequest() throws IOException {
URL url = new URL(ENDPOINT);
// Open connection to the envoy thread listening locally on port 9001.
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
int status = connection.getResponseCode();
if (status != 200) {
throw new IOException("non 200 status: " + status);
private Response makeRequest() {
try {
URL url = new URL(ENDPOINT);
// Open connection to the envoy thread listening locally on port 9001.
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
int status = connection.getResponseCode();
if (status == 200) {
List<String> serverHeaderField = connection.getHeaderFields().get(ENVOY_SERVER_HEADER);
InputStream inputStream = connection.getInputStream();
String body = deserialize(inputStream);
inputStream.close();
return new Success(body,
serverHeaderField != null ? String.join(", ", serverHeaderField) : "");
} else {
return new Failure("failed with status " + status);
}
} catch (Exception e) {
return new Failure(e.getMessage());
}

List<String> serverHeaderField = connection.getHeaderFields().get(ENVOY_SERVER_HEADER);
InputStream inputStream = connection.getInputStream();
String body = deserialize(inputStream);
inputStream.close();
return new Response(body,
serverHeaderField != null ? String.join(", ", serverHeaderField) : "");
}

private String deserialize(InputStream inputStream) throws IOException {
Expand Down
12 changes: 0 additions & 12 deletions examples/java/hello_world/Response.java

This file was deleted.

37 changes: 0 additions & 37 deletions examples/java/hello_world/ResponseRecyclerViewAdapter.java

This file was deleted.

23 changes: 0 additions & 23 deletions examples/java/hello_world/ResponseViewHolder.java

This file was deleted.

22 changes: 0 additions & 22 deletions examples/java/hello_world/res/layout/item.xml

This file was deleted.

12 changes: 5 additions & 7 deletions examples/kotlin/hello_world/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ kt_android_library(
name = "hello_envoy_kt_lib",
srcs = [
"MainActivity.kt",
"Response.kt",
"ResponseRecyclerViewAdapter.kt",
"ResponseViewHolder.kt",
],
custom_package = "io.envoyproxy.envoymobile.helloenvoykotlin",
manifest = "AndroidManifest.xml",
resource_files = glob([
"res/**/*.xml",
"res/**/*.yaml",
]),
resource_files = [
"res/layout/activity_main.xml",
"res/raw/config.yaml",
],
deps = [
"//dist:envoy_mobile_android",
"//examples/kotlin/shared:hello_envoy_shared_lib",
"@androidsdk//com.android.support:appcompat-v7-25.0.0",
"@androidsdk//com.android.support:recyclerview-v7-25.0.0",
],
Expand Down
40 changes: 23 additions & 17 deletions examples/kotlin/hello_world/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log

import io.envoyproxy.envoymobile.Envoy
import io.envoyproxy.envoymobile.shared.Failure
import io.envoyproxy.envoymobile.shared.Response
import io.envoyproxy.envoymobile.shared.ResponseRecyclerViewAdapter
import io.envoyproxy.envoymobile.shared.Success
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
import java.util.concurrent.TimeUnit

import io.envoyproxy.envoymobile.Envoy

private const val REQUEST_HANDLER_THREAD_NAME = "hello_envoy_kt"
private const val ENDPOINT = "http://0.0.0.0:9001/api.lyft.com/static/demo/hello_world.txt"
private const val ENVOY_SERVER_HEADER = "server"
Expand All @@ -32,7 +34,7 @@ class MainActivity : Activity() {
setContentView(R.layout.activity_main)

Envoy.load(baseContext)

// Create Envoy instance with config.
envoy = Envoy(baseContext, loadEnvoyConfig(baseContext, R.raw.config))

Expand All @@ -51,7 +53,7 @@ class MainActivity : Activity() {
override fun run() {
try {
val response = makeRequest()
recyclerView.post({ adapter.add(response) })
recyclerView.post { adapter.add(response) }
} catch (e: IOException) {
Log.d("MainActivity", "exception making request.", e)
}
Expand All @@ -68,19 +70,23 @@ class MainActivity : Activity() {
}

private fun makeRequest(): Response {
val url = URL(ENDPOINT)
// Open connection to the envoy thread listening locally on port 9001
val connection = url.openConnection() as HttpURLConnection
val status = connection.responseCode
if (status != 200) {
throw IOException("non 200 status: $status")
return try {
val url = URL(ENDPOINT)
// Open connection to the envoy thread listening locally on port 9001
val connection = url.openConnection() as HttpURLConnection
val status = connection.responseCode
if (status == 200) {
val serverHeaderField = connection.headerFields[ENVOY_SERVER_HEADER]
val inputStream = connection.inputStream
val body = deserialize(inputStream)
inputStream.close()
Success(body, serverHeaderField?.joinToString(separator = ", ") ?: "")
} else {
Failure("failed with status: $status")
}
} catch (e: IOException) {
Failure(e.message ?: "failed with exception")
}

val serverHeaderField = connection.headerFields[ENVOY_SERVER_HEADER]
val inputStream = connection.inputStream
val body = deserialize(inputStream)
inputStream.close()
return Response(body, serverHeaderField?.joinToString(separator = ", ") ?: "")
}

private fun deserialize(inputStream: InputStream): String {
Expand Down
4 changes: 0 additions & 4 deletions examples/kotlin/hello_world/Response.kt

This file was deleted.

15 changes: 0 additions & 15 deletions examples/kotlin/hello_world/ResponseViewHolder.kt

This file was deleted.

5 changes: 0 additions & 5 deletions examples/kotlin/hello_world/res/values/strings.xml

This file was deleted.

5 changes: 5 additions & 0 deletions examples/kotlin/shared/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.envoyproxy.envoymobile.shared"
android:versionCode="1"
android:versionName="0.1">
</manifest>
25 changes: 25 additions & 0 deletions examples/kotlin/shared/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
licenses(["notice"]) # Apache 2

load("@envoy//bazel:envoy_build_system.bzl", "envoy_package")
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library")

envoy_package()

kt_android_library(
name = "hello_envoy_shared_lib",
srcs = [
"Response.kt",
"ResponseRecyclerViewAdapter.kt",
"ResponseViewHolder.kt",
],
custom_package = "io.envoyproxy.envoymobile.shared",
manifest = "AndroidManifest.xml",
resource_files = glob([
"res/layout/item.xml",
"res/values/colors.xml",
"res/values/strings.xml",
]),
deps = [
"@androidsdk//com.android.support:recyclerview-v7-25.0.0",
],
)
16 changes: 16 additions & 0 deletions examples/kotlin/shared/Response.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.envoyproxy.envoymobile.shared

// Response is a class to handle HTTP responses.
sealed class Response {
fun fold(success: (Success) -> Unit, failure: (Failure) -> Unit) = when (this) {
is Success -> success(this)
is Failure -> failure(this)
}
}

data class Success(val title: String, val header: String) : Response()

data class Failure(val message: String) : Response()



Loading

0 comments on commit a5916eb

Please sign in to comment.