bcb-0028
1.000
Challenge · difficulty 2/5
# BigCodeBench/28
Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; define `task_func` at module level.
Allowed libraries: `base64`, `requests`, `json`.
```python
import requests
import json
import base64
def task_func(data, url="http://your-api-url.com"):
"""
Convert a Python dictionary into a JSON-formatted string, encode this string in base64 format,
and send it as a 'payload' in a POST request to an API endpoint.
Parameters:
data (dict): The Python dictionary to encode and send.
url (str, optional): The API endpoint URL. Defaults to "http://your-api-url.com".
Returns:
requests.Response: The response object received from the API endpoint after the POST request.
Requirements:
- requests
- json
- base64
Example:
>>> data = {'name': 'John', 'age': 30, 'city': 'New York'}
>>> response = task_func(data, url="http://example-api-url.com")
>>> print(response.status_code)
200
"""
```
<!-- imported from BigCodeBench (BigCodeBench/28) -->
tests/test_bcb_0028.py
# Auto-generated from BigCodeBench BigCodeBench/28. Do not edit by hand.
import pathlib as _pathlib
exec(_pathlib.Path(__file__).with_name("solution.py").read_text(), globals())
import unittest
from unittest.mock import patch, Mock
import requests
import json
# Mocking the requests.post method
def mock_post(*args, **kwargs):
mock_response = Mock()
mock_response.status_code = 200
mock_response.text = "OK"
return mock_response
class TestCases(unittest.TestCase):
@patch('requests.post', side_effect=mock_post)
def test_case_1(self, mock_post_method):
data = {'name': 'John', 'age': 30, 'city': 'New York'}
response = task_func(data, url="http://mock-api-url.com")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, "OK")
@patch('requests.post', side_effect=mock_post)
def test_case_2(self, mock_post_method):
data = {'task': 'Write code', 'status': 'completed'}
response = task_func(data, url="http://mock-api-url.com")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, "OK")
@patch('requests.post', side_effect=mock_post)
def test_case_3(self, mock_post_method):
data = {}
response = task_func(data, url="http://mock-api-url.com")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, "OK")
@patch('requests.post', side_effect=mock_post)
def test_case_4(self, mock_post_method):
data = {'fruit': 'apple', 'color': 'red', 'taste': 'sweet'}
response = task_func(data, url="http://mock-api-url.com")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, "OK")
@patch('requests.post', side_effect=mock_post)
def test_case_5(self, mock_post_method):
data = {'country': 'USA', 'capital': 'Washington, D.C.'}
response = task_func(data, url="http://mock-api-url.com")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, "OK")
@patch('requests.post', side_effect=mock_post)
def test_case_6(self, mock_post_method):
# Test to verify that the POST request is made with the correct parameters
data = {'name': 'John', 'age': 30, 'city': 'New York'}
json_data = json.dumps(data)
encoded_data = base64.b64encode(json_data.encode('ascii')).decode('ascii')
task_func(data, url="http://mock-api-url.com")
try:
mock_post_method.assert_called_once_with("http://mock-api-url.com", data={"payload": encoded_data})
except:
mock_post_method.assert_called_once_with("http://mock-api-url.com", json={"payload": encoded_data})
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```python
import requests
import json
import base64
def task_func(data, url="http://your-api-url.com"):
"""
Convert a Python dictionary into a JSON-formatted string, encode this string in base64 format,
and send it as a 'payload' in a POST request to an API endpoint.
Parameters:
data (dict): The Python dictionary to encode and send.
url (str, optional): The API endpoint URL. Defaults to "http://your-api-url.com".
Returns:
requests.Response: The response object received from the API endpoint after the POST request.
Requirements:
- requests
- json
- base64
Example:
>>> data = {'name': 'John', 'age': 30, 'city': 'New York'}
>>> response = task_func(data, url="http://example-api-url.com")
>>> print(response.status_code)
200
"""
# Convert the dictionary to a JSON-formatted string
json_string = json.dumps(data)
# Encode the JSON string in base64 format
encoded_payload = base64.b64encode(json_string.encode('utf-8')).decode('utf-8')
# Send the base64 encoded string as a 'payload' in a POST request
response = requests.post(url, json={"payload": encoded_payload})
return response
```Test output (stdout)
...... [100%] 6 passed in 0.02s