Created
February 19, 2018 04:14
-
-
Save kevinweber/b3ff455b2ab3e310a6042ea5b22f1356 to your computer and use it in GitHub Desktop.
Giphy API Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
checkStatus, | |
parseJSON | |
} from './request'; | |
/** | |
* Request data Giphy API | |
* https://developers.giphy.com/explorer/ | |
*/ | |
const endpoint = 'https://api.giphy.com/v1/gifs/search'; | |
export default function requestGif({ | |
apiKey = '', // <-- provide this! | |
lang = 'en', | |
limit = '1', | |
offset = '0', | |
rating = 'G', | |
term = 'shrug', | |
} = {}) { | |
const queryUri = `?api_key=${apiKey}&q=${term}&limit=${limit}&offset=${offset}&rating=${rating}&lang=${lang}`; | |
return fetch(endpoint + queryUri) | |
.then(checkStatus) | |
.then(parseJSON); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requestGif from './giphy'; | |
requestGif({ | |
apiKey: CONFIG.giphyApiKey, | |
term: CONFIG.giphyTerm, | |
}).then((response) => { | |
const newImage = document.createElement('img'); | |
newImage.src = response.data[0].images.fixed_width.url; | |
// Insert new image element into some DOM element with `data-image="giphy"` | |
const target = document.querySelector('[data-image="giphy"]'); | |
target.appendChild(newImage); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment