dragonfly icon indicating copy to clipboard operation
dragonfly copied to clipboard

Adding the ability to specify headers for a URL based attachment

Open brendon opened this issue 7 years ago • 3 comments

Hi @markevans, I hope all is going well in the leadup to Christmas :)

I'm looking to download files from Google Drive via dragonfly. They'll be specified by the user using the Google Drive file picker tool that returns a file_id and oauth_token. I can send these back to the server and have Dragonfly download the file from there. The trouble is that to download the file, the oauth token needs to be specified in a header:

GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer <ACCESS_TOKEN>

See: https://developers.google.com/drive/api/v3/manage-downloads

I've had a look at the code and it appears specifying extra headers isn't currently supported. Would you be interested in having this as a feature? or do you think there's another way to do this?

I guess the biggest challenge would be working out a tidy way to pass in the extra headers. Perhaps passing a hash to the assignment method that contains say:

@photo.image_url = {
  url: 'https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media',
  headers: { 'Authorization': 'Bearer <ACCESS_TOKEN>' }
}

would work? I'd be keen to get onto this asap, so if you had any ideas or pointers before I begin that would be really helpful :)

brendon avatar Nov 30 '18 10:11 brendon

hi - I'd be reluctant to change anything with the model.image_url= method as this is just a convenience method that will get complicated if adding extra stuff. The place to do it would be in fetch_url. You could do

photo.image = Dragonfly.app.fetch_url("https://www.goo...", headers: {"Authorization"=> "Bearer <TOKEN>"})

The only thing missing that isn't currently implemented is the second arg with headers: {... in FetchUrl, but it would be fairly easy to add.

markevans avatar Nov 30 '18 11:11 markevans

Thanks @markevans, that makes a lot of sense. I've created a PR for this. Fairly simple. Let me know if there's anything to fix :)

brendon avatar Dec 02 '18 22:12 brendon

I've tested this approach in my app now and it works well.

brendon avatar Dec 03 '18 00:12 brendon