from queue import Queue
from threading import Thread

import requests

from tsparser import config
from tsparser.utils import StatisticDataCollector


send_queue = Queue()


class Sender(Thread):
    def run(self):
        while True:
            data, url = send_queue.get()
            _send_data(data, url)
            StatisticDataCollector().on_request_sent((data, url))
            send_queue.task_done()


def send_data(data, url):
    """
    Add data to send to request queue

    :param data: data to send
    :type data: dict
    :param url: url where data are sent to
    :type url: str
    """
    send_queue.put((data, url))
    StatisticDataCollector().on_new_request((data, url))


def _send_data(data, url):
    """
    Send data to server

    :param data: data to send
    :type data: dict
    :param url: url where data are sent to
    :type url: str
    :return True if data have been sent successfully; False otherwise
    :rtype bool
    """
    try:
        response = requests.post(url, data=data,
                                 auth=(config.USERNAME, config.PASSWORD))
    except Exception as err:
        StatisticDataCollector().get_logger().log('sender', '{}: {}'.format(err.__class__.__name__, err))
        return False
    return response.status_code == 201

@@ -249,16 +249,22 @@
 ata, url
+, file
  = send_
@@ -307,16 +307,22 @@
 ata, url
+, file
 )%0A      
@@ -433,32 +433,43 @@
 d_data(data, url
+, file=None
 ):%0A    %22%22%22%0A    A
@@ -611,24 +611,116 @@
 pe url: str%0A
+    :param file: content of file to send as multiple encoded file%0A    :type file: bytearray%0A
     %22%22%22%0A    
@@ -740,24 +740,30 @@
 t((data, url
+, file
 ))%0A    Stati
@@ -836,16 +836,27 @@
 ata, url
+, file=None
 ):%0A    %22
@@ -992,24 +992,116 @@
 pe url: str%0A
+    :param file: content of file to send as multiple encoded file%0A    :type file: bytearray%0A
     :return 
@@ -1191,16 +1191,45 @@
    try:%0A
+        if file is None:%0A    
         
@@ -1302,16 +1302,20 @@
         
+   
+ 
 auth=(co
@@ -1347,16 +1347,236 @@
 SWORD))%0A
+        else:%0A            response = requests.post(url, data=data,%0A                                     auth=(config.USERNAME, config.PASSWORD),%0A                                     files=%7B'photo': (%22photo.jpg%22, file)%7D)%0A
     exce

add support for sending photos