Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
fix: send successful request to retrieve correct ap object
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankLi123 committed Oct 18, 2024
1 parent b1f3c6f commit 57102a3
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions kafka_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,35 @@
value_serializer=lambda v: json.dumps(v).encode('utf-8')
)

def process_inbox(data):
producer.send(KAFKA_TOPIC, data)
return jsonify({'status': 'success'}), 202

@app.route('/inbox', methods=['POST'])
def inbox():
data = request.get_json()
return process_inbox(data)

@app.route('/actor/inbox', methods=['POST'])
def actor_inbox():
data = request.get_json()
full_object = None
if data.get('type') == 'Announce':
object_url = data.get('object')
print("object_url:", object_url)
if object_url:
# Append /activity to the object_url to fetch the correct JSON
object_url_with_activity = f"{object_url}/activity"
try:
response = requests.get(object_url)
# Fetch the object data from the external URL
response = requests.get(object_url_with_activity)
if response.status_code == 200:
full_object = response.json()
data['full_object'] = full_object
except requests.RequestException:
# Handle any errors in fetching the full object
print(f"Failed to fetch object")
pass
else:
return jsonify({
'status': 'error',
'message': f'Failed to fetch object from {object_url_with_activity}, Status Code: {response.status_code}',
'url': object_url_with_activity
}), 400
except requests.RequestException as e:
return jsonify({'status': 'error', 'message': f'Error fetching the object: {str(e)}', 'url': object_url_with_activity}), 500

# Send the data to Kafka
producer.send(KAFKA_TOPIC, full_object)
return jsonify(full_object), 202


if __name__ == '__main__':
app.run(host='0.0.0.0', port=3001)
app.run(host='0.0.0.0', port=3001)

0 comments on commit 57102a3

Please sign in to comment.