124 lines
4.6 KiB
Swift
124 lines
4.6 KiB
Swift
//
|
|
// OTRS.swift
|
|
// OTRS-Watch
|
|
//
|
|
// Created by Gerrit Linnemann on 06.12.16.
|
|
// Copyright © 2016 Adawim UG (haftungsbeschränkt). All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class OTRS {
|
|
|
|
var url : String;
|
|
var username : String;
|
|
var password : String;
|
|
|
|
|
|
//MARK: init
|
|
|
|
init(url_:String, username_:String, password_:String) {
|
|
self.url = url_
|
|
self.username = username_
|
|
self.password = password_
|
|
}
|
|
|
|
|
|
//MARK: REST
|
|
|
|
func get(forQue:Int) {
|
|
|
|
let config = URLSessionConfiguration.default // Session configuration
|
|
let session = URLSession(configuration: config) // Load configuration into session
|
|
let url = URL(string: buildURLForGettingNew(queID: forQue))
|
|
|
|
let task = session.dataTask(with: url!, completionHandler: {
|
|
(data, response, error) in
|
|
|
|
if error != nil {
|
|
print(error!.localizedDescription)
|
|
} else {
|
|
do {
|
|
if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] {
|
|
// work with json ...
|
|
//print(json)
|
|
|
|
var cnt = 0;
|
|
|
|
for object in json {
|
|
let ticketIDs = object.value as! Array<String>
|
|
//print(ticketIDs)
|
|
|
|
for ticketID in ticketIDs {
|
|
if let tID:Int = Int(ticketID) {
|
|
self.fetch(ticket: tID, session: session)
|
|
cnt = cnt+1;
|
|
}
|
|
}
|
|
}
|
|
|
|
let nc = NotificationCenter.default
|
|
nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.LIST.UPDATED),
|
|
object: nil,
|
|
userInfo: ["count":cnt])
|
|
}
|
|
} catch {
|
|
print("Error in JSONSerialization")
|
|
}
|
|
}
|
|
})
|
|
|
|
task.resume()
|
|
}
|
|
|
|
func fetch(ticket:Int, session:URLSession) {
|
|
let url = URL(string: buildURLForGettingTicketInfo(ticketID: ticket))
|
|
|
|
let task = session.dataTask(with: url!, completionHandler: { // see: https://developer.apple.com/swift/blog/?id=37
|
|
(data, response, error) in
|
|
|
|
if error != nil {
|
|
print(error!.localizedDescription)
|
|
} else {
|
|
do {
|
|
let dictionary = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String : AnyObject]
|
|
let ticketData : Dictionary = dictionary["Ticket"]?.firstObject as! [String : AnyObject]
|
|
let ticket = try Ticket(source: ticketData)
|
|
//print(ticket)
|
|
|
|
let nc = NotificationCenter.default
|
|
nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.IN),
|
|
object: nil,
|
|
userInfo: ["message":ticket.id, "ticket":ticket])
|
|
|
|
} catch SerializationError.missing(let marker) {
|
|
print("Error in JSONSerialization for ticket #\(ticket) at \"\(marker)\"")
|
|
} catch {
|
|
print("Error in JSONSerialization for ticket #\(ticket): \(error)")
|
|
}
|
|
}
|
|
})
|
|
|
|
task.resume()
|
|
}
|
|
|
|
|
|
//MARK: Build urls
|
|
|
|
private func buildURLForGettingNew(queID:Int) -> String {
|
|
return buildURLForGettingListByState(forState: "new", queID: queID)
|
|
}
|
|
|
|
private func buildURLForGettingOpen(queID:Int) -> String {
|
|
return buildURLForGettingListByState(forState: "open", queID: queID)
|
|
}
|
|
|
|
private func buildURLForGettingListByState(forState:String, queID:Int) -> String {
|
|
return "http://saeotrs01.sae.intra/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket?UserLogin=\(self.username)&Password=\(self.password)&QueueIDs=\(queID)&States=\(forState)"
|
|
}
|
|
|
|
private func buildURLForGettingTicketInfo(ticketID:Int) -> String {
|
|
return "http://saeotrs01.sae.intra/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/\(ticketID)?UserLogin=\(self.username)&Password=\(self.password)"
|
|
}
|
|
}
|