title: Downloading csv data from an API
body: <p>I am attempting to download csv data from an API which I will then edit I am struggling to get the different functions to work together.</p>
  
  <p>i.e. passing the export link through to download the file and then through to opening it.</p>
  
  <pre><code>    '''
  File name: downloadAWR.py
  Author: Harry&amp;Joe
  Date created: 3/10/17
  Date last modified: 5/10/17
  Version: 3.6
  '''
  
  import requests
  import json
  import urllib2
  import zipfile
  import io
  import csv
  import os
  from urllib2 import urlopen, URLError, HTTPError
  </code></pre>
  
  <p>geturl() is used to create a download link for the  csv data, one link will be created with user input data in this case the name and dates, this will then create a link that we can use to download the data. the link is stored in export_link</p>
  
  <pre><code>def geturl():
  
  #getProjectName
  project_name = 'BIMM'
  
  #getApiToken
  api_token = "API KEY HERE"
  
  #getStartDate
  start_date = '2017-01-01'
  
  #getStopDate
  stop_date = '2017-09-01'
  
  url = "https://api.awrcloud.com/get.php?action=export_ranking&amp;project=%s&amp;token=%s&amp;startDate=%s&amp;stopDate=%s" % (project_name,api_token,start_date,stop_date)
  
  export_link = requests.get(url).content
  
  return export_link
  </code></pre>
  
  <p>dlfile is used to actually use the link a get a file we can manipulate and edit e.g. removing columns and some of the data.</p>
  
  <pre><code>def dlfile(export_link):
  # Open the url
  try:
      f = urlopen(export_link)
      print ("downloading " + export_link)
  
      # Open our local file for writing
      with open(os.path.basename(export_link), "wb") as local_file:
          local_file.write(f.read())
  
  #handle errors
  except HTTPError as e:
      print ("HTTP Error:", e.code, export_link)
  except URLError as e:
      print ("URL Error:", e.reason, export_link)
  
  return f
  </code></pre>
  
  <p>readdata is used to go into the file and open it for us to use.</p>
  
  <pre><code>def readdata():
      with zipfile.ZipFile(io.BytesIO(zipdata)) as z:
          for f in z.filelist:
              csvdata = z.read(f)
  
  #reader = csv.reader(io.StringIO(csvdata.decode()))
  
  
  
  def main():
  #Do something with the csv data
  
      export_link = (geturl())
  
  
      data = dlfile(export_link)
  
      csvdata = data.readdata()
  
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Generally I'm finding that the code works independently but struggles when I try to put it all together synchronously. </p>