Skip to content

Commit dc503c5

Browse files
committed
Merge branch '0.0.5' - Ledgers
2 parents 292e0b5 + 59f5bdf commit dc503c5

23 files changed

+1847
-110
lines changed

Amatino.xcodeproj/project.pbxproj

Lines changed: 72 additions & 12 deletions
Large diffs are not rendered by default.

Amatino.xcodeproj/xcshareddata/xcschemes/MacOS.xcscheme

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@
5151
debugDocumentVersioning = "YES"
5252
debugServiceExtension = "internal"
5353
allowLocationSimulation = "YES">
54+
<MacroExpansion>
55+
<BuildableReference
56+
BuildableIdentifier = "primary"
57+
BlueprintIdentifier = "Amatino::Amatino"
58+
BuildableName = "Amatino.framework"
59+
BlueprintName = "AmatinoMacOS"
60+
ReferencedContainer = "container:Amatino.xcodeproj">
61+
</BuildableReference>
62+
</MacroExpansion>
5463
<AdditionalOptions>
5564
</AdditionalOptions>
5665
</LaunchAction>

Sources/Amatino/Account.swift

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
import Foundation
99

10-
public class Account: Decodable {
11-
12-
private static let path = "/accounts"
10+
public class AccountError: AmatinoObjectError {}
11+
12+
public class Account: AmatinoObject {
13+
14+
internal static let path = "/accounts"
15+
internal static let errorType: AmatinoObjectError.Type = AccountError.self
16+
1317
private static let urlKey = "account_id"
1418

1519
public let id: Int
@@ -37,6 +41,35 @@ public class Account: Decodable {
3741
description: description,
3842
globalUnit: globalUnit
3943
)
44+
let _ = try Account.create(session, entity, arguments, callback)
45+
return
46+
}
47+
48+
public static func create(
49+
session: Session,
50+
entity: Entity,
51+
name: String,
52+
description: String,
53+
globalUnit: GlobalUnit,
54+
parent: Account,
55+
callback: @escaping (Error?, Account?) -> Void
56+
) throws {
57+
let arguments = try AccountCreateArguments(
58+
name: name,
59+
description: description,
60+
globalUnit: globalUnit,
61+
parent: parent
62+
)
63+
let _ = try Account.create(session, entity, arguments, callback)
64+
return
65+
}
66+
67+
private static func create(
68+
_ session: Session,
69+
_ entity: Entity,
70+
_ arguments: AccountCreateArguments,
71+
_ callback: @escaping (Error?, Account?) -> Void
72+
) throws {
4073
let requestData = try RequestData(data: arguments)
4174
let urlParameters = UrlParameters(singleEntity: entity)
4275
let _ = try AmatinoRequest(
@@ -46,9 +79,8 @@ public class Account: Decodable {
4679
urlParameters: urlParameters,
4780
method: .POST,
4881
callback: { (error, data) in
49-
let _ = loadResponse(error, data, callback)
82+
let _ = loadResponse(error, data, callback, Account.self)
5083
})
51-
return
5284
}
5385

5486
public static func create(
@@ -66,7 +98,7 @@ public class Account: Decodable {
6698
urlParameters: urlParameters,
6799
method: .POST,
68100
callback: { (error, data) in
69-
let _ = loadArrayResponse(error, data, callback)
101+
let _ = loadArrayResponse(error, data, callback, Account.self)
70102
})
71103
}
72104

@@ -91,7 +123,7 @@ public class Account: Decodable {
91123
urlParameters: urlParameters,
92124
method: .GET,
93125
callback: { (error, data) in
94-
let _ = loadResponse(error, data, callback)
126+
let _ = loadResponse(error, data, callback, Account.self)
95127
})
96128
}
97129

@@ -113,53 +145,10 @@ public class Account: Decodable {
113145
urlParameters: urlParameters,
114146
method: .GET,
115147
callback: { (error, data) in
116-
let _ = loadArrayResponse(error, data, callback)
148+
let _ = loadArrayResponse(error, data, callback, Account.self)
117149
})
118150
}
119-
120-
private static func loadResponse(
121-
_ error: Error?,
122-
_ data: Data?,
123-
_ callback: (Error?, Account?) -> Void
124-
) {
125-
guard error == nil else {callback(error, nil); return}
126-
let decoder = JSONDecoder()
127-
let accounts: Account
128-
do {
129-
accounts = try decoder.decode(
130-
[Account].self,
131-
from: data!
132-
)[0]
133-
callback(nil, accounts)
134-
return
135-
} catch {
136-
callback(error, nil)
137-
return
138-
}
139-
}
140-
141-
private static func loadArrayResponse(
142-
_ error: Error?,
143-
_ data: Data?,
144-
_ callback: (Error?, [Account]?) -> Void
145-
) {
146-
guard error == nil else {callback(error, nil); return}
147-
let decoder = JSONDecoder()
148-
let accounts: [Account]
149-
do {
150-
accounts = try decoder.decode(
151-
[Account].self,
152-
from: data!
153-
)
154-
callback(nil, accounts)
155-
return
156-
} catch {
157-
callback(error, nil)
158-
return
159-
}
160-
}
161151

