Skip to content

Commit c090c3c

Browse files
refactor: Splits up files
1 parent 00130a8 commit c090c3c

File tree

10 files changed

+99
-99
lines changed

10 files changed

+99
-99
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import CryptoKit
2+
3+
@available(macOS 10.15, *)
4+
enum AuthHash: String {
5+
case SHA512 = "SHA-512"
6+
case SHA256 = "SHA-256"
7+
8+
var hash: any HashFunction.Type {
9+
switch self {
10+
case .SHA256:
11+
return CryptoKit.SHA256.self
12+
case .SHA512:
13+
return CryptoKit.SHA512.self
14+
}
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct AuthMessage: CustomStringConvertible {
2+
let scheme: String
3+
let attributes: [String: String]
4+
5+
var description: String {
6+
// Unwrap is safe because attributes is immutable
7+
"\(scheme) \(attributes.keys.sorted().map { "\($0)=\(attributes[$0]!)" }.joined(separator: ", "))"
8+
}
9+
10+
static func from(_ string: String) throws -> Self {
11+
// Example input: "SCRAM hash=SHA-256, handshakeToken=aabbcc"
12+
let scheme: String
13+
let attributes: [String: String]
14+
// If space exists then parse attributes as well.
15+
if let spaceIndex = string.firstIndex(of: " ") {
16+
scheme = String(string[..<spaceIndex]).trimmingCharacters(in: .whitespaces)
17+
let attributesString = String(string[spaceIndex...]).trimmingCharacters(in: .whitespaces)
18+
attributes = extractNameValuePairs(from: attributesString)
19+
} else {
20+
scheme = string
21+
attributes = [:]
22+
}
23+
return Self(scheme: scheme, attributes: attributes)
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@available(macOS 10.15, *)
2+
protocol Authenticator {
3+
func getAuthToken() async throws -> String
4+
}

Sources/HaystackClient/Authenticators.swift renamed to Sources/HaystackClient/Authentication/ScramAuthenticator.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import CryptoKit
22
import Foundation
33

4-
@available(macOS 13.0, *)
5-
protocol Authenticator {
6-
func getAuthToken() async throws -> String
7-
}
8-
94
@available(macOS 13.0, *)
105
struct ScramAuthenticator<Hash: HashFunction>: Authenticator {
116
let url: URL
@@ -144,29 +139,3 @@ struct ScramAuthenticator<Hash: HashFunction>: Authenticator {
144139
case authFailedWithHttpCode(Int)
145140
}
146141
}
147-
148-
struct AuthMessage: CustomStringConvertible {
149-
let scheme: String
150-
let attributes: [String: String]
151-
152-
var description: String {
153-
// Unwrap is safe because attributes is immutable
154-
"\(scheme) \(attributes.keys.sorted().map { "\($0)=\(attributes[$0]!)" }.joined(separator: ", "))"
155-
}
156-
157-
static func from(_ string: String) throws -> Self {
158-
// Example input: "SCRAM hash=SHA-256, handshakeToken=aabbcc"
159-
let scheme: String
160-
let attributes: [String: String]
161-
// If space exists then parse attributes as well.
162-
if let spaceIndex = string.firstIndex(of: " ") {
163-
scheme = String(string[..<spaceIndex]).trimmingCharacters(in: .whitespaces)
164-
let attributesString = String(string[spaceIndex...]).trimmingCharacters(in: .whitespaces)
165-
attributes = extractNameValuePairs(from: attributesString)
166-
} else {
167-
scheme = string
168-
attributes = [:]
169-
}
170-
return Self(scheme: scheme, attributes: attributes)
171-
}
172-
}

Sources/HaystackClient/HaystackClient.swift renamed to Sources/HaystackClient/Client.swift

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Haystack
33
import Foundation
44

55
@available(macOS 13.0, *)
6-
public class HaystackClient {
6+
public class Client {
77
private let userAgentHeaderValue = "swift-haystack-client"
88

99
public let baseUrl: URL
@@ -407,59 +407,24 @@ public class HaystackClient {
407407
}
408408
}
409409

410-
public enum DataFormat: String {
411-
case json
412-
case zinc
413-
414-
// See Content Negotiation: https://haxall.io/doc/docHaystack/HttpApi.html#contentNegotiation
415-
var acceptHeaderValue: String {
416-
switch self {
417-
case .json: return "application/json"
418-
case .zinc: return "text/zinc"
419-
}
420-
}
421-
422-
var contentTypeHeaderValue: String {
423-
switch self {
424-
case .json: return "application/json"
425-
case .zinc: return "text/zinc; charset=utf-8"
426-
}
427-
}
428-
}
429-
430410
enum HaystackClientError: Error {
431411
case authHelloNoWwwAuthenticateHeader
432412
case authHelloHandshakeTokenNotPresent
433413
case authHelloHashFunctionNotPresent
434414
case authHashFunctionNotRecognized(String)
435415
case authMechanismNotRecognized(String)
436416
case authMechanismNotImplemented(AuthMechanism)
437-
case notLoggedIn
438417
case baseUrlCannotBeFile
418+
case notLoggedIn
419+
case pointWriteLevelIsNotIntBetween1And17
439420
case responseIsNotZinc
440421
case requestFailed(httpCode: Int, message: String?)
441-
case pointWriteLevelIsNotIntBetween1And17
442422
}
443423

444424
enum AuthMechanism: String {
445425
case SCRAM
446426
}
447427

448-
@available(macOS 10.15, *)
449-
enum AuthHash: String {
450-
case SHA512 = "SHA-512"
451-
case SHA256 = "SHA-256"
452-
453-
var hash: any HashFunction.Type {
454-
switch self {
455-
case .SHA256:
456-
return CryptoKit.SHA256.self
457-
case .SHA512:
458-
return CryptoKit.SHA512.self
459-
}
460-
}
461-
}
462-
463428
enum HTTPHeader {
464429
static let accept = "Accept"
465430
static let authenticationInfo = "Authentication-Info"
@@ -468,33 +433,3 @@ enum HTTPHeader {
468433
static let userAgent = "User-Agent"
469434
static let wwwAuthenticate = "Www-Authenticate"
470435
}
471-
472-
public enum HisReadRange {
473-
case today
474-
case yesterday
475-
case date(Haystack.Date)
476-
case dateRange(from: Haystack.Date, to: Haystack.Date)
477-
case dateTimeRange(from: DateTime, to: DateTime)
478-
case after(DateTime)
479-
480-
func toRequestString() -> String {
481-
switch self {
482-
case .today: return "today"
483-
case .yesterday: return "yesterday"
484-
case let .date(date): return "\(date.toZinc())"
485-
case let .dateRange(fromDate, toDate): return "\(fromDate.toZinc()),\(toDate.toZinc())"
486-
case let .dateTimeRange(fromDateTime, toDateTime): return "\(fromDateTime.toZinc()),\(toDateTime.toZinc())"
487-
case let .after(dateTime): return "\(dateTime.toZinc())"
488-
}
489-
}
490-
}
491-
492-
public struct HisItem {
493-
let ts: DateTime
494-
let val: any Val
495-
496-
public init(ts: DateTime, val: any Val) {
497-
self.ts = ts
498-
self.val = val
499-
}
500-
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public enum DataFormat: String {
2+
case json
3+
case zinc
4+
5+
// See Content Negotiation: https://haxall.io/doc/docHaystack/HttpApi.html#contentNegotiation
6+
var acceptHeaderValue: String {
7+
switch self {
8+
case .json: return "application/json"
9+
case .zinc: return "text/zinc"
10+
}
11+
}
12+
13+
var contentTypeHeaderValue: String {
14+
switch self {
15+
case .json: return "application/json"
16+
case .zinc: return "text/zinc; charset=utf-8"
17+
}
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Haystack
2+
3+
public struct HisItem {
4+
let ts: DateTime
5+
let val: any Val
6+
7+
public init(ts: DateTime, val: any Val) {
8+
self.ts = ts
9+
self.val = val
10+
}
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Haystack
2+
3+
public enum HisReadRange {
4+
case today
5+
case yesterday
6+
case date(Haystack.Date)
7+
case dateRange(from: Haystack.Date, to: Haystack.Date)
8+
case dateTimeRange(from: DateTime, to: DateTime)
9+
case after(DateTime)
10+
11+
func toRequestString() -> String {
12+
switch self {
13+
case .today: return "today"
14+
case .yesterday: return "yesterday"
15+
case let .date(date): return "\(date.toZinc())"
16+
case let .dateRange(fromDate, toDate): return "\(fromDate.toZinc()),\(toDate.toZinc())"
17+
case let .dateTimeRange(fromDateTime, toDateTime): return "\(fromDateTime.toZinc()),\(toDateTime.toZinc())"
18+
case let .after(dateTime): return "\(dateTime.toZinc())"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)