|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | +import { |
| 3 | + COLOR_OPTIONS, |
| 4 | + ORIENTATION_OPTIONS, |
| 5 | + SIZE_OPTIONS, |
| 6 | +} from "./common/constants.mjs"; |
| 7 | + |
1 | 8 | export default {
|
2 | 9 | type: "app",
|
3 | 10 | app: "pexels",
|
4 |
| - propDefinitions: {}, |
| 11 | + propDefinitions: { |
| 12 | + searchQuery: { |
| 13 | + type: "string", |
| 14 | + label: "Search Query", |
| 15 | + description: "The search query. **Ocean**, **Tigers**, **Pears**, etc.", |
| 16 | + }, |
| 17 | + orientation: { |
| 18 | + type: "string", |
| 19 | + label: "Orientation", |
| 20 | + description: "Desired photo orientation.", |
| 21 | + optional: true, |
| 22 | + options: ORIENTATION_OPTIONS, |
| 23 | + }, |
| 24 | + size: { |
| 25 | + type: "string", |
| 26 | + label: "Size", |
| 27 | + description: "Minimum photo size.", |
| 28 | + optional: true, |
| 29 | + options: SIZE_OPTIONS, |
| 30 | + }, |
| 31 | + color: { |
| 32 | + type: "string", |
| 33 | + label: "Color", |
| 34 | + description: "Desired photo color. You can set any color listed or any hexidecimal color code (eg. #ffffff)", |
| 35 | + optional: true, |
| 36 | + options: COLOR_OPTIONS, |
| 37 | + }, |
| 38 | + photoId: { |
| 39 | + type: "string", |
| 40 | + label: "Photo ID", |
| 41 | + description: "The ID of the photo to retrieve or download.", |
| 42 | + }, |
| 43 | + }, |
5 | 44 | methods: {
|
6 |
| - // this.$auth contains connected account data |
7 |
| - authKeys() { |
8 |
| - console.log(Object.keys(this.$auth)); |
| 45 | + _baseUrl() { |
| 46 | + return "https://api.pexels.com/v1"; |
| 47 | + }, |
| 48 | + _headers() { |
| 49 | + return { |
| 50 | + "Authorization": `${this.$auth.api_key}`, |
| 51 | + }; |
| 52 | + }, |
| 53 | + _makeRequest({ |
| 54 | + $ = this, path, ...opts |
| 55 | + }) { |
| 56 | + return axios($, { |
| 57 | + url: this._baseUrl() + path, |
| 58 | + headers: this._headers(), |
| 59 | + ...opts, |
| 60 | + }); |
| 61 | + }, |
| 62 | + searchPhotos(opts = {}) { |
| 63 | + return this._makeRequest({ |
| 64 | + path: "/search", |
| 65 | + ...opts, |
| 66 | + }); |
| 67 | + }, |
| 68 | + getPhoto({ |
| 69 | + photoId, ...opts |
| 70 | + } = {}) { |
| 71 | + return this._makeRequest({ |
| 72 | + path: `/photos/${photoId}`, |
| 73 | + ...opts, |
| 74 | + }); |
| 75 | + }, |
| 76 | + getCuratedPhotos(opts = {}) { |
| 77 | + return this._makeRequest({ |
| 78 | + path: "/curated", |
| 79 | + ...opts, |
| 80 | + }); |
| 81 | + }, |
| 82 | + async *paginate({ |
| 83 | + fn, params = {}, maxResults = null, ...opts |
| 84 | + }) { |
| 85 | + let hasMore = false; |
| 86 | + let count = 0; |
| 87 | + let page = 0; |
| 88 | + |
| 89 | + do { |
| 90 | + params.page = ++page; |
| 91 | + const { photos } = await fn({ |
| 92 | + params, |
| 93 | + ...opts, |
| 94 | + }); |
| 95 | + for (const d of photos) { |
| 96 | + yield d; |
| 97 | + |
| 98 | + if (maxResults && ++count === maxResults) { |
| 99 | + return count; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + hasMore = photos.length; |
| 104 | + |
| 105 | + } while (hasMore); |
9 | 106 | },
|
10 | 107 | },
|
11 | 108 | };
|
0 commit comments