Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with types for API request (expects array)

I’m working on a SDK, and I have a request that is being formatted as following:

{
    "maxCount": 5,
    "startDateTime": "0001-01-01T00:00:00+00:00",
    "endDateTime": "9999-12-31T23:59:59.9999999+00:00",
    "clinics": {
        "clinicId": "string",
        "treatments": {
            "treatmentId": "string",
            "treaters": [
                "string",
                "string"
            ]
        }
    }
}

however, the API expects this:

{
    "maxCount": 5,
    "startDateTime": "0001-01-01T00:00:00+00:00",
    "endDateTime": "9999-12-31T23:59:59.9999999+00:00",
    "clinics": [
        {
            "clinicId": "string",
            "treatments": [
                {
                    "treatmentId": "string",
                    "treaters": [
                        "string",
                        "string"
                    ]
                }
            ]
        }
    ]
}

if you spot the difference, the issue is that clinics and treatments should be an array [ ].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

now, I should change this in the constructor?

    public record SearchAvailableSlotsByClinicsRequest
    {
        public SearchAvailableSlotsByClinicsRequest(int maxCount, DateTimeOffset startDateTime, DateTimeOffset endDateTime, ClinicTreatment clinics)
        {
            MaxCount = maxCount;
            StartDatetime = startDateTime;
            EndDateTime = endDateTime;
            Clinics = clinics;
        }
        public int MaxCount { get; set; }
        public DateTimeOffset StartDatetime { get; set; }
        public DateTimeOffset EndDateTime { get; set; }
        public ClinicTreatment  Clinics { get; set; }
    }

I’m asking as it seems that if I make the ClinicTreatment and IEnumerable the class that created the clinicTreatment will give error as the object it creates is not an IEnumerable. what am I doing wrong?

    public class ClinicTreatment
    {
        public string ClinicId { get; set; }
        public TreatmentTreater Treatments { get; set; }
    }

and then the clinicTreatment gives problem as the clinicTreatment is not IEnumerable:

var clinicTreatment = new ClinicTreatment {ClinicId = clinic.Id.Id, Treatments = treatmentTreater };

>Solution :

Do it like this:

    public record SearchAvailableSlotsByClinicsRequest
    {
        public SearchAvailableSlotsByClinicsRequest(int maxCount, DateTimeOffset startDateTime, DateTimeOffset endDateTime, ClinicTreatment clinics)
        {
            MaxCount = maxCount;
            StartDatetime = startDateTime;
            EndDateTime = endDateTime;
            Clinics = clinics;
        }
        public int MaxCount { get; set; }
        public DateTimeOffset StartDatetime { get; set; }
        public DateTimeOffset EndDateTime { get; set; }
        public ClinicTreatment[]  Clinics { get; set; }
    }

And when you need to serialize it, then use not this:

{
   Clinics = yourClinics,
}

Then use this:

{
   Clinics = new Clinics[1]{yourClinics},
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading