Skip to content

Commit

Permalink
Merge pull request #908 from WalletConnect/feature/w3m-explorer-user-…
Browse files Browse the repository at this point in the history
…agent

[W3M] Custom User-Agent for metrics
  • Loading branch information
radeknovis authored Jun 16, 2023
2 parents 1af9a52 + 600fd9e commit da27232
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Sources/WalletConnectRelay/EnvironmentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ import UIKit
#endif
import Foundation

enum EnvironmentInfo {
public enum EnvironmentInfo {

static var userAgent: String {
public static var userAgent: String {
"\(protocolName)/\(sdkName)/\(operatingSystem)"
}

static var protocolName: String {
public static var protocolName: String {
"wc-2"
}

static var sdkName: String {
public static var sdkName: String {
"swift-v\(packageVersion)"
}

static var packageVersion: String {
public static var packageVersion: String {
let configURL = Bundle.resourceBundle.url(forResource: "PackageConfig", withExtension: "json")!
let jsonData = try! Data(contentsOf: configURL)
let config = try! JSONDecoder().decode(PackageConfig.self, from: jsonData)
return config.version
}

static var operatingSystem: String {
public static var operatingSystem: String {
#if os(iOS)
return "\(UIDevice.current.systemName)-\(UIDevice.current.systemVersion)"
#elseif os(macOS)
Expand Down
5 changes: 4 additions & 1 deletion Sources/Web3Modal/Modal/ModalInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ final class DefaultModalSheetInteractor: ModalSheetInteractor {
let httpClient = HTTPNetworkClient(host: "explorer-api.walletconnect.com")
let response = try await httpClient.request(
ListingsResponse.self,
at: ExplorerAPI.getListings(projectId: Web3Modal.config.projectId)
at: ExplorerAPI.getListings(
projectId: Web3Modal.config.projectId,
metadata: Web3Modal.config.metadata
)
)

return response.listings.values.compactMap { $0 }
Expand Down
21 changes: 18 additions & 3 deletions Sources/Web3Modal/Networking/Explorer/ExplorerAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import HTTPClient

enum ExplorerAPI: HTTPService {
case getListings(projectId: String)
case getListings(projectId: String, metadata: AppMetadata)

var path: String {
switch self {
Expand All @@ -22,7 +22,7 @@ enum ExplorerAPI: HTTPService {

var queryParameters: [String: String]? {
switch self {
case let .getListings(projectId):
case let .getListings(projectId, _):
return [
"projectId": projectId,
"page": "1",
Expand All @@ -36,6 +36,21 @@ enum ExplorerAPI: HTTPService {
}

var additionalHeaderFields: [String: String]? {
nil

switch self {
case let .getListings(_, metadata):
return [
"User-Agent": ExplorerAPI.userAgent,
"referer": metadata.name
]
}
}

private static var protocolName: String {
"w3m-ios-1.0.0"
}

static var userAgent: String {
"\(protocolName)/\(EnvironmentInfo.sdkName)/\(EnvironmentInfo.operatingSystem)"
}
}
6 changes: 5 additions & 1 deletion Sources/Web3Modal/UI/Common/AsyncImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ struct AsyncImage<Content>: View where Content: View {
init(_ url: URL?) {
guard let url = url else { return }

URLSession.shared.dataTaskPublisher(for: url)
var request = URLRequest(url: url)
request.setValue(ExplorerAPI.userAgent, forHTTPHeaderField: "User-Agent")
request.setValue(Web3Modal.config.metadata.name, forHTTPHeaderField: "Referer")

URLSession.shared.dataTaskPublisher(for: request)
.map(\.data)
.map { $0 as Data? }
.replaceError(with: nil)
Expand Down
7 changes: 6 additions & 1 deletion Sources/Web3Modal/Web3Modal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Web3Modal {

struct Config {
let projectId: String
var metadata: AppMetadata
var sessionParams: SessionParams
}

Expand All @@ -48,7 +49,11 @@ public class Web3Modal {
sessionParams: SessionParams = .default
) {
Pair.configure(metadata: metadata)
Web3Modal.config = Web3Modal.Config(projectId: projectId, sessionParams: sessionParams)
Web3Modal.config = Web3Modal.Config(
projectId: projectId,
metadata: metadata,
sessionParams: sessionParams
)
}

public static func set(sessionParams: SessionParams) {
Expand Down
1 change: 1 addition & 0 deletions Sources/Web3Modal/Web3ModalImports.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if !CocoaPods
@_exported import WalletConnectSign
@_exported import WalletConnectPairing
@_exported import WalletConnectRelay
#endif
17 changes: 17 additions & 0 deletions Tests/Web3ModalTests/ExplorerAPITests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import TestingUtils
@testable import Web3Modal
import XCTest

final class ExplorerAPITests: XCTestCase {

func testCorrectUserAgent() throws {

let request = ExplorerAPI
.getListings(projectId: "foo", metadata: .stub())
.resolve(for: "www.google.com")

XCTAssertEqual(request?.allHTTPHeaderFields?["Referer"], "Wallet Connect")
// Should look something like this: w3m-ios-1.0.0/swift-v1.6.8/iOS-16.1
XCTAssertTrue(request?.allHTTPHeaderFields?["User-Agent"]?.starts(with: "w3m-ios-1.0.0/swift-v") ?? false)
}
}

0 comments on commit da27232

Please sign in to comment.