def get_callback(request): url = request.url path = urlparse(url).path sobject_name, custom_id_field, record_id = parse_detail_url(path) try: if not custom_id_field: sobject = virtual_salesforce.get(sobject_name, record_id) else: sobject = virtual_salesforce.get_by_custom_id( sobject_name, record_id, custom_id_field) except (AssertionError, KeyError): return ( 404, {}, json.dumps([{ "errorCode": NOT_FOUND }]), ) return ( 200, {}, json.dumps({ "attributes": { "type": sobject_name, "url": path }, **sobject }), )
def test_parse_detail_url_with_upsert_key(): sobject, upsert_key, record_id = parse_detail_url( "/services/data/v42.0/sobjects/Contact/customExtIdField__c/123") assert sobject == "Contact" assert upsert_key == "customExtIdField__c" assert record_id == "123"
def delete_callback(request): url = request.url path = urlparse(url).path sobject, _, record_id = parse_detail_url(path) try: virtual_salesforce.delete(sobject, record_id) except AssertionError: return ( 404, {}, json.dumps([{ "errorCode": NOT_FOUND }]), ) return ( 204, {}, b"", )
def update_callback(request): url = request.url path = urlparse(url).path body = json.loads(request.body) sobject, upsert_key, record_id = parse_detail_url(path) if not upsert_key: try: virtual_salesforce.update(sobject, record_id, body, url) except AssertionError: return ( 404, {}, json.dumps([{ "errorCode": NOT_FOUND }]), ) else: salesforce_id, created = virtual_salesforce.upsert( sobject, record_id, body, upsert_key=upsert_key) return ( 204, {}, json.dumps({ "id": salesforce_id, "success": True, "errors": [], "created": created }), ) return ( 204, {}, b"", )
def test_parse_detail_url(): sobject, _, record_id = parse_detail_url( "/services/data/v42.0/sobjects/Contact/123") assert sobject == "Contact" assert record_id == "123"