from functools import wraps
import six
import requests
from dbt.exceptions import RegistryException
from dbt.utils import memoized
from dbt.logger import GLOBAL_LOGGER as logger
import os

if os.getenv('DBT_PACKAGE_HUB_URL'):
    DEFAULT_REGISTRY_BASE_URL = os.getenv('DBT_PACKAGE_HUB_URL')
else:
    DEFAULT_REGISTRY_BASE_URL = 'https://hub.getdbt.com/'


def _get_url(url, registry_base_url=None):
    if registry_base_url is None:
        registry_base_url = DEFAULT_REGISTRY_BASE_URL

    return '{}{}'.format(registry_base_url, url)


def _wrap_exceptions(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        try:
            return fn(*args, **kwargs)
        except requests.exceptions.ConnectionError as e:
            six.raise_from(
                RegistryException('Unable to connect to registry hub'), e)
    return wrapper


@_wrap_exceptions
def _get(path, registry_base_url=None):
    url = _get_url(path, registry_base_url)
    logger.debug('Making package registry request: GET {}'.format(url))
    resp = requests.get(url)
    logger.debug('Response from registry: GET {} {}'.format(url,
                                                            resp.status_code))
    resp.raise_for_status()
    return resp.json()


def index(registry_base_url=None):
    return _get('api/v1/index.json', registry_base_url)


index_cached = memoized(index)


def packages(registry_base_url=None):
    return _get('api/v1/packages.json', registry_base_url)


def package(name, registry_base_url=None):
    return _get('api/v1/{}.json'.format(name), registry_base_url)


def package_version(name, version, registry_base_url=None):
    return _get('api/v1/{}/{}.json'.format(name, version), registry_base_url)


def get_available_versions(name):
    response = package(name)
    return list(response['versions'])

@@ -180,16 +180,28 @@
 mport os
+%0Aimport time
 %0A%0Aif os.
@@ -620,16 +620,110 @@
 wargs):%0A
+        max_attempts = 5%0A        attempt = 0%0A        while True:%0A            attempt += 1%0A    
         
@@ -735,24 +735,28 @@
             
+   
+ 
 return fn(*a
@@ -770,16 +770,20 @@
 kwargs)%0A
+    
         
@@ -829,18 +829,130 @@
 ror as e
-:%0A
+xc:%0A                if attempt %3C max_attempts:%0A                    time.sleep(1)%0A                    continue%0A    
         
@@ -971,16 +971,20 @@
 e_from(%0A
+    
         
@@ -1046,18 +1046,57 @@
 y hub'),
+%0A                    exc%0A               
  
-e
 )%0A    re

add a retry + sleep loop to registry calls