@@ -26,109 +26,125 @@ class ApiAction: NSObject, URLSessionDelegate, URLSessionDataDelegate, URLSessio
2626 return
2727 }
2828
29- URLCache . shared. removeAllCachedResponses ( )
30- var path = " "
31-
32- switch endpoint {
33- case " jamf-pro-version " :
34- path = " v1/ \( endpoint) "
35- default :
36- path = " v2/ \( endpoint) "
37- }
38-
39- var urlString = " \( serverUrl) /uapi/ \( path) "
40- urlString = urlString. replacingOccurrences ( of: " //uapi " , with: " /uapi " )
41- // print("[ApiAction] urlString: \(urlString)")
42-
43- let url = URL ( string: " \( urlString) " )
44- let configuration = URLSessionConfiguration . ephemeral
45- var request = URLRequest ( url: url!)
46- request. httpMethod = method
47-
48- // print("[ApiAction.action] Attempting \(method) on \(urlString).")
49-
50- configuration. httpAdditionalHeaders = [ " Authorization " : " Bearer \( JamfProServer . accessToken) " , " Content-Type " : " application/json " , " Accept " : " application/json " ]
51- let session = Foundation . URLSession ( configuration: configuration, delegate: self as URLSessionDelegate , delegateQueue: OperationQueue . main)
52- let task = session. dataTask ( with: request as URLRequest , completionHandler: {
53- ( data, response, error) -> Void in
54- session. finishTasksAndInvalidate ( )
55- if let httpResponse = response as? HTTPURLResponse {
56- if httpResponse. statusCode >= 200 && httpResponse. statusCode <= 299 {
57- let json = try ? JSONSerialization . jsonObject ( with: data!, options: . allowFragments)
58- if let endpointJSON = json! as? [ String : Any ] {
59- WriteToLog . shared. message ( " [ApiAction.action] Token retrieved from \( urlString) . " )
60- completion ( endpointJSON)
61- return
62- } else { // if let endpointJSON error
63- WriteToLog . shared. message ( " [ApiAction.action] JSON error. " )
29+ JamfPro . shared. getToken ( serverUrl: serverUrl) {
30+ ( statusCode, result) in
31+ if result == " success " {
32+ URLCache . shared. removeAllCachedResponses ( )
33+ var path = " "
34+
35+ switch endpoint {
36+ case " jamf-pro-version " :
37+ path = " v1/ \( endpoint) "
38+ default :
39+ path = " v2/ \( endpoint) "
40+ }
41+
42+ var urlString = " \( serverUrl) /uapi/ \( path) "
43+ urlString = urlString. replacingOccurrences ( of: " //uapi " , with: " /uapi " )
44+ // print("[ApiAction] urlString: \(urlString)")
45+
46+ let url = URL ( string: " \( urlString) " )
47+ let configuration = URLSessionConfiguration . ephemeral
48+ var request = URLRequest ( url: url!)
49+ request. httpMethod = method
50+
51+ // print("[ApiAction.action] Attempting \(method) on \(urlString).")
52+
53+ configuration. httpAdditionalHeaders = [ " Authorization " : " Bearer \( JamfProServer . accessToken) " , " Content-Type " : " application/json " , " Accept " : " application/json " ]
54+ let session = Foundation . URLSession ( configuration: configuration, delegate: self as URLSessionDelegate , delegateQueue: OperationQueue . main)
55+ let task = session. dataTask ( with: request as URLRequest , completionHandler: {
56+ ( data, response, error) -> Void in
57+ session. finishTasksAndInvalidate ( )
58+ if let httpResponse = response as? HTTPURLResponse {
59+ if httpResponse. statusCode >= 200 && httpResponse. statusCode <= 299 {
60+ let json = try ? JSONSerialization . jsonObject ( with: data!, options: . allowFragments)
61+ if let endpointJSON = json! as? [ String : Any ] {
62+ WriteToLog . shared. message ( " [ApiAction.action] Token retrieved from \( urlString) . " )
63+ completion ( endpointJSON)
64+ return
65+ } else { // if let endpointJSON error
66+ WriteToLog . shared. message ( " [ApiAction.action] JSON error. " )
67+ completion ( [ : ] )
68+ return
69+ }
70+ } else { // if httpResponse.statusCode <200 or >299
71+ WriteToLog . shared. message ( " [ApiAction.action] Response error: \( httpResponse. statusCode) . " )
72+ completion ( [ : ] )
73+ return
74+ }
75+ } else {
76+ WriteToLog . shared. message ( " [ApiAction.action] GET response error. Verify url and port. " )
6477 completion ( [ : ] )
6578 return
6679 }
67- } else { // if httpResponse.statusCode <200 or >299
68- WriteToLog . shared. message ( " [ApiAction.action] Response error: \( httpResponse. statusCode) . " )
69- completion ( [ : ] )
70- return
71- }
80+ } )
81+ task. resume ( )
7282 } else {
73- WriteToLog . shared. message ( " [ApiAction.action] GET response error. Verify url and port. " )
7483 completion ( [ : ] )
7584 return
7685 }
77- } )
78- task. resume ( )
79-
86+ }
8087 } // func action - end
8188
8289 func classic( whichServer: String , serverUrl: String , endpoint: String , method: String , completion: @escaping ( _ returnedJSON: [ String : Any ] ) -> Void ) {
8390
84- if stopScan {
85- completion ( [ : ] )
86- return
87- }
88- var urlString = " \( serverUrl) /JSSResource/ \( endpoint) "
89- urlString = urlString. replacingOccurrences ( of: " //JSSResource " , with: " /JSSResource " )
90- // print("[classic] urlString: \(urlString)")
91-
92- let url = URL ( string: " \( urlString) " )
93- let configuration = URLSessionConfiguration . ephemeral
94- var request = URLRequest ( url: url!)
95- request. httpMethod = method
96-
97- print ( " [classic] Attempting \( method) on \( urlString) " )
98- print ( " [classic] Bearer \( JamfProServer . accessToken) " )
99-
100- URLCache . shared. removeAllCachedResponses ( )
101-
102- configuration. httpAdditionalHeaders = [ " Authorization " : " Bearer \( JamfProServer . accessToken) " , " Content-Type " : " application/json " , " Accept " : " application/json " ]
103- let session = Foundation . URLSession ( configuration: configuration, delegate: self as URLSessionDelegate , delegateQueue: OperationQueue . main)
104- let task = session. dataTask ( with: request as URLRequest , completionHandler: {
105- ( data, response, error) -> Void in
106- session. finishTasksAndInvalidate ( )
107- if let httpResponse = response as? HTTPURLResponse {
108- print ( " [classic] httpResponse: \( httpResponse) " )
109- if httpResponse. statusCode >= 200 && httpResponse. statusCode <= 299 {
110- let json = try ? JSONSerialization . jsonObject ( with: data!, options: . allowFragments)
111- if let endpointJSON = json! as? [ String : Any ] {
112- WriteToLog . shared. message ( " [classic] Retrieved information from \( urlString) . " )
113- completion ( endpointJSON)
114- return
115- } else { // if let endpointJSON error
116- WriteToLog . shared. message ( " [classic] JSON error. " )
117- completion ( [ : ] )
118- return
119- }
120- } else { // if httpResponse.statusCode <200 or >299
121- WriteToLog . shared. message ( " [classic] Response error: \( httpResponse. statusCode) . " )
91+ JamfPro . shared. getToken ( serverUrl: serverUrl) {
92+ ( statusCode, result) in
93+ if result == " success " {
94+ if stopScan {
12295 completion ( [ : ] )
12396 return
12497 }
98+
99+ var urlString = " \( serverUrl) /JSSResource/ \( endpoint) "
100+ urlString = urlString. replacingOccurrences ( of: " //JSSResource " , with: " /JSSResource " )
101+ // print("[classic] urlString: \(urlString)")
102+
103+ let url = URL ( string: " \( urlString) " )
104+ let configuration = URLSessionConfiguration . ephemeral
105+ var request = URLRequest ( url: url!)
106+ request. httpMethod = method
107+
108+ print ( " [classic] Attempting \( method) on \( urlString) " )
109+ print ( " [classic] Bearer \( JamfProServer . accessToken) " )
110+
111+ URLCache . shared. removeAllCachedResponses ( )
112+
113+ configuration. httpAdditionalHeaders = [ " Authorization " : " Bearer \( JamfProServer . accessToken) " , " Content-Type " : " application/json " , " Accept " : " application/json " ]
114+ let session = Foundation . URLSession ( configuration: configuration, delegate: self as URLSessionDelegate , delegateQueue: OperationQueue . main)
115+ let task = session. dataTask ( with: request as URLRequest , completionHandler: {
116+ ( data, response, error) -> Void in
117+ session. finishTasksAndInvalidate ( )
118+ if let httpResponse = response as? HTTPURLResponse {
119+ print ( " [classic] httpResponse: \( httpResponse) " )
120+ if httpResponse. statusCode >= 200 && httpResponse. statusCode <= 299 {
121+ let json = try ? JSONSerialization . jsonObject ( with: data!, options: . allowFragments)
122+ if let endpointJSON = json! as? [ String : Any ] {
123+ WriteToLog . shared. message ( " [classic] Retrieved information from \( urlString) . " )
124+ completion ( endpointJSON)
125+ return
126+ } else { // if let endpointJSON error
127+ WriteToLog . shared. message ( " [classic] JSON error. " )
128+ completion ( [ : ] )
129+ return
130+ }
131+ } else { // if httpResponse.statusCode <200 or >299
132+ WriteToLog . shared. message ( " [classic] Response error: \( httpResponse. statusCode) . " )
133+ completion ( [ : ] )
134+ return
135+ }
136+ } else {
137+ WriteToLog . shared. message ( " [classic] GET response error. Verify url and port. " )
138+ completion ( [ : ] )
139+ return
140+ }
141+ } )
142+ task. resume ( )
125143 } else {
126- WriteToLog . shared. message ( " [classic] GET response error. Verify url and port. " )
127144 completion ( [ : ] )
128145 return
129146 }
130- } )
131- task. resume ( )
147+ }
132148 }
133149
134150 func urlSession( _ session: URLSession , didReceive challenge: URLAuthenticationChallenge , completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) {
0 commit comments