-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
223 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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,60 @@ | ||
// | ||
// LightningNodeInfo.swift | ||
// LDKNodeMonday | ||
// | ||
// Created by Matthew Ramsden on 1/31/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct LightningNodeInfo: Codable { | ||
let nodes: [Node] | ||
let channels: [Channel] | ||
|
||
struct Node: Codable { | ||
let publicKey: String | ||
let alias: String | ||
let capacity: Int? | ||
let channels: Int? | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case publicKey = "public_key" | ||
case alias | ||
case capacity | ||
case channels | ||
} | ||
} | ||
|
||
struct Channel: Codable { | ||
let channelId: String | ||
let node1Pub: String | ||
let node2Pub: String | ||
let capacity: Int | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case channelId = "channel_id" | ||
case node1Pub = "node1_pub" | ||
case node2Pub = "node2_pub" | ||
case capacity | ||
} | ||
} | ||
} | ||
|
||
//#if DEBUG | ||
let nodeMock = LightningNodeInfo.Node( | ||
publicKey: "publicKey", | ||
alias: "alias", | ||
capacity: 1, | ||
channels: 1 | ||
) | ||
let channelMock = LightningNodeInfo.Channel( | ||
channelId: "channelId", | ||
node1Pub: "node1Pub", | ||
node2Pub: "node2Pub", | ||
capacity: 100 | ||
) | ||
let nodeInfoMock = LightningNodeInfo( | ||
nodes: [nodeMock], | ||
channels: [channelMock] | ||
) | ||
//#endif |
13 changes: 13 additions & 0 deletions
13
LDKNodeMonday/Service/LightningNodesService/LightningNodeServiceError.swift
This file contains 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,13 @@ | ||
// | ||
// LightningNodeServiceError.swift | ||
// LDKNodeMonday | ||
// | ||
// Created by Matthew Ramsden on 1/31/24. | ||
// | ||
|
||
import Foundation | ||
|
||
enum NodeInfoServiceError: Error { | ||
case invalidURL | ||
case invalidServerResponse | ||
} |
52 changes: 52 additions & 0 deletions
52
LDKNodeMonday/Service/LightningNodesService/LightningNodesService.swift
This file contains 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,52 @@ | ||
// | ||
// LightningNodesService.swift | ||
// LDKNodeMonday | ||
// | ||
// Created by Matthew Ramsden on 1/31/24. | ||
// | ||
|
||
import Foundation | ||
|
||
private struct NodeInfoService { | ||
func fetchNodeInfo(searchText: String) async throws -> LightningNodeInfo { | ||
guard | ||
let encodedSearchText = searchText.addingPercentEncoding( | ||
withAllowedCharacters: .urlQueryAllowed | ||
), | ||
let url = URL( | ||
string: | ||
"https://mempool.space/api/v1/lightning/search?searchText=\(encodedSearchText)" | ||
) | ||
else { | ||
throw NodeInfoServiceError.invalidURL | ||
} | ||
let (data, response) = try await URLSession.shared.data(from: url) | ||
guard let httpResponse = response as? HTTPURLResponse, 200...299 ~= httpResponse.statusCode | ||
else { | ||
throw NodeInfoServiceError.invalidServerResponse | ||
} | ||
let jsonDecoder = JSONDecoder() | ||
let jsonObject = try jsonDecoder.decode(LightningNodeInfo.self, from: data) | ||
return jsonObject | ||
} | ||
} | ||
|
||
struct NodeInfoClient { | ||
let fetchNodeInfo: (_ searchText: String) async throws -> LightningNodeInfo | ||
private init(fetchNodeInfo: @escaping (_ searchText: String) async throws -> LightningNodeInfo) | ||
{ | ||
self.fetchNodeInfo = fetchNodeInfo | ||
} | ||
} | ||
|
||
extension NodeInfoClient { | ||
static let live = Self(fetchNodeInfo: { searchText in | ||
try await NodeInfoService().fetchNodeInfo(searchText: searchText) | ||
}) | ||
} | ||
|
||
//#if DEBUG | ||
extension NodeInfoClient { | ||
static let mock = Self(fetchNodeInfo: { searchText in nodeInfoMock }) | ||
} | ||
//#endif |
This file contains 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 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 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