redguava / cliniko-api

The API for Cliniko
74 stars 39 forks source link

Individual_appointments endpoint #333

Closed Paperpotato closed 3 years ago

Paperpotato commented 3 years ago

Hi there!

My current apps are making up to 1000+ API calls per day to the Cliniko server, which can be reduced to just 10 if the individual_appointments endpoint could include the patient’s phone number. It’s not urgent, but hope to see it one day if it isn’t too difficult 🙂

bpinto commented 3 years ago

Hi @Paperpotato,

could you give me more information about the requests you are doing? I might be able to help you reduce the number of requests you are performing.

Paperpotato commented 3 years ago

Hi Bruno!

Thanks for the reply :) The goal is to get all of tomorrow's appointments and their phone numbers. The first request gets all of tomorrow's appointments via individual_appointments; then for each appointment, follow the patient.links.self prop to get the patient's phone number. If you have any suggestions, it would be appreciated!

bpinto commented 3 years ago

Sorry for the delay on getting a reply, but I have good news, there is a way to considerably reduce the number of requests you are performing. The idea is to fetch patients in batches using the IDs on the patient.links.self property.

As an example, something like this:

import { uniq } from 'lodash'

const fetchPatients = (ids) => {
  fetch(`${API_URL}/v1/patients?q=id:=${ids.join(',')}`
}

const patientId = (appointment) => appointment.patient.links.self.match(/\/(\d+)$/)?[1]

fetchTomorrowAppointments().then(appointments => {
  const patientIds = uniq(appointments.map(appointment => patientId(appointment))
  fetchPatients(patientIds)
...
})

P.S.: The patient fetch needs to be done in batches, we suggest fetching at most 100 patients at a time using the q=id:=[] filter.

Paperpotato commented 3 years ago

Beautiful, thanks so much! I didn't know you could query multiple patients in a single request. This is super helpful :)