Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android: Update examples to display errors with a red background #166

Merged
merged 9 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
53 changes: 27 additions & 26 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 @@ -33,8 +36,6 @@ public class MainActivity extends Activity {

private HandlerThread thread = new HandlerThread(REQUEST_HANDLER_THREAD_NAME);

private Envoy envoy;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -44,14 +45,14 @@ 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) {
Log.d("MainActivity", "exception getting config.", e);
throw new RuntimeException("Can't get config to run envoy.");
}
envoy = new Envoy(context, config);
Envoy envoy = new Envoy(context, config);
buildbreaker marked this conversation as resolved.
Show resolved Hide resolved

recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Expand All @@ -68,12 +69,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 +83,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",
goaway marked this conversation as resolved.
Show resolved Hide resolved
],
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