Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
givip committed Mar 6, 2019
2 parents 1506c64 + 98b810e commit a70316e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
21 changes: 21 additions & 0 deletions Sources/Telegrammer/Helpers/HTTPRequest+Helper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// HTTPRequest.swift
// Telegrammer
//
// Created by Givi on 06/03/2019.
//

import Foundation
import HTTP

extension HTTPRequest {
var urlRequest: URLRequest {
var request = URLRequest(url: self.url)
request.httpMethod = "POST"
request.httpBody = self.body.data
self.headers.forEach { header in
request.addValue(header.value, forHTTPHeaderField: header.name)
}
return request
}
}
30 changes: 28 additions & 2 deletions Sources/Telegrammer/Network/BotClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class BotClient {
let port: Int

let token: String
var client: HTTPClient?
//Due to memory leak in HTTPClient, temporarely switching on URLSession
// var client: HTTPClient?

let worker: Worker
let callbackWorker: Worker

Expand Down Expand Up @@ -53,6 +55,27 @@ public class BotClient {
}

private func send<T: Codable>(request: HTTPRequest) -> Future<TelegramContainer<T>> {
let promise = worker.eventLoop.newPromise(of: TelegramContainer<T>.self)

URLSession.shared.dataTask(with: request.urlRequest) { (data, response, error) in
if let error = error {
promise.fail(error: error)
return
}
if let data = data {
do {
let response = try JSONDecoder().decode(TelegramContainer<T>.self, from: data)
promise.succeed(result: response)
} catch {
promise.fail(error: error)
}
}
}.resume()

return promise.futureResult

//Due to memory leak in HTTPClient, temporarely switching on URLSession
/*
var futureClient: Future<HTTPClient>
if let existingClient = client {
Log.info("Using existing HTTP client")
Expand Down Expand Up @@ -82,17 +105,20 @@ public class BotClient {
Log.info("Decoding response from HTTPClient")
return try self.decode(response: response)
}
*/
}

func decode<T: Encodable>(response: HTTPResponse) throws -> TelegramContainer<T> {
///Temporary workaround for drop current HTTPClient state after each request,
///waiting for fixes from Vapor team
self.client = nil
//Due to memory leak in HTTPClient, temporarely switching on URLSession
// self.client = nil
if let data = response.body.data {
return try JSONDecoder().decode(TelegramContainer<T>.self, from: data)
}
throw BotError()
}


func apiUrl(endpoint: String) -> URL {
return URL(string: "https://\(host):\(port)/bot\(token)/\(endpoint)")!
Expand Down
6 changes: 5 additions & 1 deletion Sources/Telegrammer/Network/Longpolling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ public class Longpolling: Connection {
private func longpolling(with params: Bot.GetUpdatesParams) {
var requestBody = params
do {
try self.bot.getUpdates(params: requestBody).whenSuccess({ (updates) in
try self.bot.getUpdates(params: requestBody)
.catch { error in
self.retryRequest(with: params, after: error)
}
.whenSuccess({ (updates) in
if !updates.isEmpty {
if !self.cleanStart || !(self.cleanStart && self.isFirstRequest) {
self.dispatcher.enqueue(updates: updates)
Expand Down

0 comments on commit a70316e

Please sign in to comment.