Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #140 from unsplash/maintenance/rename-download-photo
Browse files Browse the repository at this point in the history
Rename downloadPhoto to trackDownload
  • Loading branch information
lukechesser authored Sep 3, 2020
2 parents 35a3006 + 9fb3fb5 commit 01c8942
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 6.3.0

### Changes

- Deprecate `photos.photoDownload` in favor of `photos.trackDownload` to better clarify method usage. `photoDownload` will continue to be supported until version 7.0.

## 6.2.0

### Changes
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Quick links to methods you're likely to care about:

- [Get a list of new photos](#photos-all) 🎉
- [Get a random photo](#photo-random) 🎑
- [Trigger a photo download](#photo-download) 📡
- [Trigger a photo download](#track-download) 📡
- [Search for a photo by keyword](#search-photos) 🕵️‍♂️

**Note:** Every application must abide by the [API Guidelines](https://help.unsplash.com/api-guidelines/unsplash-api-guidelines). Specifically, remember to [hotlink images](https://help.unsplash.com/api-guidelines/more-on-each-guideline/guideline-hotlinking-images), [attribute photographers](https://help.unsplash.com/api-guidelines/more-on-each-guideline/guideline-attribution), and [trigger a download when appropriate](https://help.unsplash.com/api-guidelines/more-on-each-guideline/guideline-triggering-a-download).
Expand Down Expand Up @@ -310,9 +310,9 @@ unsplash.photos.unlikePhoto("mtNweauBsMQ")
```
---

<div id="photo-download" />
<div id="track-download" />

### photos.downloadPhoto(photo)
### photos.trackDownload(photo)
Trigger a download of a photo as per the [download tracking requirement of API Guidelines](https://medium.com/unsplash/unsplash-api-guidelines-triggering-a-download-c39b24e99e02). [See endpoint docs 🚀](https://unsplash.com/documentation#track-a-photo-download)

*Note*: this accepts a photo JSON object, not a URL string or photo ID. See the example below for how to pair it with other calls to trigger it.
Expand All @@ -328,14 +328,14 @@ __Example__
unsplash.photos.getPhoto("mtNweauBsMQ")
.then(toJson)
.then(json => {
unsplash.photos.downloadPhoto(json);
unsplash.photos.trackDownload(json);
});

// or if working with an array of photos
unsplash.search.photos("dogs", 1)
.then(toJson)
.then(json => {
unsplash.photos.downloadPhoto(json["results"][0]);
unsplash.photos.trackDownload(json["results"][0]);
});
```

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unsplash-js",
"version": "6.2.0",
"version": "6.3.0",
"description": "A JavaScript wrapper for the Unsplash API",
"main": "lib/unsplash.js",
"scripts": {
Expand Down
31 changes: 18 additions & 13 deletions src/methods/photos.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,25 @@ export default function photos() {
});
},

downloadPhoto: (photo) => {
const downloadLocation = get(photo, "links.download_location", undefined);
// Deprecated in 6.2
downloadPhoto: track.bind(this),

if (downloadLocation === undefined) {
throw new Error(`Object received is not a photo. ${photo}`);
}
trackDownload: track.bind(this)
};
}

const urlComponents = getUrlComponents(downloadLocation);
function track(photo) {
const downloadLocation = get(photo, "links.download_location", undefined);

return this.request({
url: urlComponents.pathname,
method: "GET",
query: urlComponents.query
});
}
};
if (downloadLocation === undefined) {
throw new Error(`Object received is not a photo. ${photo}`);
}

const urlComponents = getUrlComponents(downloadLocation);

return this.request({
url: urlComponents.pathname,
method: "GET",
query: urlComponents.query
});
}
29 changes: 29 additions & 0 deletions test/unsplash-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,35 @@ describe("Unsplash", () => {
expect(() => unsplash.photos.downloadPhoto(mockPhotoResponse)).toThrow(/Object received is not a photo/);
});
});

describe("trackDownload", () => {
it("should make a GET request to the photo's download_location", () => {
let spy = spyOn(unsplash, "request");
const mockPhotoResponse = {
"id": "123123",
"links": {
"download_location": "https://api.unsplash.com/photos/123123/download?ixid=drake"
}
};

unsplash.photos.trackDownload(mockPhotoResponse);

expect(spy.calls.length).toEqual(1);
expect(spy.calls[0].arguments).toEqual([{
method: "GET",
url: "/photos/123123/download",
query: {
ixid: "drake"
}
}]);
});

it("should throw an error if passed a malformed photo object", () => {
const mockPhotoResponse = "123123";

expect(() => unsplash.photos.trackDownload(mockPhotoResponse)).toThrow(/Object received is not a photo/);
});
});
});

describe("collections", () => {
Expand Down

0 comments on commit 01c8942

Please sign in to comment.