At Canva, we have thousands of designs created each day. We want to write a program that…
- User
At Canva, we have thousands of designs created each day. We want to write a program that can fetch public designs from our backends to do some processing on them.
The backend API looks like:
GET /design/ Starting simple, let's write a solution that fetches 10 designs from this backend API (say, designs with IDs between 1 and 10). The program should write Done to the console once all the designs have been fetched.
- Assistant
In this example, I'll demonstrate how to fetch 10 designs using Python and the requests library. If you haven't installed the requests library, you can do so by running:
codepip install requestsHere's a simple program to fetch designs with IDs between 1 and 10:
pythonimport requests def fetch_design(design_id): url = f"https://api.canva.com/design/{design_id}" response = requests.get(url) if response.status_code == 200: return response.json() else: print(f"Failed to fetch design with ID {design_id}") return None def main(): designs = [] for design_id in range(1, 11): design = fetch_design(design_id) if design is not None: designs.append(design) print(f"Fetched {len(designs)} designs.") print("Done") if __name__ == "__main__": main() - User
Thanks for that - can you do this in JavaScript instead please?
- Assistant
Certainly! Here's a JavaScript example using Node.js and the
node-fetchlibrary. If you haven't installed thenode-fetchlibrary, you can do so by running:sqlnpm install node-fetchCreate a file called
fetchDesigns.jsand add the following code:javascriptconst fetch = require("node-fetch"); async function fetchDesign(designId) { const url = `https://api.canva.com/design/${designId}`; const response = await fetch(url); if (response.ok) { return await response.json(); } else { console.log(`Failed to fetch design with ID ${designId}`); return null; } } async function main() { const designs = []; for (let designId = 1; designId <= 10; designId++) { const design = await fetchDesign(designId); if (design !== null) { designs.push(design); } } console.log(`Fetched ${designs.length} designs.`); console.log("Done"); } main(); - User
Now that we are able to fetch the designs from the backend API, we want to perform some analytics on each of the designs fetched.
Requests to the backend API return a document in the following structure:
{ designId: 1, shapes: [ { shapeId: 'basic-square', color: { r: 255, g: 255, b: 255 }}, { shapeId: 'basic-circle', color: { r: 255, g: 255, b: 255 }}, { shapeId: 'basic-diamond', color: { r: 255, g: 0, b: 0 }}, { shapeId: 'basic-rectangle', color: { r: 0, g: 255, b: 0 }} ] }
Given this document structure, we want to find the average colour of all the shapes within each of the designs and log the results to the console
- Assistant
To find the average color of all shapes within each design, you can modify the JavaScript code as follows:
javascript - User
Are there any performance implications with this approach and do you have any ideas on how you could approve the async work?
- Assistant
Yes, there are some performance implications with the current approach. The program fetches each design sequentially, which may result in slower execution times, especially when dealing with a large number of designs. To improve the performance, you can use
Promise.allto fetch all designs concurrently.Here's the modified version of the code that uses
Promise.allfor better performance:javascript
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.