import requests
import json

from requests.auth import HTTPBasicAuth

class SocorroMiddleware(object):
    def __init__(self):
        self.base_url = 'http://localhost:8080/bpapi'
        self.http_host = 'socorro-api-dev-internal'
        self.username = 'dbrwaccess'
        self.password = ''
  
    def fetch(self, url):
        headers = {'Host': self.http_host}
        resp = requests.get(url, auth=(self.username, self.password),
                            headers=headers)
        print resp
        return json.loads(resp.content)

    def current_versions(self):
        url = '%s/current/versions/' % self.base_url
        print url
        return self.fetch(url)['currentversions']

    def adu_by_day(self, product, versions, os_names, start_date, end_date):
        params = {
            'base_url': self.base_url,
            'product': product,
            'versions': versions,
            'os_names': os_names,
            'start_date': start_date,
            'end_date': end_date,
        }
        url = '%(base_url)s/adu/byday/p/%(product)s/v/%(versions)s/rt/any/os/%(os_names)s/start/%(start_date)s/end/%(end_date)s' % params
        return self.fetch(url)

    def tcbs(self, product, version, end_date, duration, limit=300):
        params = {
            'base_url': self.base_url,
            'product': product,
            'version': version,
            'end_date': end_date,
            'duration': duration,
            'limit': limit,
        }

        url = '%(base_url)s/crashes/signatures/product/%(product)s/version/%(version)s/crash_type/browser/end_date/%(end_date)s/duration/%(duration)s/limit/%(limit)s/' % params
        return self.fetch(url)

    def search(self, product, versions, os_names, start_date, end_date,
               limit=100):
        params = {
            'base_url': self.base_url,
            'product': product,
            'versions': versions,
            'os_names': os_names,
            'start_date': start_date,
            'end_date': end_date,
            'limit': limit,
        }
        url = '%(base_url)s/search/signatures/products/%(product)s/in/signature/search_mode/contains/to/%(end_date)s/from/%(start_date)s/report_type/any/report_process/any/result_number/%(limit)s/' % params
        return self.fetch(url)


@@ -61,16 +61,49 @@
 asicAuth
+%0Afrom django.conf import settings
 %0A%0Aclass 
@@ -181,89 +181,81 @@
 l = 
-'http://localhost:8080/bpapi'%0A        self.http_host = 'socorro-api-dev-internal'
+settings.MWARE_BASE_URL%0A        self.http_host = settings.MWARE_HTTP_HOST
 %0A   
@@ -279,20 +279,31 @@
 e = 
-'dbrwaccess'
+settings.MWARE_USERNAME
 %0A   
@@ -327,10 +327,31 @@
 d = 
-''
+settings.MWARE_PASSWORD
 %0A  %0A

use django settings