# Copyright 2014 Red Beacon, Inc.  All Rights Reserved
#
# This code, and all derivative work, is the exclusive property of
# Red Beacon, Inc. and may not be used without Red Beacon, Inc.'s
# authorization.
#
# Author: Gobind Ball

"""
okapi.api
~~~~~~~~~
This module implements the Requests API while storing valuable information into mongodb.
"""

import datetime
import requests
import time
import urlparse

from pymongo import MongoClient
from pymongo.errors import ConnectionFailure

# TODO:
# Depends on how we want to calculate the time to
# receieve the request form Home Depots API.
# There are several choices.
# Time.time, Time.clock, and a class from request called elapsed
# I have a test file that makes it seem that time.clock is fastest but is it most accurate?!?
# I have used time.clock for now

class Api(object):

    def __init__(self, project_name, host, port):
        """ initialization of class api"""
        self.host = host
        self.port = port
        self.project_name = project_name

        try:
            client = MongoClient(self.host, self.port)
            self.db = client.okapi
        except ConnectionFailure:
            self.db = None

    def request(self, method, url, **kwargs):
        """calls a method of request library while storing info about api call into mongo db"""
        start = time.clock()
        res = requests.request(method, url, **kwargs)
        end = time.clock()

        if self.db is not None:

            content = ''

            if not res.ok:
                content = res.content

                date = datetime.datetime.utcnow()
                host = urlparse.urlparse(res.url)

                data = {'content': content,
                        'date': date,
                        'host': host.hostname,
                        'method': method,
                        'project_name': self.project_name,
                        'response_time': (end - start),
                        'status_code': res.status_code,
                        'url': res.url,
                    }

                datas = self.db.datas
                datas.insert(data)

        return res

    def get(self, url, **kwargs):
        return self.request('GET', url, **kwargs)

    def delete(self, url, **kwargs):
        return self.request('DELETE', url, **kwargs)

    def post(self, url, **kwargs):
        return self.request('POST', url, **kwargs)

    def put(self, url, **kwargs):
        return self.request('PUT', url, **kwargs)

@@ -480,16 +480,54 @@
 nFailure
+%0Afrom pymongo.errors import InvalidURI
 %0A%0A# TODO
@@ -906,18 +906,41 @@
 me, 
-host, port
+mongodb_uri='mongodb://localhost'
 ):%0A 
@@ -949,18 +949,17 @@
      %22%22%22
- i
+I
 nitializ
@@ -976,19 +976,18 @@
 lass api
-%22%22%22
+.%0A
 %0A       
@@ -991,49 +991,145 @@
     
-self.host = host%0A        self.port = port
+See http://docs.mongodb.org/manual/reference/connection-string/ for%0A        more information about the mongodb_uri parameter.%0A        %22%22%22
 %0A   
@@ -1217,28 +1217,19 @@
 ent(
-self.host, self.port
+mongodb_uri
 )%0A  
@@ -1276,16 +1276,17 @@
  except 
+(
 Connecti
@@ -1294,16 +1294,29 @@
 nFailure
+, InvalidURI)
 :%0A      

Use mongodb_uri string to connect to the DB