From 3c36af9c2fff2738df6dc63c3c686f02cc229a3a Mon Sep 17 00:00:00 2001 From: Shane Qi Date: Sat, 22 Jul 2017 16:59:50 -0500 Subject: [PATCH 1/4] Removed PerfectCURL and ZEGUpdateHandler protocol. --- Package.pins | 12 ++++++++++++ Package.swift | 3 +-- Sources/Methods.swift | 18 +++++++++--------- Sources/ZEGBot.swift | 41 +++++++++++++++-------------------------- 4 files changed, 37 insertions(+), 37 deletions(-) create mode 100644 Package.pins diff --git a/Package.pins b/Package.pins new file mode 100644 index 0000000..74ebfee --- /dev/null +++ b/Package.pins @@ -0,0 +1,12 @@ +{ + "autoPin": true, + "pins": [ + { + "package": "SwiftyJSON", + "reason": null, + "repositoryURL": "https://github.com/IBM-Swift/SwiftyJSON.git", + "version": "15.0.6" + } + ], + "version": 1 +} \ No newline at end of file diff --git a/Package.swift b/Package.swift index 24c459b..a560733 100644 --- a/Package.swift +++ b/Package.swift @@ -3,7 +3,6 @@ import PackageDescription let package = Package( name: "ZEGBot", dependencies: [ - .Package(url: "https://github.com/IBM-Swift/SwiftyJSON.git", majorVersion: 15), - .Package(url: "https://github.com/PerfectlySoft/Perfect-CURL.git", versions: Version(0,0,0).. JSON? { - guard var bodyBytes = JSON(payload).rawString()?.bytes() else { + guard let bodyData = try? JSON(payload).rawData() else { Log.warning(onMethod: method) return nil } + var request = URLRequest(url: URL(string: urlPrefix + method)!) + request.httpMethod = "POST" + request.httpBody = bodyData + request.addValue("application/json", forHTTPHeaderField: "Content-Type") + let task = session.dataTask(with: request) { _, _, _ in + } + task.resume() - let curl = CURL() - curl.url = urlPrefix + method - curl.setOption(CURLOPT_POSTFIELDS, v: &bodyBytes) - curl.setOption(CURLOPT_HTTPHEADER, s: PARAM.POST_JSON_HEADER_CONTENT_TYPE) - - return JSON(data: Data(bytes: curl.performFully().2)) + return nil } diff --git a/Sources/ZEGBot.swift b/Sources/ZEGBot.swift index 8ecc76d..49bbb6c 100644 --- a/Sources/ZEGBot.swift +++ b/Sources/ZEGBot.swift @@ -8,42 +8,37 @@ // Licensed under Apache License v2.0 // -import cURL -import PerfectCURL import SwiftyJSON import Foundation public struct ZEGBot { + internal let session = URLSession(configuration: .default) + internal var urlPrefix: String public init(token: String) { self.urlPrefix = "https://api.telegram.org/bot"+token+"/" } - public func run(with handler: @escaping ZEGUpdateHandle ) { - - let curl = CURL() - var offset = 0 - + public func run(withHandler handler: @escaping UpdateHandler) { + getUpdates(offset: 0, handler: handler) while true { + if readLine() == "stop" { return } + } + } - curl.url = urlPrefix + "getupdates?timeout=60&offset=\(offset)" - - guard let updates = ZEGBot.decodeUpdates(from: Data(bytes: curl.performFully().2)) else { continue } - - if let lastUpdate = updates.last { offset = lastUpdate.updateId + 1 } - + private func getUpdates(offset: Int, handler: @escaping UpdateHandler) { + let task = session.dataTask(with: URL(string: urlPrefix + "getupdates?timeout=60&offset=\(offset)")!) { data, _, error in + guard let updates = ZEGBot.decodeUpdates(from: data!) else { return } for update in updates { handler(update, self) } - + var newOffset = offset + if let lastUpdate = updates.last { newOffset = lastUpdate.updateId + 1 } + self.getUpdates(offset: newOffset, handler: handler) } - - } - - public func run(with handler: ZEGUpdateHandler) { - run(with: handler.handle) + task.resume() } } @@ -66,10 +61,4 @@ extension ZEGBot { } -public typealias ZEGUpdateHandle = (Update, ZEGBot) -> Void - -public protocol ZEGUpdateHandler { - - func handle(update: Update, bot: ZEGBot) - -} +public typealias UpdateHandler = (Update, ZEGBot) -> Void From 79abd9969f0553fc01b2d3ed9c578cd74e70fbae Mon Sep 17 00:00:00 2001 From: Shane Qi Date: Sat, 22 Jul 2017 20:30:30 -0500 Subject: [PATCH 2/4] Began using Swift Dispatch framework. --- Sources/Methods.swift | 21 ++++++++++++++++----- Sources/ZEGBot.swift | 30 ++++++++++++++++-------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/Sources/Methods.swift b/Sources/Methods.swift index b1b5eb0..ab8598f 100644 --- a/Sources/Methods.swift +++ b/Sources/Methods.swift @@ -10,7 +10,9 @@ import SwiftyJSON import Foundation +import Dispatch +/// All methods are performed synchronized. extension ZEGBot { @discardableResult @@ -21,7 +23,7 @@ extension ZEGBot { var payload: [String: Any] = [ PARAM.TEXT: text - ] + ] if let parseMode = parseMode { payload[PARAM.PARSE_MODE] = parseMode.rawValue } if disableWebPagePreview { payload[PARAM.DISABLE_WEB_PAGE_PREVIEW] = true } @@ -254,21 +256,30 @@ extension ZEGBot { } internal func perform(method: String, payload: [String: Any]) -> JSON? { + if let data: Data = performRequest(ofMethod: method, payload: payload) { + return JSON(data: data) + } + return nil + } + private func performRequest(ofMethod method: String, payload: [String: Any]) -> Data? { guard let bodyData = try? JSON(payload).rawData() else { Log.warning(onMethod: method) return nil } + var resultData: Data? + let semaphore = DispatchSemaphore(value: 0) var request = URLRequest(url: URL(string: urlPrefix + method)!) request.httpMethod = "POST" request.httpBody = bodyData request.addValue("application/json", forHTTPHeaderField: "Content-Type") - let task = session.dataTask(with: request) { _, _, _ in + let task = URLSession(configuration: .default).dataTask(with: request) { data, _, _ in + resultData = data + semaphore.signal() } task.resume() - - return nil - + semaphore.wait() + return resultData } } diff --git a/Sources/ZEGBot.swift b/Sources/ZEGBot.swift index 49bbb6c..322e8d1 100644 --- a/Sources/ZEGBot.swift +++ b/Sources/ZEGBot.swift @@ -10,6 +10,7 @@ import SwiftyJSON import Foundation +import Dispatch public struct ZEGBot { @@ -22,23 +23,24 @@ public struct ZEGBot { } public func run(withHandler handler: @escaping UpdateHandler) { - getUpdates(offset: 0, handler: handler) + var offset = 0 + let semaphore = DispatchSemaphore(value: 0) while true { - if readLine() == "stop" { return } - } - } - - private func getUpdates(offset: Int, handler: @escaping UpdateHandler) { - let task = session.dataTask(with: URL(string: urlPrefix + "getupdates?timeout=60&offset=\(offset)")!) { data, _, error in - guard let updates = ZEGBot.decodeUpdates(from: data!) else { return } - for update in updates { - handler(update, self) + let task = session.dataTask(with: URL(string: urlPrefix + "getupdates?timeout=60&offset=\(offset)")!) { data, _, _ in + guard let updatesData = data, + let updates = ZEGBot.decodeUpdates(from: updatesData) else { + semaphore.signal() + return + } + if let lastUpdate = updates.last { offset = lastUpdate.updateId + 1 } + semaphore.signal() + for update in updates { + handler(update, self) + } } - var newOffset = offset - if let lastUpdate = updates.last { newOffset = lastUpdate.updateId + 1 } - self.getUpdates(offset: newOffset, handler: handler) + task.resume() + semaphore.wait() } - task.resume() } } From 0018362c628e5c2ece29cabb76875f8fcfef44e9 Mon Sep 17 00:00:00 2001 From: Shane Qi Date: Sat, 22 Jul 2017 20:45:02 -0500 Subject: [PATCH 3/4] Added build script. --- .travis.yml | 11 +---------- scripts/build.sh | 10 ++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 scripts/build.sh diff --git a/.travis.yml b/.travis.yml index 28dcc3d..6c833a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,13 +4,4 @@ services: - docker script: - - sudo docker run - -v `pwd`/:/ZEGBot - swift:latest - /bin/sh -c - " - apt-get update; - apt-get install uuid-dev -y; - cd ZEGBot; - swift build; - " + - sudo sh ./scripts/build.sh diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100644 index 0000000..6878147 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,10 @@ +sudo docker run \ +-v `pwd`/:/ZEGBot \ +swift:latest \ +/bin/sh -c \ +"\ +apt-get update;\ +apt-get install uuid-dev -y;\ +cd ZEGBot;\ +swift build;\ +" From 1b6001c03a5368d76658d33deb4ffaedc493becb Mon Sep 17 00:00:00 2001 From: Shane Qi Date: Sat, 22 Jul 2017 21:13:35 -0500 Subject: [PATCH 4/4] Updated README.md. --- README.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ebd28df..0cceafe 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,12 @@ This library wraps the JSON decoding processing, making it easy to decode incomi This library wraps the processing of converting objects to Telegram Bot API request parameters and the processing of performing request, making it easy to handle incoming update. -**This is a server-side swift library powered by [Perfect](https://github.com/PerfectlySoft/Perfect). -Which will help you build your own Perfect Telegram Bot!** - ## Installation Add this project as a dependency in your Package.swift file. ```swift -.Package(url: "https://github.com/ShaneQi/ZEGBot.git", versions: Version(0,0,0)..