66//
77
88import Foundation
9+
10+ /**
11+ Base class that deals with all the interactions with OSM server
12+ */
913public class OSMConnection {
1014 // Creates a connection
1115 let baseUrl : String
1216 var currentChangesetId : Int ? = 0
1317 let userCreds : OSMLogin
1418 // Lets see if we can start with some authentication or node
19+
20+ /// Initializer for OSMConnection
21+ /// - parameter config : The Server configuration (defaults to OSMConfig.testOSM)
22+ /// - parameter currentChangesetId: The overal changeset id for the user
23+ /// - parameter userCreds: User credentials for authenticated calls. Defaults to testOSM
1524 public init ( config: OSMConfig = OSMConfig . testOSM, currentChangesetId: Int ? = nil , userCreds: OSMLogin = OSMLogin . testOSM) {
1625 self . baseUrl = config. baseUrl
1726 self . currentChangesetId = currentChangesetId
1827 self . userCreds = userCreds
1928 }
20-
29+ /// Fetches a single node
30+ /// - parameter id : the `id` of the node
31+ /// - parameter completion: The completion handler that receives the node
2132 func getNode( id: String , _ completion: @escaping ( Result < OSMNodeResponse , Error > ) -> Void ) {
33+ //TODO: Write error when node does not exist
2234 let urlString = self . baseUrl. appending ( " node/ " ) . appending ( id) . appending ( " .json " )
2335 guard let url = URL ( string: urlString) else {
2436 print ( " Invalid URL given " )
@@ -27,8 +39,11 @@ public class OSMConnection {
2739 BaseNetworkManager . shared. addOrSetHeaders ( header: " Authorization " , value: " Basic \( self . userCreds. getHeaderData ( ) ) " )
2840 BaseNetworkManager . shared. fetchData ( url: url, completion: completion) // Need to improve this one
2941 }
30-
42+ /// Fetches a single way
43+ /// - parameter id: the `id` of the way
44+ /// - parameter completion: The completion handler that receives the way
3145 func getWay( id: String , _ completion: @escaping ( Result < OSMWayResponse , Error > ) -> Void ) {
46+ //TODO: Write errors when way does not exist
3247 let urlString = self . baseUrl. appending ( " way/ " ) . appending ( id) . appending ( " .json " )
3348 guard let url = URL ( string: urlString) else {
3449 print ( " Invalid URL given " )
@@ -38,8 +53,10 @@ public class OSMConnection {
3853 BaseNetworkManager . shared. fetchData ( url: url, completion: completion) // Need to improve this one
3954 }
4055
56+ /// Opens a changeset for the given user
57+ /// - parameter completion: Completion handler that receives the newly opened changesetId
4158 public func openChangeSet( _ completion: @escaping ( ( Result < Int , Error > ) -> Void ) ) {
42- // Have to open changeset
59+ //TODO: Write errors when not authenticated and if there is already an open changeset with same user
4360 let urlString = self . baseUrl. appending ( " changeset/create " )
4461 guard let url = URL ( string: urlString) else {
4562 print ( " Invalid URL given " )
@@ -58,8 +75,11 @@ public class OSMConnection {
5875 completion ( result)
5976 }
6077 }
61-
78+ /// Closes the changeset
79+ /// - parameter id: The changeset ID to be closed
80+ /// - parameter completion: The completion handler
6281 public func closeChangeSet( id: String , completion: @escaping ( ( Result < Bool , Error > ) -> Void ) ) {
82+ //TODO: Write error codes if not able to close
6383 let urlString = self . baseUrl. appending ( " changeset/ " ) . appending ( id) . appending ( " /close " )
6484 guard let url = URL ( string: urlString) else {
6585 print ( " Invalid URL given " )
@@ -109,9 +129,12 @@ public class OSMConnection {
109129 BaseNetworkManager . shared. fetchData ( url: url, completion: completion)
110130 }
111131
112-
132+ /// Updates a single node
133+ /// - parameter node : An instance of `OSMNode`
134+ /// - parameter tags: A `[String:String]` dictionary containing new added tags
135+ /// - parameter completion : A handler that is called after the call is made
113136 public func updateNode( node: inout OSMNode , tags: [ String : String ] , completion: @escaping ( ( Result < Int , Error > ) -> Void ) ) {
114- // Have to do this.
137+ //TODO: Error handling when there is no active changeset, no node and not authorized
115138 let urlString = self . baseUrl. appending ( " node/ " ) . appending ( String ( node. id) )
116139 guard let url = URL ( string: urlString) else {
117140 print ( " Invalid URL given " )
@@ -130,9 +153,12 @@ public class OSMConnection {
130153 BaseNetworkManager . shared. postData ( url: url, method: " PUT " , body: node , completion: completion)
131154
132155 }
133-
156+ /// Updates a single way
157+ /// - parameter way: the current way element
158+ /// - parameter tags: `[String:String]` dictionary containing new tags
159+ /// - parameter completion: The completion handler after this is done
134160 public func updateWay( way: inout OSMWay , tags: [ String : String ] , completion: @escaping ( ( Result < Int , Error > ) -> Void ) ) {
135- // Have to do this.
161+ //TODO: Error handling when there is no active changeset, way not found and not authorized
136162 let urlString = self . baseUrl. appending ( " way/ " ) . appending ( String ( way. id) )
137163 guard let url = URL ( string: urlString) else {
138164 print ( " Invalid URL given " )
@@ -148,15 +174,24 @@ public class OSMConnection {
148174 BaseNetworkManager . shared. postData ( url: url, method: " PUT " , body: way , completion: completion)
149175
150176 }
177+
178+ /// Fetches the user details based on `id`
179+ /// - parameter id : Id of the user
180+ /// - parameter completion : Completion handler with user data response
151181 func getUserDetailsWithId( id: String , _ completion: @escaping ( Result < OSMUserDataResponse , Error > ) -> Void ) {
182+ //TODO: Error for user id not found
152183 let urlString = self . baseUrl. appending ( " user/ " ) . appending ( id) . appending ( " .json " )
153184 guard let url = URL ( string: urlString) else {
154185 print ( " Invalid URL given " )
155186 return
156187 }
157188 BaseNetworkManager . shared. fetchData ( url: url, completion: completion)
158189 }
190+
191+ /// Fetches the current logged in user details
192+ /// - parameter completion : Completion handler
159193 public func getUserDetails( _ completion: @escaping ( Result < OSMUserDataResponse , Error > ) -> Void ) {
194+ //TODO: Errors for unauthenticated call
160195 let urlString = self . baseUrl. appending ( " user/details.json " )
161196 guard let url = URL ( string: urlString) else {
162197 print ( " Invalid URL given " )
0 commit comments