<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Can i send multiple post requests to an API endpoint and get the info if all succeeded ? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/can-i-send-multiple-post-requests-to-an-api-endpoint-and-get-the/m-p/100459#M40308</link>
    <description>&lt;P&gt;Hello I am trying to send multiple post requests to an endpoint, i have a spark dataframe and each column of this dataframe is sent through the payload of the post request.&lt;/P&gt;&lt;P&gt;However when i run this in my notebook, no exception is raised. I'm guessing it is because the requests are executed on spark workers&lt;/P&gt;&lt;P&gt;That's why i wonder how can i get the info if a post request returned a HTTP error 422 or 502 ?&lt;/P&gt;&lt;P&gt;Here's something i tried but with no result&lt;/P&gt;&lt;LI-CODE lang="python"&gt;url = "string"
headers = {
    'header': 'XXXXX'
}

def process_partition(partition):
    partition_errors = [] 
    
    for row in partition:
        payload = {
            "col1": str(row["col1"]),
        }
        try:
            response = requests.post(url, json=payload, headers=headers)
            response.raise_for_status()
        except requests.exceptions.HTTPError as err:
            partition_errors.append({"error": str(err), "data": row.asDict()})
        except Exception as e:
            partition_errors.append({"error": str(e), "data": row.asDict()})
    
    return partition_errors

def log_errors(errors):
    if errors:
        for error in errors:
            print(f"Error: {error['error']}, Data: {error['data']}")
    else:
        print("All data has been sent.")

all_errors = []

def collect_partition_errors(partition):
    errors = process_partition(partition)
    all_errors.extend(errors) 

df_websocket_data.foreachPartition(collect_partition_errors)

log_errors(all_errors)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 29 Nov 2024 18:48:03 GMT</pubDate>
    <dc:creator>Layer</dc:creator>
    <dc:date>2024-11-29T18:48:03Z</dc:date>
    <item>
      <title>Can i send multiple post requests to an API endpoint and get the info if all succeeded ?</title>
      <link>https://community.databricks.com/t5/data-engineering/can-i-send-multiple-post-requests-to-an-api-endpoint-and-get-the/m-p/100459#M40308</link>
      <description>&lt;P&gt;Hello I am trying to send multiple post requests to an endpoint, i have a spark dataframe and each column of this dataframe is sent through the payload of the post request.&lt;/P&gt;&lt;P&gt;However when i run this in my notebook, no exception is raised. I'm guessing it is because the requests are executed on spark workers&lt;/P&gt;&lt;P&gt;That's why i wonder how can i get the info if a post request returned a HTTP error 422 or 502 ?&lt;/P&gt;&lt;P&gt;Here's something i tried but with no result&lt;/P&gt;&lt;LI-CODE lang="python"&gt;url = "string"
headers = {
    'header': 'XXXXX'
}

def process_partition(partition):
    partition_errors = [] 
    
    for row in partition:
        payload = {
            "col1": str(row["col1"]),
        }
        try:
            response = requests.post(url, json=payload, headers=headers)
            response.raise_for_status()
        except requests.exceptions.HTTPError as err:
            partition_errors.append({"error": str(err), "data": row.asDict()})
        except Exception as e:
            partition_errors.append({"error": str(e), "data": row.asDict()})
    
    return partition_errors

def log_errors(errors):
    if errors:
        for error in errors:
            print(f"Error: {error['error']}, Data: {error['data']}")
    else:
        print("All data has been sent.")

all_errors = []

def collect_partition_errors(partition):
    errors = process_partition(partition)
    all_errors.extend(errors) 

df_websocket_data.foreachPartition(collect_partition_errors)

log_errors(all_errors)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2024 18:48:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/can-i-send-multiple-post-requests-to-an-api-endpoint-and-get-the/m-p/100459#M40308</guid>
      <dc:creator>Layer</dc:creator>
      <dc:date>2024-11-29T18:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Can i send multiple post requests to an API endpoint and get the info if all succeeded ?</title>
      <link>https://community.databricks.com/t5/data-engineering/can-i-send-multiple-post-requests-to-an-api-endpoint-and-get-the/m-p/100851#M40441</link>
      <description>&lt;P&gt;The return type for foreachPartition is None, so this is expected. If you're looking to do arbitrary code execution and return a result, &lt;A href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.mapInPandas.html" target="_self"&gt;mapInPandas&lt;/A&gt; or&amp;nbsp;&lt;A href="https://spark.apache.org/docs/3.4.2/api/python/reference/pyspark.sql/api/pyspark.sql.functions.pandas_udf.html" target="_self"&gt;Pandas UDFs&lt;/A&gt; are good choices - you'd want to combine those with something like a .&lt;A href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.toLocalIterator.html" target="_self"&gt;toLocalIterator&lt;/A&gt; call to interact with results in Python&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 06:46:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/can-i-send-multiple-post-requests-to-an-api-endpoint-and-get-the/m-p/100851#M40441</guid>
      <dc:creator>cgrant</dc:creator>
      <dc:date>2024-12-04T06:46:59Z</dc:date>
    </item>
  </channel>
</rss>

