import os
import shutil

import requests


def get_cat(folder, name):
    url = "http://consuming-python-services-api.azurewebsites.net/cats/random"
    data = get_data_from_url(url)
    save_image(folder, name, data)


def get_data_from_url(url):
    response = requests.get(url, stream=True)
    return response.raw


def save_image(folder, name, data):
    file_name = os.path.join(folder, name + '.jpg')
    with open(file_name, 'wb') as fout:
        shutil.copyfileobj(data, fout)

[((14, 15, 14, 45), 'requests.get', 'requests.get', (), '', False, 'import requests\n'), ((19, 16, 19, 51), 'os.path.join', 'os.path.join', ({(19, 29, 19, 35): 'folder', (19, 37, 19, 50): "name + '.jpg'"}, {}), "(folder, name + '.jpg')", False, 'import os\n'), ((21, 8, 21, 38), 'shutil.copyfileobj', 'shutil.copyfileobj', ({(21, 27, 21, 31): 'data', (21, 33, 21, 37): 'fout'}, {}), '(data, fout)', False, 'import shutil\n')]