# -*- coding: utf-8 -*-

"""
traceview.resources

This module contains the objects associated with API resources.

"""

import logging

import requests


log = logging.getLogger(__name__)


class Resource(object):
    """ The :class:`Resource <Resource>` object.

    Acts as a base class for TraceView API resources.

    """

    AUTHORITY = "https://api.tv.appneta.com"
    VERSION = "api-v2"
    PATH = None

    def __init__(self, api_key):
        self.api_key = api_key
        self.path = self.PATH

    @property
    def uri(self):
        return "{0}/{1}/{2}".format(self.AUTHORITY, self.VERSION, self.path)

    def build_query_params(self, params=None):
        """ Builds and returns a Dictionary of query parameters.

        :params: (optional) Dictionary of query parameters.

        """
        if not isinstance(params, dict):
            params = {}
        params.update({"key": self.api_key})
        return params

    def get(self, *args, **kwargs):
        """ Perform a HTTP GET request for the given Resource.

        :param dict kwargs: (optional) Query parameters for the request.

        """
        params = self.build_query_params(kwargs)

        if self.path is None:
            raise Exception("Pump fake")

        log.debug(self.uri, params)
        response = requests.get(self.uri, params=params, allow_redirects=False)
        if response.status_code != requests.codes.ok: # pylint: disable-msg=E1101
            raise Exception(response.status_code, self.uri, response.text)

        return response.json()['data']

@@ -404,16 +404,70 @@
 H = None
+%0A    __methods = %5B%0A        'get',%0A        'post'%0A    %5D
 %0A%0A    de
@@ -993,24 +993,36 @@
     def 
-get(self
+request(self, method
 , *args,
@@ -1064,44 +1064,88 @@
 TTP 
-GET 
 request
- for the given Resource.%0A
+.%0A%0A        :params str method: The HTTP method to perform on the request.
 %0A   
@@ -1289,106 +1289,345 @@
     
-if self.path is None:%0A            raise Exception(%22Pump fake%22)%0A%0A        log.debug(self.uri, params
+method = method.lower()%0A        if method not in self.__methods:%0A            raise requests.HTTPError(%22HTTP method is unsupported: %25s%22 %25 (method,))%0A%0A        if self.path is None:%0A            raise requests.URLRequired%0A%0A        log.debug(%22%25s %25s %25s%22 %25 (method.upper(), self.uri, params,))%0A%0A        request_method = getattr(requests, method
 )%0A  
@@ -1654,13 +1654,15 @@
 uest
-s.get
+_method
 (sel
@@ -1810,17 +1810,26 @@
 ise 
-Exception
+requests.HTTPError
 (res
@@ -1912,8 +1912,457 @@
 'data'%5D%0A
+%0A    def get(self, *args, **kwargs):%0A        %22%22%22 Perform a HTTP GET request.%0A%0A        :param dict kwargs: (optional) Query parameters for the request.%0A%0A        %22%22%22%0A        return self.request(method='get', *args, **kwargs)%0A%0A    def post(self, *args, **kwargs):%0A        %22%22%22 Perform a HTTP POST request.%0A%0A        :param dict kwargs: (optional) Query parameters for the request.%0A%0A        %22%22%22%0A        return self.request(method='post', *args, **kwargs)%0A

Refactor to support POST HTTP method #5