cat /dev/brain

github3.py (update)

I previously mentioned my work on github3.py and how I was having trouble creating downloads on GitHub because they use Amazon's S3 service for the uploaded files. What this means is that first you have to "create" the download on GitHub then you have to upload the actual file to S3.

The GitHub documentation for this section uses curl to illustrate this but I seem to have completely misunderstood the very last (and most crucial) part where they say:

-F "file=@new_file.jpg"

For some reason, it completely went over my head that the form field name was "file", and not the name of the file. After starting to modify the behavior of requests, I realized what I was doing wrong. (Until I finish modifying requests as described in that issue, I have to create my own multipart/form-data response like so

boundary = '--GitHubBoundary'
content_disposition = 'Content-Disposition: form-data; name="{0}"'
data = [boundary]

for (k, v) in form:
    data.extend([content_disposition.format(k), '', v, boundary])

data.append(content_disposition.format('file') +
        '; filename="{0}"'.format(json.get('name')))
data.extend(['', open(path, 'rb').read(), boundary + '--', ''])
form = '\r\n'.join(data)

Prior to using "file" as the field name, I was using the filename as the field name and that was what caused all of my problems. Notice, also, that once my changes are merged in to requests, all of that changes to

file = [('file', open(path, 'rb').read())]
resp = self._post(json.get('s3_url'), data=form, files=file, auth=tuple())

Since this is now completed, everything documented is covered by github3.py. Sure everything isn't covered by tests, but I'm working on that.