为什么我的API响应总是无效的?我的call_api函数有问题吗?[已解决]

huangapple go评论113阅读模式
英文:

Why are my API response always invalid ? Is there something wrong with my call_api function?[Solved]

问题

在我的call_api函数中,我使用了一种不同的方法来检测无效/有效的API,即通过检查响应头"Response_Description"中是否存在字符串"Hazard was successfully processed and at least one value is returned."。不知何故,我的输出始终为false,标记为输出表中的Pass列中的N。

def call_api(url):
    try:
        headers = {
            "User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"
        }
        response = requests.get(url, headers=headers)
        json_response = response.json()
        if (
            "Response_Description" in json_response
            and "Hazard was successfully processed" in json_response["Response_Description"]
        ):
            return True
        else:
            return False
    except requests.exceptions.RequestException:
        return False

# 其余的代码部分保持不变

如果你希望在修改代码后正确验证API,请确保API的响应中包含所需的字符串 "Hazard was successfully processed",并且这个字符串在 "Response_Description" 中出现。如果API响应的格式或内容发生变化,你可能需要相应地调整代码以匹配新的响应格式。

英文:

In my call_api function, I used a different way to detect invalid/valid APIs which was by checking if the string, "Hazard was successfully processed and at least one value is returned." was present in the header "Response_Description". Somehow my output is always false which is labelled N in the output table under the Pass column.

def call_api(url):
try:
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"
    }
    response = requests.get(url, headers=headers)
    json_response = response.json()
    if (
        "Response_Description" in json_response
        and "Hazard was successfully processed" in json_response["Response_Description"]
    ):
        return True
    else:
        return False
except requests.exceptions.RequestException:
    return False

df = pd.read_csv(r"C:\Users\Jose.Moquaimbo\Bulk Calling APIs\dataset.csv")

# Number of iterations
num_iterations = 5

# Create an empty DataFrame to store the results
results_df = pd.DataFrame(columns=["Iteration", "Pass", "Time Taken"])

# Variables for tracking min, max, and total time
min_time = float("inf")
max_time = float("-inf")
total_time = 0


def process_iteration(iteration):
# Get a random sample of URLs from the DataFrame
 random_urls = df["url"].sample(n=1).tolist()

# Execute API calls concurrently using ThreadPoolExecutor
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    start_time = time.time()
    futures = [executor.submit(call_api, url) for url in random_urls]

# Wait for all futures to complete and get their results
results = [future.result() for future in futures]

# Stop timer
end_time = time.time()

# Calculate the time taken for this iteration
iteration_time = end_time - start_time

# Update min, max, and total time
global min_time, max_time, total_time
min_time = min(min_time, iteration_time)
max_time = max(max_time, iteration_time)
total_time += iteration_time

# Check if any API call was not successful in this iteration
passed = "Y" if all(results) else "N"

# Add the iteration results to the DataFrame
results_df.loc[iteration] = [iteration, passed, iteration_time]


# Run the iterations
for i in range(1, num_iterations + 1):
    process_iteration(i)

# Calculate average time per iteration
avg_time = total_time / num_iterations

# Display the results DataFrame
print(results_df)

# Summary statistics
print("Minimum time taken:", min_time)
print("Maximum time taken:", max_time)
print("Average time per iteration:", avg_time)
print("Y stands for error-free response and N for invalid response")

Output

       Iteration Pass  Time Taken
 1          1    N    0.276398
 2          2    N    0.298180
 3          3    N    0.307337
 4          4    N    0.323730
 5          5    N    0.333215
 Minimum time taken: 0.2763981819152832
 Maximum time taken: 0.33321452140808105
 Average time per iteration: 0.3077719688415527
 Y stands for error-free response and N for invalid response

Is there a way to modify the code, so it can correctly validate the API because it seems that the API is always invalidated. I suspect something is wrong my call_api function

Edit:

response_text = response.text

答案1

得分: 1

重构你的call_api()函数,以更好地处理异常并报告可能出现的问题。

    import requests
    import sys
    
    HEADERS = {
        'User-Agent': 'Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36'
    }
    KEY = 'Response_Description'
    VALUE = 'Hazard was successfully processed'
    
    def call_api(url: str) -> bool:
        try:
            with requests.get(url, headers=HEADERS) as response:
                response.raise_for_status()
                return VALUE in response.json().get(KEY, '')
        except Exception as e:
            print(e, file=sys.stderr)
        return False
英文:

Restructure your call_api() function to better handle exceptions and report any problems that might arise.

import requests
import sys

HEADERS = {
    'User-Agent': 'Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36'
}
KEY = 'Response_Description'
VALUE = 'Hazard was successfully processed'

def call_api(url: str) -> bool:
    try:
        with requests.get(url, headers=HEADERS) as response:
            response.raise_for_status()
            return VALUE in response.json().get(KEY, '')
    except Exception as e:
        print(e, file=sys.stderr)
    return False

huangapple
  • 本文由 发表于 2023年6月15日 14:51:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定