HTTP

Networking library for communicating with external API’s and services.

This module allows users to make requests to external web services. It offers a normalized interface for making requests similar to Angular’s HTTP module.

Internally, this module makes use of Axios which can then be used to make traditional REST-based requests. Axios returns a promise after a request has been created.

This module contains two methods:

Promises returned by the requests made through Axios can be used with a stack which allows using ES7 Async/Await syntax to enable writing asynchronous code synchronously.

Request

Signature

Request(urlPath: string = \"\", options: object = {})

Returns: Axios.Instance

For most cases (if not all), this instance can be created by not passing in any arguments. This will use the default absolute path of the server that the page is currently loaded from and work with Skedulo’s base path.

However, to make requests to external 3rd party services or API’s, you will have to pass in the baseURL to the target service.

Basic example

import { HTTP } from 'uranium'

// Make JSON requests to Skedulo's API
const http = HTTP.Request()

const vocabularyP = http.get('/vocabulary')

vocabularyP
	.then(res => res.data)
	.then(vocabulary => {
  	// Access to your org's vocabulary
	})

External services

import { HTTP } from 'uranium'

// Make JSON requests to Skedulo's API
const http = HTTP.Request('http://jsonplaceholder.typicode.com')

// Make Request
http.get('/posts')
  .then(res => res.data)
	.then(posts => {
  	// Access posts here 
	})

Async/Wait example

import { HTTP } from 'uranium'

// Make JSON requests to Skedulo's API
const http = HTTP.Request()

// Fetch Vocabulary
// @returns: Promise<Vocabulary>
function fetchVocabulary() {
	return http.get("/vocabulary").then(res => res.data)
}

// Fetching UserMetadata 
// @returns: Promise<UserMetadata>
function fetchUserMetadata() {
  return http.get("/usermetadata").then(res => res.data)
}

async function data(jobIds) {
	const vocabulary = await fetchVocabulary() 
  const patientForm = await fetchUserMetadata()
  
	// Continue ...
}

All requests made using Skedulo’s Query Engine will have to be sent through the Axios instance created by the API Request module. This instance of Axios will perform modifications to the response sent by Skedulo’s API to make it easily accessible.

API Request

Signature

Example

APIRequest(urlPath: string, options: object = {})

Returns: Axios.Instance

For most cases (if not all), this instance can be created by not passing in any arguments. This will use the default API path of the server that the page is currently loaded from.

For security purposes, you should not make requests to external services using APIRequest. Use the instance generated by the Request function for those scenarios.

Basic example

import { HTTP } from 'uranium'

// Make JSON requests to Skedulo's API
const httpAPI = HTTP.APIRequest()

// Fetching from `Jobs` salesforce object 
// @returns: Promise<Jobs>
const query = new Query()
  .filter("UID in $1", [jobIds])

return query.makeRequest(httpAPI, "Jobs")
.then(res => res.records)
.then(Jobs => {
	// Continue ...
})

Async/Wait example

import { HTTP } from 'uranium'

const http = HTTP.Request()
const httpAPI = HTTP.APIRequest()

// Fetch Vocabulary
// @returns: Promise<Vocabulary>
function fetchVocabulary() {
	return http.get("/vocabulary").then(res => res.data)
}

// Fetching another salesforce object 
// In this case, a patient form
// @returns: Promise<PatientForm>
function fetchPatientForm(jobIds) {

  const query = new Query()
    .filter("JobId in $1", [jobIds])
    .withFields("Job.UID, Job.Name, Job.Start, Job.End, Job.Description")
    .orderBy("PF")
    .limit(1000);

  return query.makeRequest(httpAPI, "PatientForms")
    // Use latest record made against each JobId
    .then(res => _.keyBy(res.records, "JobId"))
}

async function data(jobIds) {
	const vocabulary = await fetchVocabulary() 
  const patientForm = await fetchPatientForm(jobIds)
  
	// Continue building out data-set or make response
}

Last modified August 2, 2019: Updated fonts and finished guide 1:1 (fe87bc2)