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

"""
traceview.resources

This module contains the objects associated with API resources.

"""

import requests


class Resource(object):
    """ The Resource object.

    """

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

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

    def __call__(self):
        """

        """
        return self.get()

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

    @property
    def params(self):
        return {"key": self.api_key}

    def get(self):
        """

        """
        response = requests.get(self.path, params=self.params, allow_redirects=False)
        if response.status_code == requests.codes.ok: # pylint: disable-msg=E1101
            return response.json().get('data', None)
        else:
            return None


class User(Resource):

    PATH = "organization/users"


class Organization(Resource):

    PATH = "organization"

    def __init__(self, *args, **kwargs):
        super(Organization, self).__init__(*args, **kwargs)
        self.users = User(*args, **kwargs)


class App(Resource):

    PATH = "apps"

@@ -369,35 +369,76 @@
 self
-):%0A        %22%22%22%0A%0A        %22%22%22
+, *args, **kwargs):%0A        params = self.build_query_params(kwargs)
 %0A   
@@ -458,16 +458,29 @@
 elf.get(
+params=params
 )%0A%0A    @
@@ -594,76 +594,321 @@
     
-@property%0A    def params(self):%0A        return %7B%22key%22: self.api_key%7D
+def build_query_params(self, params=None):%0A        %22%22%22 Builds and returns a Dictionary of query parameters.%0A%0A        :params: (optional) Dictionary of query parameters.%0A%0A        %22%22%22%0A        if not isinstance(params, dict):%0A            params = %7B%7D%0A        params.update(%7B%22key%22: self.api_key%7D)%0A        return params
 %0A%0A  
@@ -917,24 +917,37 @@
 def get(self
+, params=None
 ):%0A        %22
@@ -948,29 +948,193 @@
      %22%22%22
-%0A%0A        %22%22%22
+ Perform a HTTP GET request for the given Resource.%0A%0A        :params: (optional) Dictionary of query parameters.%0A%0A        %22%22%22%0A        if params is None:%0A            params = %7B%7D%0A
 %0A       
@@ -1176,21 +1176,16 @@
  params=
-self.
 params, 
@@ -1239,17 +1239,17 @@
 us_code 
-=
+!
 = reques
@@ -1306,109 +1306,202 @@
    r
-eturn response.json().get('data', None)%0A        else:%0A            return None%0A%0A%0Aclass User(Resource):
+aise Exception(response.status_code, self.path, response.text)%0A%0A        return response.json()%5B'data'%5D%0A%0A%0Aclass User(Resource):%0A    %22%22%22 Returns a List of user data for your organization.%0A%0A    %22%22%22
 %0A%0A  
@@ -1553,32 +1553,92 @@
 ation(Resource):
+%0A    %22%22%22 Returns a Dictionary of organization data.%0A%0A    %22%22%22
 %0A%0A    PATH = %22or
@@ -1821,23 +1821,575 @@
 e):%0A
-%0A    PATH = %22apps%22
+    %22%22%22 Returns a List of all available apps.%0A%0A    %22%22%22%0A%0A    PATH = %22apps%22%0A%0A%0Aclass Layer(Resource):%0A    %22%22%22 Returns a List of all layers reporting data recently for the given app.%0A    The default time window for reported layers is 1 day.%0A%0A    :param app: The app name to list layers.%0A    :param since_time: (optional) The start of the time window as a UTC timestamp in milliseconds.%0A%0A    %22%22%22%0A%0A    PATH = %22layers/%7Bapp%7D%22%0A%0A    def __call__(self, app, *args, **kwargs):%0A        self.PATH = self.PATH.format(app=app)%0A        return super(Layer, self).__call__(*args, **kwargs)
 %0A

Update resources.py file.