162-
163152
public required init(from decoder: Decoder) throws {
164153
let container = try decoder.container(keyedBy: CodingKeys.self)
165154
id = try container.decode(Int.self, forKey: .id)
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// BalanceCore.swift
2+
// AccountBalance.swift
33
// Amatino
44
//
55
// Created by Hugh Jeremy on 18/7/18.
@@ -9,7 +9,7 @@ import Foundation
99

1010
class BalanceError: AmatinoObjectError {}
1111

12-
internal class BalanceCore: Decodable {
12+
internal class AccountBalance: Decodable {
1313

1414
public let accountId: Int
1515
public let balanceTime: Date
@@ -49,20 +49,10 @@ internal class BalanceCore: Decodable {
4949
forKey: .customUnitDenomination
5050
)
5151
let rawMagnitude = try container.decode(String.self, forKey: .balance)
52-
let negative: Bool = rawMagnitude.contains("(")
53-
let parseMagnitude: String
54-
if negative == true {
55-
var magnitudeToStrip = rawMagnitude
56-
magnitudeToStrip.removeFirst()
57-
magnitudeToStrip.removeLast()
58-
parseMagnitude = "-" + magnitudeToStrip
59-
} else {
60-
parseMagnitude = rawMagnitude
61-
}
62-
guard let decimalMagnitude = Decimal(string: parseMagnitude) else {
63-
throw BalanceError(.incomprehensibleResponse)
64-
}
65-
magnitude = decimalMagnitude
52+
magnitude = try Magnitude(
53+
fromString: rawMagnitude,
54+
withError: BalanceError.self
55+
).decimal
6656
recursive = try container.decode(Bool.self, forKey: .recursive)
6757
return
6858
}

Sources/Amatino/AccountCreateArguments.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,18 @@ public struct AccountCreateArguments: Encodable {
6767

6868
public init(
6969
name: String,
70-
type: AccountType,
7170
description: String,
7271
customUnit: CustomUnit,
73-
parentAccount: Account
72+
parent: Account
7473
) throws {
7574

7675
self.name = name
7776
self.description = description
7877
self.globalUnit = nil
79-
self.type = type
78+
self.type = parent.type
8079
self.customUnit = customUnit
8180
self.counterPartyEntity = nil
82-
self.parentAccount = parentAccount
81+
self.parentAccount = parent
8382
self.colour = nil
8483

8584
try checkName(name: name)
@@ -90,19 +89,18 @@ public struct AccountCreateArguments: Encodable {
9089

9190
public init(
9291
name: String,
93-
type: AccountType,
9492
description: String,
9593
globalUnit: GlobalUnit,
96-
parentAccount: Account
94+
parent: Account
9795
) throws {
9896

9997
self.name = name
10098
self.description = description
10199
self.globalUnit = globalUnit
102-
self.type = type
100+
self.type = parent.type
103101
self.customUnit = nil
104102
self.counterPartyEntity = nil
105-
self.parentAccount = parentAccount
103+
self.parentAccount = parent
106104
self.colour = nil
107105

108106
try checkName(name: name)

Sources/Amatino/AmatinoDate.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// AmatinoDate.swift
3+
// Amatino
4+
//
5+
// Created by Hugh Jeremy on 27/7/18.
6+
//
7+
8+
import Foundation
9+
10+
internal struct AmatinoDate {
11+
12+
let decodedDate: Date
13+
14+
internal init (
15+
fromString dateString: String,
16+
withError ErrorType: AmatinoObjectError.Type
17+
) throws {
18+
let formatter = DateFormatter()
19+
formatter.dateFormat = RequestData.dateStringFormat
20+
guard let date: Date = formatter.date(from: dateString) else {
21+
throw ErrorType.init(.incomprehensibleResponse)
22+
}
23+
decodedDate = date
24+
return
25+
}
26+
27+
}

Sources/Amatino/AmatinoObject.swift

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,98 @@
55
// author: hugh@amatino.io
66
//
77

8-
protocol AmatinoObject {
8+
internal protocol AmatinoObject: Decodable {
9+
static var errorType: AmatinoObjectError.Type { get }
10+
}
11+
12+
extension AmatinoObject {
13+
14+
static func loadResponse<ObjectType: AmatinoObject>(
15+
_ error: Error?,
16+
_ data: Data?,
17+
_ callback: (Error?, ObjectType?) -> Void,
18+
_ object: ObjectType.Type
19+
) {
20+
guard error == nil else {callback(error, nil); return}
21+
let decoder = JSONDecoder()
22+
let object: ObjectType
23+
let objects: [ObjectType]
24+
guard let dataToDecode: Data = data else {
25+
callback(errorType.init(.inconsistentInternalState), nil)
26+
return
27+
}
28+
do {
29+
objects = try decoder.decode(
30+
[ObjectType].self,
31+
from: dataToDecode
32+
)
33+
guard objects.count > 0 else {
34+
callback(errorType.init(.incomprehensibleResponse), nil)
35+
return
36+
}
37+
object = objects[0]
38+
callback(nil, object)
39+
return
40+
} catch {
41+
callback(error, nil)
42+
return
43+
}
44+
}
45+
46+
static func loadArrayResponse<ObjectType: AmatinoObject>(
47+
_ error: Error?,
48+
_ data: Data?,
49+
_ callback: (Error?, [ObjectType]?) -> Void,
50+
_ object: ObjectType.Type
51+
) {
52+
guard error == nil else {callback(error, nil); return}
53+
let decoder = JSONDecoder()
54+
let objects: [ObjectType]
55+
guard let dataToDecode: Data = data else {
56+
callback(AmatinoObjectError(.inconsistentInternalState), nil)
57+
return
58+
}
59+
do {
60+
objects = try decoder.decode(
61+
[ObjectType].self,
62+
from: dataToDecode
63+
)
64+
guard objects.count > 0 else {
65+
callback(AmatinoObjectError(.incomprehensibleResponse), nil)
66+
return
67+
}
68+
callback(nil, objects)
69+
return
70+
} catch {
71+
callback(error, nil)
72+
return
73+
}
74+
}
975

10-
var entity: Entity { get }
11-
var session: Session { get }
76+
static func loadObjectResponse<ObjectType: AmatinoObject>(
77+
_ error: Error?,
78+
_ data: Data?,
79+
_ callback: (Error?, ObjectType?) -> Void,
80+
_ object: ObjectType.Type
81+
) {
82+
guard error == nil else {callback(error, nil); return}
83+
let decoder = JSONDecoder()
84+
let object: ObjectType
85+
guard let dataToDecode: Data = data else {
86+
callback(errorType.init(.inconsistentInternalState), nil)
87+
return
88+
}
89+
do {
90+
object = try decoder.decode(
91+
ObjectType.self,
92+
from: dataToDecode
93+
)
94+
callback(nil, object)
95+
return
96+
} catch {
97+
callback(error, nil)
98+
return
99+
}
100+
}
12101

13102
}

Sources/Amatino/AmatinoObjectError.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class AmatinoObjectError: Error {
2020
case neverInitialized
2121
case operationInProgress
2222
case incomprehensibleResponse
23+
case inconsistentInternalState
2324
}
2425

2526
let kind: Kind

Sources/Amatino/AmatinoRequest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ enum AmatinoRequestError: Error {
1818

1919
internal class AmatinoRequest {
2020

21-
private let agent = "Amatino Swift 0.0.2"
21+
private let agent = "Amatino Swift 0.0.5"
2222
private let apiEndpoint = "https://api.amatino.io"
2323
private static let apiSession = URLSession(
2424
configuration: URLSessionConfiguration.ephemeral
@@ -90,7 +90,7 @@ internal class AmatinoRequest {
9090
_ urlParameters: UrlParameters?,
9191
_ method: HTTPMethod
9292
) throws -> URLRequest {
93-
93+
9494

9595
let fullURL: String
9696

0 commit comments

Comments
 (0)