Call the API

After understanding what type of data Asyncher is able to work with, let's now understand how to interface with it.

When you decide to use Asyncher in your infrastructure, you will no longer have to do your HTTP requests directly from the client to the server but, as far as synchronization is concerned, you will have to send requests to Asyncher.


Client Side

Regardless of the programming language you're using, what you'll need to do is a simple HTTP request to the only API exposed by Asyncher. Let's look at this example:

void callAsyncherExample(url) async {
  // Fetch all data
  List<ExampleData> allData = getAllData();
  
  // Send http request to Asyncher with the data encoded in json
  final response = await http.post(
    url,
    headers: {
      'Content-Type': 'application/json'
    },
    body: jsonEncode(allData)
  );
  
  // Update database with synchronized data
  deleteAllData();
  insertAllData(response.body);
}

Ignoring the Dart syntax, the main parts are:

  1. Get all data (naturally the data you want to sync).

  2. Make an HTTP request with all the data.

  3. Overwrite all data in the database

Note that I converted the data to JSON before sending it. Asyncher always expects a JSON and returns a JSON.


Server Side

The logic is practically the same. In this case your server will have to expose two endpoints:

  • One that Asyncher will use to get all the data (pull_url)

  • One that Asyncher will use, after comparing the data, to push it to the server (push_url)

This could be your endpoints in PHP:

// This will be the pull_url method
public function pullData() {
    $data = $this->findAllData();
    
    return json_encode($data);
}

// This will be the push_url method
public function pushData($data) {
    $this->removeAllData();
    $this->insertAllData($data);
}

I recommend you protect these two endpoints (for example, being able to call them only if you are logged in, if you have a specific token etc.) and see how Asyncher handles requests with credentials.

Last updated