Picsum API Code
In this project, the aim was to create a simple API with the website Lorem Picsum. The original goal was to construct some javascript that allowed an email to be stored with the ability to save images associated with that email address that Picsum randomly generated on the page. When I began to implement this I felt it was a little too simple, so I wanted to add multiple users, each with their own emails and associated URLs. I did this by creating an empty array and populating it with a for loop.
let users = [];
for (var i=0; i < 5; i++) {
users[i] = {
/* white space used in case
an email contains the word
'email' followed by a number.*/
email: "email " + i,
url1: "url" +1,
url2: "url" +2,
url3: "url" +3,
};
};
Allowing 5 users to have 3 URLs each was just an arbitrary limitation so I had a set scope to work with. Next I set an array to populate with each email address so I can use it as a shorthand index.
let emailCheckArr = [];
for (var i=0; i < 5; i++) {
emailCheckArr.push(users[i].email);
};
This is just a simple for loop that takes any of the user emails in 'users' and fills this array up with them. Now I have the storage structure set up for the emails and URL's, I will create a simple Axios promise. This, through the picsum API, will randomly generate an image for the visitor to see.
const URL = 'https://picsum.photos/300/200';
const axiosImgGet = function() {
axios.get(URL)
.then((response) => {
let picID = response.headers['picsum-id'];
let picURL = `https://picsum.photos/id/${picID}/300/200`;
IMG.src = picURL;
});
};