Skip to content

Instantly share code, notes, and snippets.

@dmsl1805
Created April 5, 2017 08:35
Show Gist options
  • Save dmsl1805/27148077f339afe9026b15b34d5b1da0 to your computer and use it in GitHub Desktop.
Save dmsl1805/27148077f339afe9026b15b34d5b1da0 to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController {
fileprivate let databaseURL = "postgres://nwritrny:[email protected]:5432/nwritrny"
fileprivate let apiURL = "http://api.rutetiderframework.com"
@IBAction func subscribeAction(_ sender: Any) {
let headers = ["content-type": "application/x-www-form-urlencoded"]
let postData = NSMutableData(data: "url=\(databaseURL)".data(using: .utf8)!)
postData.append("&user_id=1251252".data(using: .utf8)!)
postData.append("&group_name=PD-3431".data(using: .utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "\(apiURL)/subscribers/add_subscriber")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "PUT"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
}
@IBAction func getSubscriptionInfoAction(_ sender: Any) {
let headers = ["content-type": "application/x-www-form-urlencoded"]
let postData = NSMutableData(data: "url=\(databaseURL)".data(using: .utf8)!)
postData.append("&user_id=1251252".data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "\(apiURL)/subscribers/get_subscriber_group")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else if let jsonData = data {
do {
let json = try JSONSerialization.jsonObject(with: jsonData) as? Dictionary<String, Any>
print(json?["group"])
} catch let error{
print(error)
}
}
})
dataTask.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment