11# -*- coding: utf-8 -*-
22
33from datetime import datetime
4- from json import JSONEncoder
54from .errors import ArgumentError
65from .errors import HttpError # noqa
76from .lib .setter_property import SetterProperty
7+ from .request import Request
8+ from .admin import Admin
9+ from .company import Company
10+ from .event import Event
11+ from .message import Message
12+ from .note import Note
13+ from .notification import Notification
14+ from .user import User
15+ from .subscription import Subscription
16+ from .tag import Tag
817
918import copy
10- import json
1119import random
1220import re
13- import requests
1421import time
1522
1623__version__ = '2.0.alpha'
1724
25+ __all__ = (
26+ Admin , Company , Event , Message , Note , Notification , Subscription , Tag , User
27+ )
28+
1829
1930RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \
2031 for usage examples."
@@ -35,7 +46,6 @@ class _Config(object):
3546 endpoints = None
3647 current_endpoint = None
3748 target_base_url = None
38- timeout = 10
3949 endpoint_randomized_at = None
4050
4151
@@ -57,6 +67,10 @@ def app_api_key(self):
5767 def app_api_key (self , value ):
5868 self ._config .app_api_key = value
5969
70+ @property
71+ def _auth (self ):
72+ return (self .app_id , self .app_api_key )
73+
6074 @property
6175 def _random_endpoint (self ):
6276 if self .endpoints :
@@ -144,54 +158,25 @@ class Intercom(object):
144158 __metaclass__ = IntercomType
145159
146160 @classmethod
147- def send_request_to_path (cls , method , path , params = None ):
148- """ Construct an API request, send it to the API, and parse the
149- response. """
150- req_params = {}
161+ def get_url (cls , path ):
151162 if '://' in path :
152163 url = path
153164 else :
154165 url = cls .current_endpoint + path
155-
156- headers = {
157- 'User-Agent' : 'python-intercom/' + __version__ ,
158- 'Accept' : 'application/json'
159- }
160- if method in ('POST' , 'PUT' , 'DELETE' ):
161- headers ['content-type' ] = 'application/json'
162- req_params ['data' ] = json .dumps (params , cls = ResourceEncoder )
163- elif method == 'GET' :
164- req_params ['params' ] = params
165- req_params ['headers' ] = headers
166-
167- resp = requests .request (
168- method , url , timeout = cls ._config .timeout ,
169- auth = (Intercom .app_id , Intercom .app_api_key ), ** req_params )
170-
171- print resp .status_code , resp .content
172- if resp .content :
173- return json .loads (resp .content )
166+ return url
174167
175168 @classmethod
176169 def get (cls , path , ** params ):
177- return cls .send_request_to_path ('GET' , path , params )
170+ return Request .send_request_to_path ('GET' , cls . get_url ( path ), cls . _auth , params )
178171
179172 @classmethod
180173 def post (cls , path , ** params ):
181- return cls .send_request_to_path ('POST' , path , params )
174+ return Request .send_request_to_path ('POST' , cls . get_url ( path ), cls . _auth , params )
182175
183176 @classmethod
184177 def put (cls , path , ** params ):
185- return cls .send_request_to_path ('PUT' , path , params )
178+ return Request .send_request_to_path ('PUT' , cls . get_url ( path ), cls . _auth , params )
186179
187180 @classmethod
188181 def delete (cls , path , ** params ):
189- return cls .send_request_to_path ('DELETE' , path , params )
190-
191-
192- class ResourceEncoder (JSONEncoder ):
193- def default (self , o ):
194- if hasattr (o , 'attributes' ):
195- # handle API resources
196- return o .attributes
197- return super (ResourceEncoder , self ).default (o )
182+ return Request .send_request_to_path ('DELETE' , cls .get_url (path ), cls ._auth , params )
0 commit comments