refactor: extract duplicated code into shared utility classes by devin-ai-integration[bot] · Pull Request #2 · oneky/NewBingGoGo-MagicURL-java · GitHub
Skip to content

refactor: extract duplicated code into shared utility classes#2

Open
devin-ai-integration[bot] wants to merge 1 commit into
masterfrom
devin/1782311922-refactor-shared-utilities
Open

refactor: extract duplicated code into shared utility classes#2
devin-ai-integration[bot] wants to merge 1 commit into
masterfrom
devin/1782311922-refactor-shared-utilities

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

Extracted repeated patterns across NewBingGoGoServer and NewBingGoGoClientWebSocket into three shared utility classes under cn.jja8.newbinggogo.util:

JsonUtils — general-purpose string helpers:

  • escapeJsonString(String) — JSON string escaping (moved from NewBingGoGoServer)
  • throwableToString(Throwable) — stack trace to string (moved from NewBingGoGoServer.printErrorToString)

ResponseBuilder — all error response construction:

  • buildWebSocketErrorJson(message) — the {"type":2,"result":{"value":"Error",...}} format used by both getReturnErrorWebSocket and NewBingGoGoClientWebSocket.onError
  • sendWebSocketErrorAndClose(ws, message) — the send-error-then-close pattern duplicated across both classes
  • httpError(...) overloads — the HTTP JSON error response (replaces getReturnError)

HttpProxyUtils — HTTP proxy forwarding:

  • proxyGet(session, url [, headers]) — the full proxy logic (URL open → copy headers → read response) previously inline in NewBingGoGoServer.goUrl
  • proxyImageRequest(session, url, referer) — eliminates the duplicated create-headers/set-mime-type pattern used by both /images/create and /images/create/async/results handlers

Also fixes a latent bug: NewBingGoGoClientWebSocket.onError() previously built error JSON without calling escapeJsonString, which could produce malformed JSON if the exception message contained quotes or backslashes. Now routes through ResponseBuilder.buildWebSocketErrorJson which always escapes.

NewBingGoGoServer shrinks from 252 → 108 lines; all routing logic is now ~30 lines of clean delegation.

Link to Devin session: https://app.devin.ai/sessions/476cc2aa4b54433e9bd06a53961958e9
Requested by: @oneky

- JsonUtils: escapeJsonString() and throwableToString()
- ResponseBuilder: WebSocket/HTTP error JSON construction and send-error-and-close pattern
- HttpProxyUtils: HTTP proxy logic and image proxy helper (proxyImageRequest)

Also fixes a bug where NewBingGoGoClientWebSocket.onError() did not
escape the error message JSON string.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@oneky oneky self-assigned this Jun 24, 2026
@devin-ai-integration

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant