Skip to content
This repository was archived by the owner on Aug 31, 2024. It is now read-only.

Commit 5c4e418

Browse files
committed
Convert to combine/async/await. Easier interface for data upload to networks.
Progress towards a more swifty data structures and interfacing
1 parent 3e052c5 commit 5c4e418

29 files changed

+749
-378
lines changed

README.md

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# IPFSKit
22

3-
![Travis CI Build](https://app.travis-ci.com/0xKala/IPFSKit.svg?branch=main)
4-
[![Swift 5.3](https://img.shields.io/badge/swift-5.5-brightgreen.svg)](https://github.com/apple/swift)
5-
[![xcode-version](https://img.shields.io/badge/xcode-12.5-brightgreen)](https://developer.apple.com/xcode/)
3+
[![Swift 5.7](https://img.shields.io/badge/swift-5.7-brightgreen.svg)](https://github.com/apple/swift)
4+
[![xcode-version](https://img.shields.io/badge/xcode-14.2-brightgreen)](https://developer.apple.com/xcode/)
65

76
Based on: https://github.com/ipfs-shipyard/swift-ipfs-http-client
87
Converted into a standalone Swift Package.
@@ -20,46 +19,43 @@ Add this repo link as a swift package to your XCode project.
2019
```swift
2120
import IPFSKit
2221

23-
let client: IPFSClient?
22+
struct InfuraGateway: IPFSGateway {
23+
var host: IPFSHost {
24+
InfuraHost(id: "...",//On Infura, this is simply the API_KEY
25+
secret: "...")//API_KEY_SECRET
26+
}
2427

25-
public init() {
26-
client = try? IPFSClient.init(
27-
host: "ipfs.infura.io",
28-
port: 5001,
29-
ssl: true,
30-
id: "INFURA-PROJECT-ID",
31-
secret: "INFURA-SECRET"
32-
)
28+
var gateway: String {
29+
"https://neatia.infura-ipfs.io"
30+
}
3331
}
32+
33+
IPFSKit.gateway = InfuraGateway()
3434
```
3535

3636
### Adding data to IPFS
3737

3838
```swift
39-
if let data = result.data.pngData() {
40-
do {
41-
try state.service.client?.add(data) { nodes in // Adding data to IPFS
42-
// use 'nodes' to pin content
43-
}
44-
} catch let error {
45-
GraniteLogger.info("error adding new image:\n\(error)", .expedition, focus: true)
46-
}
47-
}
39+
40+
let data: Data = "Hello World".data(using: .utf8)!
41+
42+
let response = await IPFS.upload(data)
43+
44+
print(IPFSKit.gateway?.url(for: response))
4845

4946
```
5047

5148
Convert any type of object into a `Data` type to prepare for adding.
5249

5350
### Pinning data to IPFS
5451

52+
> Infura seems to have updated their services to automatically pin data that is added via the `/add` endpoint. This step is uncessary depending on the provider used or edge cases involved.
53+
5554
```swift
56-
do {
57-
if let addHash = nodes.first?.hash { // the `nodes` from the closure of the previous code example
58-
try client?.pin.add(addHash) { pinHash in
59-
let gatewayHash = b58String(pinHash) // IPFS public hash gateway for the pinned content
60-
}
61-
}
62-
} catch let error {
63-
GraniteLogger.info("error pinning:\n\(error)", .expedition, focus: true)
64-
}
55+
56+
let data: Data = "Hello World".data(using: .utf8)!
57+
58+
let response = await IPFS.upload(data)
59+
60+
let gatewayHash = await IPFS.pin(response)
6561
```

Sources/IPFSExecutable/main.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
//
2-
// File.swift
2+
// main.swift
33
//
44
//
5-
// Created by Ritesh Pakala on 7/11/23.
5+
// Created by PEXAVC on 7/11/23.
66
//
77

88
import Foundation
99
import IPFSKit
10+
11+
struct InfuraGateway: IPFSGateway {
12+
var host: IPFSHost {
13+
InfuraHost(id: "...",//On Infura, this is simply the API_KEY
14+
secret: "...")//API_KEY_SECRET
15+
}
16+
17+
var gateway: String {
18+
"https://neatia.infura-ipfs.io"
19+
}
20+
}
21+
22+
IPFSKit.gateway = InfuraGateway()
23+
24+
let data: Data = "Hello World".data(using: .utf8)!
25+
26+
let response = await IPFS.upload(data)
27+
28+
print(IPFSKit.gateway?.url(for: response))

Sources/IPFSKit/Client/GraniteLogger.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ public struct GraniteLogger {
6969

7070
var disable: Bool {
7171
return true
72-
// switch self {
73-
// case .expedition:
74-
// return false
75-
// default:
76-
// return true
77-
// }
7872
}
7973
}
8074

Sources/IPFSKit/Client/HttpIo.swift

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// Licensed under MIT See LICENCE file in the root of this project for details.
1010

1111
import Foundation
12+
import SwiftUI
13+
import Combine
1214

1315
enum HttpIoError : Error {
1416
case urlError(String)
@@ -18,26 +20,31 @@ enum HttpIoError : Error {
1820
public struct HttpIo : NetworkIo {
1921
let auth: String
2022

21-
public func receiveFrom(_ source: String, completionHandler: @escaping (Data) throws -> Void) throws {
23+
public func receiveFrom(_ source: String) throws -> AnyPublisher<Data, URLError> {
2224
guard let url = URL(string: source) else { throw HttpIoError.urlError("Invalid URL") }
2325
GraniteLogger.info("HttpIo receiveFrom url is \(url)")
24-
let task = URLSession.shared.dataTask(with: url) {
25-
(data: Data?, response: URLResponse?, error: Error?) in
26-
27-
do {
28-
guard error == nil else { throw HttpIoError.transmissionError((error?.localizedDescription)!) }
29-
guard let data = data else { throw IpfsApiError.nilData }
30-
31-
// GraniteLogger.info("The data:",NSString(data: data, encoding: String.Encoding.utf8.rawValue))
32-
33-
try completionHandler(data)
34-
35-
} catch {
36-
GraniteLogger.info("Error \(error) in completionHandler passed to fetchData ")
37-
}
38-
}
26+
// let task = URLSession.shared.dataTask(with: url) {
27+
// (data: Data?, response: URLResponse?, error: Error?) in
28+
//
29+
// do {
30+
// guard error == nil else { throw HttpIoError.transmissionError((error?.localizedDescription)!) }
31+
// guard let data = data else { throw IpfsApiError.nilData }
32+
//
33+
//// GraniteLogger.info("The data:",NSString(data: data, encoding: String.Encoding.utf8.rawValue))
34+
//
35+
// try completionHandler(data)
36+
//
37+
// } catch {
38+
// GraniteLogger.info("Error \(error) in completionHandler passed to fetchData ")
39+
// }
40+
// }
41+
//
42+
// task.resume()
3943

40-
task.resume()
44+
return URLSession.shared
45+
.dataTaskPublisher(for: url)
46+
.map { $0.data }
47+
.eraseToAnyPublisher()
4148
}
4249

4350

@@ -54,21 +61,22 @@ public struct HttpIo : NetworkIo {
5461
task.resume()
5562
}
5663

57-
public func sendTo(_ target: String, content: Data, completionHandler: @escaping (Data) -> Void) throws {
64+
public func sendTo(_ target: String, content: Data) throws -> AnyPublisher<Data, URLError> {
5865

5966
var multipart = try Multipart(targetUrl: target, encoding: .utf8, auth: self.auth)
6067
multipart = try Multipart.addFilePart(multipart, fileName: nil , fileData: content)
61-
Multipart.finishMultipart(multipart, completionHandler: completionHandler)
68+
69+
return Multipart.finishMultipart(multipart)
6270
}
6371

6472

65-
public func sendTo(_ target: String, filePath: String, completionHandler: @escaping (Data) -> Void) throws {
73+
public func sendTo(_ target: String, filePath: String) throws -> AnyPublisher<Data, URLError> {
6674

6775
var multipart = try Multipart(targetUrl: target, encoding: .utf8, auth: self.auth)
6876

6977
multipart = try handle(oldMultipart: multipart, files: [filePath])
7078

71-
Multipart.finishMultipart(multipart, completionHandler: completionHandler)
79+
return Multipart.finishMultipart(multipart)
7280

7381
}
7482

0 commit comments

Comments
 (0)