Commit 1cf4f2b8 authored by Paul Bird's avatar Paul Bird

Replace operateAPIUtilities.swift

parent 56b30e96
......@@ -10,12 +10,15 @@ import Foundation
class operateAPIUtilities {
//
// Need to add client Secret below
//
static let clientSecret = ""
static let oauth2Base = "https://api.occupie.com/oauth2/token"
//static let baseURL = "https://api.occupie.com/api/1.0/"
static let baseURL = "https://eu1.occupie.com/api/1.0/"
static let baseURL = "https://api.occupie.com/api/1.0/"
static func queryStringForCalendar(resource: String,
......@@ -56,6 +59,7 @@ class operateAPIUtilities {
return queryString
}
static func queryStringForProduct(_ location: String,
isMeetingRoom: Bool,
fields: String) -> String {
......@@ -73,25 +77,6 @@ class operateAPIUtilities {
}
static func createArea() {
let putString = "data={ 'areas': [ { 'areaname': 'N33' } ] }"
let req = putRequestUsingAccessToken(query: "\(baseURL)area", putBody: putString)
let task = URLSession.shared.dataTask(with: req, completionHandler: { data, response, error in
if let httpStatus = response as? HTTPURLResponse,
httpStatus.statusCode != 200 {
print("httpStatus.statusCode = \(httpStatus.statusCode)")
return
}
let responseString = String(data: data!, encoding: String.Encoding.utf8)
if let dataFromString = responseString?.data(using: String.Encoding.utf8, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
print(json)
}
})
task.resume()
}
static func deleteChargeSheet(id: String) {
let queryString = "\(baseURL)chargesheet"
......@@ -123,6 +108,7 @@ class operateAPIUtilities {
task.resume()
}
static func deleteNote(id: String) {
let req = deleteRequest(endpoint: "note", id: id)
......@@ -154,7 +140,7 @@ class operateAPIUtilities {
// Need to Add client Secret
var postString = "grant_type=password&client_id=trustedclient"
postString += "&client_secret="
postString += "&client_secret=\(clientSecret)"
postString += "&scope=hubapi&username=\(username)&password=\(password)"
request.httpBody = postString.data(using: String.Encoding.utf8)
......@@ -175,7 +161,7 @@ class operateAPIUtilities {
//handle errors here
return
}
let responseString = String(data: data!, encoding: String.Encoding.utf8)
let responseString = String(data: data!, encoding: String.Encoding.utf8)
if let dataFromString = responseString?.data(using: String.Encoding.utf8, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
print(json)
......@@ -183,13 +169,13 @@ class operateAPIUtilities {
print("Access Token")
print(access_token)
UserDefaultsHelper.setAccessToken(access_token)
}
if let refresh_token = json["refresh_token"].string {
}
if let refresh_token = json["refresh_token"].string {
print("Refresh Token")
print(refresh_token)
UserDefaultsHelper.setRefreshToken(refresh_token)
}
}
}
})
task.resume()
}
......@@ -263,6 +249,7 @@ class operateAPIUtilities {
static func getLocations1() {
print("getLocations1() called")
let queryFields = "[locationid,locationname,officehoursstart,officehoursend]"
let query = queryStringForLocation(queryFields)
let request = NSMutableURLRequest(url: URL(string: query)!)
......@@ -273,7 +260,17 @@ class operateAPIUtilities {
let req: URLRequest = request as URLRequest
let task = URLSession.shared.dataTask(with: req, completionHandler: { data, response, error in
// set up the session
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 10
config.httpShouldSetCookies = false
let session = URLSession(configuration: config)
//let task = URLSession.shared.dataTask(with: req, completionHandler: { data, response, error in
let task = session.dataTask(with: req, completionHandler: { data, response, error in
print("req.allHTTPHeaderFields.debugDescription")
print(req.allHTTPHeaderFields.debugDescription)
guard error == nil && data != nil else {
print("error=\(String(describing: error))")
......@@ -281,7 +278,8 @@ class operateAPIUtilities {
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
//handle errors here
print("getLocations1() HTTPURLResponse.debugDescription()")
print(HTTPURLResponse.debugDescription())
}
let responseString = String(data: data!, encoding: String.Encoding.utf8)
......@@ -303,6 +301,7 @@ class operateAPIUtilities {
}
}
})
print("getLocations1() task.resume() called")
task.resume()
}
......@@ -373,6 +372,63 @@ class operateAPIUtilities {
}
static func putArea() {
print("putArea() called")
let putString = "data={ 'areas': [ { 'areaname': 'N33' } ] }"
let req = putRequestUsingAccessTokenJSON(query: "\(baseURL)area", putBody: putString)
// set up the session
let config = URLSessionConfiguration.default
config.httpShouldSetCookies = false
let session = URLSession(configuration: config)
//let task = URLSession.shared.dataTask(with: req, completionHandler: { data, response, error in
let task = session.dataTask(with: req, completionHandler: { data, response, error in
print(response.debugDescription)
print(error.debugDescription)
if let httpStatus = response as? HTTPURLResponse,
httpStatus.statusCode != 200 {
print("httpStatus.statusCode = \(httpStatus.statusCode)")
return
}
guard let responseStringData = data else {
print("createArea data is nil")
return
}
let responseString = String(data: responseStringData, encoding: String.Encoding.utf8)
if let dataFromString = responseString?.data(using: String.Encoding.utf8, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
print(json)
}
})
task.resume()
print("putArea() task.resume() called")
}
fileprivate static func putRequestUsingAccessTokenJSON(
query: String,
putBody: String) -> URLRequest
{
let request = NSMutableURLRequest(url: URL(string: query)!)
request.httpMethod = "PUT"
request.addValue("Bearer \(UserDefaultsHelper.accessToken())", forHTTPHeaderField: "Authorization")
request.addValue("application/json; charset=utf-8",
forHTTPHeaderField: "Content-Type")
let putBodyData = putBody.data(using: String.Encoding.utf8)
let putBodyDataSize = putBodyData!.count
print(putBodyDataSize)
request.addValue("\(putBodyDataSize)", forHTTPHeaderField: "Content-Length")
request.httpBody = putBody.data(using: String.Encoding.utf8)
let req: URLRequest = request as URLRequest
return req
}
fileprivate static func putRequestUsingAccessToken(
query: String,
putBody: String) -> URLRequest
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment