-
-
Notifications
You must be signed in to change notification settings - Fork 475
open URLs on host machine #5977
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
Merged
0xadam-brown
merged 5 commits into
hackweek/sentry-buddy
from
feat/hackweek/sentry-buddy-open-urls-on-host
Aug 18, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1cb924e
open URLs on host machine
markushi 10b9c5d
Merge branch 'hackweek/sentry-buddy' into feat/hackweek/sentry-buddy-…
markushi b6f8e14
Merge branch 'hackweek/sentry-buddy' into feat/hackweek/sentry-buddy-…
markushi 44605b7
Merge branch 'hackweek/sentry-buddy' into feat/hackweek/sentry-buddy-…
markushi d9d1703
Merge remote-tracking branch 'origin/hackweek/sentry-buddy' into feat…
0xadam-brown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
sentry-android-buddy/src/main/java/io/sentry/android/buddy/SentryBuddyHttpOpenUrlApi.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package io.sentry.android.buddy | ||
|
|
||
| import android.content.Context | ||
| import io.sentry.ILogger | ||
| import io.sentry.JsonObjectReader | ||
| import io.sentry.JsonSerializable | ||
| import io.sentry.JsonSerializer | ||
| import io.sentry.ObjectWriter | ||
| import io.sentry.SentryOptions | ||
| import java.io.IOException | ||
| import java.io.StringReader | ||
| import java.io.StringWriter | ||
| import okhttp3.HttpUrl.Companion.toHttpUrl | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okhttp3.RequestBody.Companion.toRequestBody | ||
| import okhttp3.Response | ||
| import org.jetbrains.annotations.ApiStatus | ||
|
|
||
| @ApiStatus.Experimental | ||
| public class SentryBuddyHttpOpenUrlApi | ||
| @JvmOverloads | ||
| public constructor( | ||
| private val baseUrl: String, | ||
| private val client: OkHttpClient = OkHttpClient(), | ||
| ) : SentryBuddyOpenUrlApi { | ||
| private val json = JsonSerializer(SentryOptions()) | ||
|
|
||
| override fun open(context: Context, url: String) { | ||
| val httpRequest = | ||
| Request.Builder() | ||
| .url(baseUrl.toHttpUrl().newBuilder().addPathSegments("v1/open-url").build()) | ||
| .post(serialize(OpenUrlRequest(url)).toRequestBody(JSON_MEDIA_TYPE)) | ||
| .build() | ||
| execute(httpRequest) | ||
| } | ||
|
|
||
| private fun serialize(request: OpenUrlRequest): String { | ||
| val writer = StringWriter() | ||
| json.serialize(request, writer) | ||
| return writer.toString() | ||
| } | ||
|
|
||
| private fun execute(request: Request) { | ||
| try { | ||
| client.newCall(request).execute().use { response -> | ||
| if (!response.isSuccessful) { | ||
| throw IllegalStateException(response.errorMessage()) | ||
| } | ||
| } | ||
| } catch (exception: IOException) { | ||
| throw IllegalStateException( | ||
| "Failed to call open URL bridge: ${exception.message}", | ||
| exception, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun Response.errorMessage(): String { | ||
| val error = extractError(body?.string().orEmpty()) | ||
| return buildString { | ||
| append("Open URL bridge request failed with HTTP ").append(code) | ||
| error?.let { append(": ").append(it) } | ||
| } | ||
| } | ||
|
|
||
| private fun extractError(body: String): String? { | ||
| if (body.isBlank()) { | ||
| return null | ||
| } | ||
| return try { | ||
| (JsonObjectReader(StringReader(body)).use { it.nextObjectOrNull() } as? Map<*, *>) | ||
| ?.get("error") | ||
| ?.toString() | ||
| } catch (_: Exception) { | ||
| body.take(200) | ||
| } | ||
| } | ||
|
|
||
| private companion object { | ||
| private val JSON_MEDIA_TYPE = "application/json; charset=utf-8".toMediaType() | ||
| } | ||
| } | ||
|
|
||
| private data class OpenUrlRequest(val url: String) : JsonSerializable { | ||
| @Throws(IOException::class) | ||
| override fun serialize(writer: ObjectWriter, logger: ILogger) { | ||
| writer.beginObject() | ||
| writer.name("url").value(url) | ||
| writer.endObject() | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
sentry-android-buddy/src/main/java/io/sentry/android/buddy/SentryBuddyOpenUrlApi.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package io.sentry.android.buddy | ||
|
|
||
| import android.content.ActivityNotFoundException | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.net.Uri | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import org.jetbrains.annotations.ApiStatus | ||
|
|
||
| @ApiStatus.Experimental | ||
| public interface SentryBuddyOpenUrlApi { | ||
| public fun open(context: Context, url: String) | ||
| } | ||
|
|
||
| @ApiStatus.Experimental | ||
| public object DummySentryBuddyOpenUrlApi : SentryBuddyOpenUrlApi { | ||
| override fun open(context: Context, url: String) { | ||
| if (Looper.myLooper() != Looper.getMainLooper()) { | ||
| Handler(Looper.getMainLooper()).post { open(context, url) } | ||
| return | ||
| } | ||
| try { | ||
| context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) | ||
| } catch (_: ActivityNotFoundException) { | ||
| // A debug overlay should not crash the app when no browser can handle the link. | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.