blaugold / unsplash_client

An unofficial, platform independent, client for the Unsplash API. Unsplash provides royalty-free images.
https://pub.dev/packages/unsplash_client
BSD 3-Clause "New" or "Revised" License
24 stars 7 forks source link

Add `Photos.downloadImage` method to download image of photo #11

Open blaugold opened 3 years ago

blaugold commented 3 years ago

This method should download the image of the photo as well as hit the Unsplash API endpoint to register the download.

edgarjc commented 1 year ago

Currently working on registering the download since the current method doesn't work

blaugold commented 1 year ago

@edgarjc Could you share a bit more about what does not work? Do you get an error?

edgarjc commented 1 year ago

The download method isn't actually "downloading" the image from unsplash. It seems like the get request isn't being sent correctly. After some digging, it looks like it takes in an id which according to their API isn't necessarily correct since each image has its own unique download token under download_location. I passed that dowload_location into the function It's still not tracking downloads.

blaugold commented 1 year ago

Hm. The Photos.download method should simply implement the GET /photos/:id/download endpoint, and I don't see what could be wrong in the implementation. photo.links.download_location just seems to be a prebuilt URL to this endpoint for a particular photo.

If you are not immediately seeing the download counter of a photo increment, it could be that unsplash does some caching, and it takes a bit of time to reflect the download.

edgarjc commented 1 year ago

So after a lot of troubleshooting, I think that the Request isn't correctly sending the Unsplash client credentials but I might be wrong about that. In the meantime the code below is what correctly implements the GET request in my project and it shows the download on the Unsplash dashboard.

final response = await http.get(
      Uri.parse("${downloadLink}&client_id=${unsplashAccessKey}"),
    );

    return response;
blaugold commented 1 year ago

Thanks for the update! I've looked at the official unsplash client for JS and there the client id is sent in the Authorization header just like this client does. There is no special handling of the URL for the download endpoint where the client_id is added to it. My guess is that the server does not implement the endpoint quite right and only uses the client id when sent in the URL. But I think the Photos.download should require that the user passes in the download_location URL, since that is how the JS client does, probably to make sure the other parameters in download_location are provided.