ó
pAçRc           @   sÐ  d  d l  Z  d  d l Z d  d l Z d  d l j Z d  d l j Z y1 e j d k  rc d  d l Z	 n d  d l
 j Z	 Wn e k
 r† n Xy d  d l Z Wn e k
 r° d Z n Xy d  d l Z Wn e k
 rÚ d Z nˆ Xy> e j Z g  e j d ƒ D] Z e e ƒ ^ q÷ \ Z Z Z Wn e k
 r,n6 Xe e e f d k  rbe j j d e f ƒ d Z n  d „  Z d „  Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e f d „  ƒ  YZ! d S(   iÿÿÿÿNi   i    t   .i   sq  Warning: the test harness will only use your Python "requests"library if it is version 0.8.8 or newer, but your "requests" library is version %s. We will fall back to an alternate HTTP library so everything should work. We recommend upgrading your "requests" library. (HINT: running "pip install -U requests" should upgrade your requests library to the latest version.)c           C   s   t  j j t  j j t ƒ d ƒ S(   Ns   ca-certificates.crt(   t   ost   patht   joint   dirnamet   __file__(    (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyt
   certs_path4   s    c          O   sx   t  r t } n\ t r- t j d	 k  r- t } n> t } t j d
 k  rK d } n d } t j j	 d i | d 6ƒ | |  | Ž  S(   Ni   i    i   i   t   pycurlt   requestsså   Warning: The test harness is falling back to *urllib2*. Its SSL implementation doesn't verify server certificates (how's that for a distributed systems problem?). We recommend instead installing %(rec)s via `pip install %(rec)s`.t   rec(   i   i    (   i   i   (
   R   t   RequestsClientR   t   syst   version_infot   PycurlClientt   Urllib2Clientt   utilt   loggert   info(   t   argst   kwargst   implt   reccomendation(    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyt   new_default_http_client8   s    				t
   HTTPClientc           B   s#   e  Z i  e d  „ Z d d „ Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   _verify_ssl_certst   headers(   t   selfR   t   verify_ssl_certs(    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyt   __init__Q   s    	c         C   s   t  d ƒ ‚ d  S(   Ns.   HTTPClient subclasses must implement `request`(   t   NotImplementedError(   R   t   methodt   urlt	   post_data(    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyt   requestU   s    N(   t   __name__t
   __module__t   TrueR   t   NoneR!   (    (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR   O   s   R
   c           B   s#   e  Z d  Z d d „ Z d „  Z RS(   R   c   	   
   C   sø   i  } |  j  r t ƒ  | d <n
 t | d <yw y. t j | | d |  j d | d d | } Wn0 t k
 rŒ t j ƒ  } t d | f ƒ ‚ n X| j	 } | j
 } Wn* t k
 rÌ t j ƒ  } |  j | ƒ n Xt j d
 k rî | j d	 ƒ } n  | | f S(   Nt   verifyR   t   datat   timeoutiP   s  Warning: It looks like your installed version of the "requests" library is not compatible with Stripe's usage thereof. (HINT: The most likely cause is that your "requests" library is out of date. You can fix that by running "pip install -U requests".) The underlying error was: %si   i    s   utf-8(   i   i    (   R   R   t   FalseR   R!   R   t	   TypeErrorR   t   exception_ast   contentt   status_codet	   Exceptiont   _handle_request_errorR   R   t   decode(	   R   R   R   R    R   t   resultt   eR,   R-   (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR!   ]   s2    	
			c         C   s   t  | t j j ƒ r7 d t | ƒ j t | ƒ f } nF d t | ƒ j f } t | ƒ rs | d t | ƒ f 7} n
 | d 7} d | f } t j | ƒ ‚ d  S(   Ns   %s: %ss   A %s was raiseds    with error message %ss    with no error messages   Network error: %s(	   t
   isinstanceR   t
   exceptionst   RequestExceptiont   typeR"   t   strt   errort   HTTPConnectionError(   R   R2   t   errt   msg(    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR/   …   s    "
N(   R"   R#   t   nameR%   R!   R/   (    (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR
   Z   s   (R   c           B   s#   e  Z d  Z d d „ Z d „  Z RS(   R   c         C   sÐ  t  j j ƒ  } t j ƒ  } | d k r= | j t j d ƒ nN | d k rr | j t j d ƒ | j t j | ƒ n | j t j | j	 ƒ  ƒ | j t j
 t  j | ƒ ƒ | j t j | j ƒ | j t j d ƒ | j t j d ƒ | j t j d ƒ | j t j g  |  j j ƒ  D] \ } } d | | f ^ qƒ |  j rW| j t j t ƒ  ƒ n | j t j t ƒ y | j ƒ  Wn- t j k
 r§t  j ƒ  } |  j | ƒ n X| j ƒ  }	 | j t j ƒ }
 |	 |
 f S(   Nt   geti   t   posti   iP   s   %s: %s(    R   t   StringIOR   t   Curlt   setoptt   HTTPGETt   POSTt
   POSTFIELDSt   CUSTOMREQUESTt   uppert   URLt   utf8t   WRITEFUNCTIONt   writet   NOSIGNALt   CONNECTTIMEOUTt   TIMEOUTt
   HTTPHEADERR   t	   iteritemsR   t   CAINFOR   t   SSL_VERIFYHOSTR)   t   performR8   R+   R/   t   getvaluet   getinfot   RESPONSE_CODE(   R   R   R   R    t   st   curlt   kt   vR2   t   rbodyt   rcode(    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR!   ”   s4    0	c         C   s†   | d } | t  j t  j t  j g k r1 d } n' | t  j t  j g k rR d } n d } t j | ƒ d | d } t j	 | ƒ ‚ d  S(   Ni    s^   Test harness could not connect to Stripe. Please check your internet connection and try again.s(   Could not verify host's SSL certificate.t    s   

Network error: %si   (
   R   t   E_COULDNT_CONNECTt   E_COULDNT_RESOLVE_HOSTt   E_OPERATION_TIMEOUTEDt   E_SSL_CACERTt   E_SSL_PEER_CERTIFICATEt   textwrapt   fillR8   R9   (   R   R2   t
   error_codeR;   (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR/   ·   s    
				N(   R"   R#   R<   R%   R!   R/   (    (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR   ‘   s   #R   c           B   s;   e  Z e j d k r d Z n d Z d d „ Z d „  Z RS(   i   i    s   urllib.requestt   urllib2c   	         s&  t  j d k r0 t | t ƒ r0 | j d ƒ } n  t j | | |  j ƒ } ˆ  d k ri ‡  f d †  | _ n  y( t j	 | ƒ } | j
 ƒ  } | j } Wng t j k
 rÈ t j ƒ  } | j } | j
 ƒ  } n3 t j t f k
 rú t j ƒ  } |  j | ƒ n Xt  j d	 k r| j d ƒ } n  | | f S(
   Ni   i    s   utf-8R=   R>   c              s
   ˆ  j  ƒ  S(   N(   RF   (    (   R   (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyt   <lambda>Ô   s    (   i   i    (   s   gets   post(   i   i    (   R   R   R3   R7   t   encodet   urllib_requestt   RequestR   t
   get_methodt   urlopent   readt   codet	   HTTPErrorR   R+   t   URLErrort
   ValueErrorR/   R0   (	   R   R   R   R    t   reqt   responseRZ   R[   R2   (    (   R   s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR!   Í   s&    	c         C   s#   d t  | ƒ } t j | ƒ ‚ d  S(   Ns   Network error: %s(   R7   R8   R9   (   R   R2   R;   (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR/   å   s    (   i   i    N(   R"   R#   R   R   R<   R%   R!   R/   (    (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyR   Ç   s
   	(   i   i    (   i    i   i   ("   R   R   Rb   t	   lib.errorR8   t   lib.utilR   R   Re   Rh   t   urllib.requestR!   t   ImportErrorR   R%   R   t   __version__t   versiont   splitt   it   intt   majort   minort   patchR.   R   t   warnR   R   t   objectR   R
   R   R   (    (    (    s4   /Users/bryon/r-bryfry/level2/test/lib/http_client.pyt   <module>   sD   

	5				76