@@ -27,6 +27,22 @@ extension PAPI {
2727 task. resume ( )
2828 }
2929
30+ func deleteCode( token: String , sourceId: String , completion: @escaping ( Data ? , URLResponse ? , Error ? ) -> Void ) {
31+ guard var url = URL ( string: PAPIEndpoint) else { completion ( nil , nil , " Unable to create URL. " ) ; return }
32+
33+ url. appendPathComponent ( PAPI . Sources. pathEntry)
34+ url. appendPathComponent ( sourceId)
35+ url. appendPathComponent ( PAPI . EdgeFunctions. pathEntry)
36+ url. appendPathComponent ( " delete-code " )
37+
38+ var request = URLRequest ( url: url, cachePolicy: . reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30 )
39+ request. httpMethod = " DELETE "
40+ request. addValue ( " Bearer \( token) " , forHTTPHeaderField: " Authorization " )
41+
42+ let task = URLSession . shared. dataTask ( with: request, completionHandler: completion)
43+ task. resume ( )
44+ }
45+
3046 // http://blah.com/whatever/create?sourceId=1
3147
3248 func createNewVersion( token: String , sourceId: String , code: String , completion: @escaping ( Data ? , URLResponse ? , Error ? ) -> Void ) {
@@ -36,12 +52,27 @@ extension PAPI {
3652 url. appendPathComponent ( PAPI . Sources. pathEntry)
3753 url. appendPathComponent ( sourceId)
3854 url. appendPathComponent ( PAPI . EdgeFunctions. pathEntry)
39-
55+ url. appendPathComponent ( " create " )
56+
4057 var request = URLRequest ( url: url, cachePolicy: . reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30 )
4158 request. httpMethod = " POST "
4259 request. addValue ( " Bearer \( token) " , forHTTPHeaderField: " Authorization " )
4360 request. addValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
44- request. httpBody = " { \" code \" : \" \( code) \" , \" sourceId \" : \" \( sourceId) \" } " . data ( using: . utf8)
61+
62+ let payload : [ String : Any ] = [
63+ " code " : code,
64+ " sourceId " : sourceId
65+ ]
66+
67+ do {
68+ request. httpBody = try JSONSerialization . data ( withJSONObject: payload, options: [ ] )
69+ if let bodyString = String ( data: request. httpBody!, encoding: . utf8) {
70+ print ( " Request payload: \( bodyString) " )
71+ }
72+ } catch {
73+ completion ( nil , nil , " Failed to create JSON payload: \( error) " )
74+ return
75+ }
4576
4677 let task = URLSession . shared. dataTask ( with: request, completionHandler: completion)
4778 task. resume ( )
0 commit comments