Closed counselorbot[bot] closed 2 years ago
Go ahead and merge this branch to main
to move on. Great work finishing this section!
⚠️ If you receive a
Conflicts
error, simply pressResolve conflicts
and you should be good to merge!
Hop on over to the next issue.
Week 3 Step 5 ⬤⬤⬤⬤⬤◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes
Download it!
This week, you will be going through steps to create an HTTP Trigger that takes parameter inputs and returns a download link for the given image.
✅ Task:
bunnimage-download
that takes in "username" from the headerusername
from blob storage usingnode-fetch
downloadUri
.BUNNIMAGE_ENDPOINT2
bunnimage-download/index.js
on thebunnimage
branchbunnimage
ontomain
, and only merge the pull request when the bot approves your changes!🚧 Test Your Work
To test your work, use Postman to send a GET request with a "username" in the header. You should see a response similar to the below:
❓ How do I add "username" to the header?
Click on the headers tab below the request URL. ![image](https://user-images.githubusercontent.com/69332964/122677983-cd14ac00-d1b2-11eb-83d3-1d5c3d1283b5.png)1: Modifying your Function
In our
module.exports
function, we'll want to:Use the
node-fetch
package we installed earlier this week to determine if the requested image is available.Use if-else statements to determine the success of the retrieval. Remember to test for both jpeg and png extentions!
Send back a JSON object containing the download link.
❓ How do I use fetch to test extensions?
Outside of the main function, you'll first want to create a variable called `fetch` that calls your `node-fetch` package: `const fetch = require("node-fetch");`. Then, within your main function, you'll need to create references to your username as well as variables for `download`, `downloadpng`, and `downloadjpg`. Since we don't know whether the image is a png or jpeg file, we need to test for both. ```js const username = req.headers['username']; let download = "" let downloadpng = "https://❓ How do I determine the right image link?
Your data will contain an attribute "status text" that lets you know if a blob doesn't exist. To use these to our advantage, we can create if-else statements that notify us if our `fetch` method was successful. ```js if (pngdata.statusText == "The specified blob does not exist." && jpgdata.statusText == "The specified blob does not exist." ) { success = false; context.log("Does not exist: " + pngdata) context.log("Does not exist: " + jpgdata) } else if (pngdata.statusText != "The specified blob does not exist.") { success = true; download = downloadpng context.log("Does exist: " + pngdata) } else if (jpgdata.statusText != "The specified blob does not exist.") { success = true; download = downloadjpg context.log("Does exist: " + jpgdata) } ```❓ How do I return the download link?
To return the download link, just set `context.res` to a JSON object with your download link. ```js context.res = { body: { "downloadUri" : download, "success": success, } }; context.log(download); context.done(); ```📹 Walkthrough Video