Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
59 changes: 59 additions & 0 deletions WordPress/Classes/Networking/PulseMiddleware.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Pulse
import WordPressAPI
import WordPressAPIInternal

final class PulseMiddleware: Middleware {

private static let errorStatusCodes: [UInt16] = [
400, 401, 402, 403, 404, 419, 429,
500
]

func process(
requestExecutor: any RequestExecutor,
response: WpNetworkResponse,
request: WpNetworkRequest,
context: RequestContext?
) async throws -> WpNetworkResponse {

LoggerStore.shared.storeRequest(
convertToUrlRequest(request),
response: try convertToUrlResponse(response),
error: Self.errorStatusCodes.contains(response.statusCode) ? parseBodyAsError(response.body) : nil,
data: response.body
)
return response
}

private func convertToUrlRequest(_ original: WpNetworkRequest) -> URLRequest {
let url = URL(string: original.url())!
var request = URLRequest(url: url)
request.httpMethod = "\(original.method())"
request.allHTTPHeaderFields = original.headerMap().toFlatMap()
request.httpBody = original.body()?.contents()
return request
}

private func convertToUrlResponse(_ original: WpNetworkResponse) throws -> URLResponse? {
HTTPURLResponse(
url: try original.requestUrl.asURL(),
statusCode: Int(original.statusCode),
httpVersion: nil,
headerFields: original.responseHeaderMap.toFlatMap()
)
}

// TODO: This implementation should probably use the underlying Rust implementation

Check warning on line 46 in WordPress/Classes/Networking/PulseMiddleware.swift

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=wordpress-mobile_WordPress-iOS&issues=AZq274KURcpTkwSKUv_7&open=AZq274KURcpTkwSKUv_7&pullRequest=25005
private func parseBodyAsError(_ data: Data) -> Error? {
try? JSONDecoder().decode(WpError.self, from: data)
}

struct WpError: Codable, Error {
let code: Int
let message: String

var description: String {
message
}
}
}
5 changes: 4 additions & 1 deletion WordPress/Classes/Networking/WordPressClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ extension WordPressClient {
urlSession: session,
apiUrlResolver: resolver,
authenticationProvider: provider,
appNotifier: notifier
middlewarePipeline: MiddlewarePipeline(middlewares: [
PulseMiddleware()
]),
appNotifier: notifier,
)
self.init(api: api, rootUrl: apiRootURL)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extension EditorConfiguration {
// Limited to Jetpack-connected sites until editor assets endpoint is available in WordPress core
.setShouldUsePlugins(Self.shouldEnablePlugins(for: blog, appPassword: applicationPassword))
.setLocale(WordPressComLanguageDatabase.shared.deviceLanguage.slug)
.setEnableNetworkLogging(true)

if let blogUrl = blog.url {
builder = builder.setSiteUrl(blogUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

let configuration = EditorConfigurationBuilder(content: initialContent ?? "")
.setShouldHideTitle(true)
.setEnableNetworkLogging(true)
.build()

let editorVC = GutenbergKit.EditorViewController(configuration: configuration)
Expand All @@ -61,6 +62,7 @@
}

extension CommentGutenbergEditorViewController: GutenbergKit.EditorViewControllerDelegate {

func editorDidLoad(_ viewContoller: GutenbergKit.EditorViewController) {
// Do nothing
}
Expand Down Expand Up @@ -108,4 +110,9 @@
func editor(_ viewController: GutenbergKit.EditorViewController, didCloseModalDialog dialogType: String) {
// Do nothing
}

func editor(_ viewController: GutenbergKit.EditorViewController, didLogNetworkRequest request: GutenbergKit.NetworkRequest) {

}

Check failure on line 116 in WordPress/Classes/ViewRelated/Comments/Controllers/Editor/CommentGutenbergEditorViewController.swift

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a nested comment explaining why this function is empty, or complete the implementation.

See more on https://sonarcloud.io/project/issues?id=wordpress-mobile_WordPress-iOS&issues=AZq274E7RcpTkwSKUv_6&open=AZq274E7RcpTkwSKUv_6&pullRequest=25005

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import WordPressShared
import WebKit
import CocoaLumberjackSwift
import Photos
import Pulse

class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor {

Expand Down Expand Up @@ -527,6 +528,7 @@ class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor
}

extension NewGutenbergViewController: GutenbergKit.EditorViewControllerDelegate {

func editorDidLoad(_ viewContoller: GutenbergKit.EditorViewController) {
if !editorSession.started {
// Note that this method is also used to track startup performance
Expand Down Expand Up @@ -681,6 +683,32 @@ extension NewGutenbergViewController: GutenbergKit.EditorViewControllerDelegate

return WPMediaType(rawValue: mediaType)
}

func editor(_ viewController: GutenbergKit.EditorViewController, didLogNetworkRequest request: NetworkRequest) {

guard let url = URL(string: request.url) else {
return
}

var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = request.method
urlRequest.allHTTPHeaderFields = request.requestHeaders
urlRequest.httpBody = request.requestBody?.data(using: .utf8)

let httpResponse = HTTPURLResponse(
url: url,
statusCode: request.status,
httpVersion: nil,
headerFields: request.responseHeaders
)

LoggerStore.shared.storeRequest(
urlRequest,
response: httpResponse,
error: nil,
data: request.responseBody?.data(using: .utf8)
)
}
}

// MARK: - GutenbergBridgeDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<EnvironmentVariable
key = "GUTENBERG_EDITOR_URL"
value = "http://localhost:5173/"
isEnabled = "NO">
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
Expand Down
Loading