Extending Content Delivery API classes

Vote:
 

Hi, I am building a headless commerce solution using content delivery API.

Is it possible to extend the Content delivery API commerce classes adding my own attributes and fields to it, taking Cart object returned by 

https://localhost:8080/api/episerver/v3.0/me/carts/3 for example 

the attributes iam trying to add could be added to the cart , line items , shipments or basically any content type retuned by content delivery API 

this is a Json Example which I am trying to extend :

{
"id": 3,
    "name": "test2",
    "customerId": "5d437ccc-91b3-447b-8694-81b13f657dc3",
    "market": "CAN",
    "currency": "CAD",
    "lastUpdated": "2023-11-26T08:54:28.987+00:00",
    "shipments": [
        {
            "id": -1,
            "shippingAddress": {
                "firstName": "test1",
                "lastName": "test2",
                "line1": "test",
                "line2": "test",
                "city": "test",
                "countryName": "test",
                "countryCode": "test",
                "postalCode": "some postal code",
                "regionName": "test",
                "email": "testUser1@test.com",
                "phoneNumber": "01123456789",
                "name": "72a21f34-cf0d-4358-9aee-164721c559a6"
            },
            "shippingMethodId": "00000000-0000-0000-0000-000000000000",
            "lineItems": [
                {
                    "id": -4,
                    "contentId": "6d16a343-dda5-4914-bbc5-fccb38f31be0",
                    "code": "some product",
                    "placedPrice": 0.0,
                    "quantity": 1.000000000,
                    "displayName": "some product",
                    "isGift": false
                },
                {
                    "id": -5,
                    "contentId": "6d16a343-dda5-4914-bbc5-fccb38f31be0",
                    "code": "some product",
                    "placedPrice": 0.0,
                    "quantity": 1.000000000,
                    "displayName": "some product",
                    "isGift": false
                }
            ]
        },
        {
            "id": -2,
            "shippingAddress": {
                "firstName": "test1",
                "lastName": "test2",
                "line1": "another street",
                "line2": "another",
                "city": "another city",
                "countryName": "united kingdom",
                "countryCode": "UK",
                "postalCode": "another code",
                "regionName": "another region",
                "email": "testUser1@test.com",
                "phoneNumber": "01123456789",
                "name": "6f2a43a1-d7a4-4786-a9b2-cd267287c25a"
            },
            "shippingMethodId": "00000000-0000-0000-0000-000000000000",
            "lineItems": [
                {
                    "id": -3,
                    "contentId": "6d16a343-dda5-4914-bbc5-fccb38f31be0",
                    "code": "some product",
                    "placedPrice": 0.0,
                    "quantity": 1.0,
                    "displayName": "some product",
                    "isGift": false
                }
            ]
        }
    ],
    "payments": [],
    "couponCodes": []
}
#313341
Nov 29, 2023 10:57
Vote:
 

Can't comment about commerce but Content Delivery Api you can add additional fields to the response.  There are core classes that can be extended to do a lot more. In CMS you will need to extend ContentApiModelFillter something like below and override filter method to add/remove additional properties

    [ServiceConfiguration(typeof(IContentApiModelFilter), Lifecycle = ServiceInstanceScope.Singleton)]
    public class CustomContentApiModelFilter : ContentApiModelFilter<ContentApiModel>
    {

        public override void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
        {
 

            // To remove a field
            contentApiModel.Properties.Remove("Category");

            #region Add Content Type GUID to output

            contentApiModel.Properties[CONTENT_TYPE_GUID] = Guid.Empty.ToString("N");
            if (contentApiModel.ContentLink?.Id != null && contentApiModel.ContentLink.Id.HasValue)
            {
                var content = contentLoader.Get<IContent>(new ContentReference(contentApiModel.ContentLink.Id.Value));
                var contentType = content.ContentTypeID;
                var type = contentTypeRepository.Load(contentType);

                if (type != null && type.GUID != Guid.Empty)
                {
                    contentApiModel.Properties[CONTENT_TYPE_GUID] = type.GUID.ToString("N");
                }
            }
            #endregion
        }

}

#313726
Dec 05, 2023 21:47
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.