API interaction scripts in Bash Scripting - Time & Space Complexity
When writing scripts that talk to APIs, it is important to know how the time to finish grows as we ask for more data or make more requests.
We want to understand how the script's work changes when the number of API calls or data size changes.
Analyze the time complexity of the following code snippet.
#!/bin/bash
urls=("https://api.example.com/data1" "https://api.example.com/data2" "https://api.example.com/data3")
for url in "${urls[@]}"; do
response=$(curl -s "$url")
echo "$response" | jq '.items[]'
done
This script loops over a list of API URLs, fetches data from each using curl, and processes the returned JSON items.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over the list of URLs and making an API call for each.
- How many times: Once per URL in the list.
As the number of URLs increases, the script makes more API calls, so the total work grows directly with the number of URLs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 API calls and processing |
| 100 | 100 API calls and processing |
| 1000 | 1000 API calls and processing |
Pattern observation: The work grows in a straight line as the number of URLs grows.
Time Complexity: O(n)
This means the time to finish grows directly in proportion to the number of API requests made.
[X] Wrong: "The script runs in constant time because each API call is independent."
[OK] Correct: Even though calls are independent, the total time adds up with each call, so more URLs mean more total time.
Understanding how your script's time grows with input size shows you can write efficient automation and predict performance, a valuable skill in real projects.
"What if we changed the script to fetch data from each URL in parallel? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of each tool
curlis used to send HTTP requests, whilegrep,awk, andsedare text processing tools.Step 2: Identify the tool for API interaction
Since APIs communicate over HTTP,curlis the correct tool to send requests and receive responses.Final Answer:
curl-> Option DQuick Check:
API calls usecurl[OK]
curl sends web requests in bash [OK]- Confusing text tools like grep with API tools
- Using sed or awk for HTTP requests
- Thinking bash has a built-in HTTP client
https://api.example.com/data using curl?Solution
Step 1: Understand default
By default,curlbehaviorcurlsends a GET request if no method is specified.Step 2: Analyze options
curl https://api.example.com/data sends a GET request simply by specifying the URL. The other options either specify POST or data, which changes the method.Final Answer:
curl https://api.example.com/data -> Option AQuick Check:
Defaultcurl= GET request [OK]
- Using -X POST when GET is needed
- Adding -d or --data without POST method
- Confusing GET and POST syntax
response=$(curl -s -H "Accept: application/json" https://api.example.com/status) echo "$response"
Solution
Step 1: Understand the command
Thecurlcommand uses-sfor silent mode and sets the header to accept JSON. It fetches data from the API URL.Step 2: Analyze the output
The response variable stores the full API response body, which is expected to be JSON. Theechoprints this JSON string.Final Answer:
The raw JSON response from the API -> Option BQuick Check:
Silent curl with JSON header returns JSON [OK]
- Expecting HTTP status code instead of body
- Thinking -H sets request method
- Assuming syntax error due to quotes
curl -X POST https://api.example.com/update -d '{"name":"John"}' -H 'Content-Type: application/json'Solution
Step 1: Check JSON data quoting
The JSON string uses double quotes inside single quotes, but the inner double quotes are escaped with backslashes, which is unnecessary and can cause errors in bash.Step 2: Correct quoting for JSON in bash
Use single quotes around JSON without escaping double quotes, or use double quotes outside and escape inner quotes properly.Final Answer:
The JSON data is not properly escaped -> Option AQuick Check:
Proper JSON quoting avoids escape errors [OK]
- Over-escaping quotes inside JSON
- Wrong Content-Type header
- Omitting -X POST when sending data
Solution
Step 1: Understand how to capture HTTP status code
The-w "%{http_code}"option tellscurlto output the HTTP status code after the response. The-o /dev/nulldiscards the response body, and-ssilences progress.Step 2: Check command correctness
status=$(curl -X POST -d '{"key":"value"}' -H 'Content-Type: application/json' https://api.example.com/api -w "%{http_code}" -o /dev/null -s) correctly uses all these options and assigns the status code to the variable. Other options either capture the response body or include headers, not just the status code.Final Answer:
status=$(curl -X POST -d '{"key":"value"}' -H 'Content-Type: application/json' https://api.example.com/api -w "%{http_code}" -o /dev/null -s) -> Option CQuick Check:
Use-wwith-o /dev/nullto get status code [OK]
- Capturing full response instead of status code
- Missing -w option for status code
- Not silencing output with -s
Ready to prove it?
Pass the test and this pattern moves unknown
