Calling API for RETS data (cURL to fetch function)

So I have been working on creating Wix website that use MLS and I have not found the best solution yet. I think with Corvid, there may be a nice way to finally make this work.

The plan is to pull all of the listing information from an API called SimplyRETS (you can see their sample data here - https://docs.simplyrets.com/api/index.html#!/default/get_properties ). I plan on pulling from the “properties” section of the API.

Anyway, I’m wondering if I could get any guidance as to how best implement this (probably in a .jsx web module) on Corvid, using the fetch features. The issue I’m running into is the fact that the documentation is strictly explaining how to do the API call using cURL, and there is also authentication here. Finally, I’m getting an error when trying to use base64. Here is what I have so far, but any guidance would be helpful


let url = ‘https://api.simplyrets.com/properties?limit=500&lastId=0’;
let username = ‘simplyrets’;
let password = ‘simplyrets’;
let headers = {
‘Authorization’: 'Basic ’ + base64.encode(username + “:” + password)
};
let options = {
method:‘POST’,
headers: headers
}
fetch(url, options)
.then((httpResponse) => {
// Add code to check the response
})
}

Here are some links I have been looking at - so if you’re trying to help you can be up to speed with what I’ve already looked at, but I’m still just confused:
https://stackoverflow.com/questions/43842793/basic-authentication-with-fetch
https://support.wix.com/en/article/corvid-accessing-third-party-services-with-the-fetch-api
https://docs.simplyrets.com/api/index.html#!/default/get_properties
https://www.wix.com/corvid/reference/wix-fetch.html

Hey Ben,

First thing I have done was adding error handling to the backend function.

The function with error handling -

export function simplyrets(factor1, factor2) {
  let url = 'https://api.simplyrets.com/properties?limit=500&lastId=0';
  let username = 'simplyrets';
  let password = 'simplyrets';
  let headers = {
        'Authorization': 'Basic ' + base64.encode(username + ":" + password)
  };
  let options = {
        method: 'POST',
        headers: headers
  };
  return fetch(url, options)
        .then((httpResponse) => {
          return httpResponse.json();
        })
        .catch(err => {
          return err;
        });
}

Note the return statement before fetch, and the handling of the .then and .catch of the fetch promise.

When running this way, the IDE shows us that base64 is not defined.

Running the code gives us the following error -

err ReferenceError: base64 is not defined

To have proper base 64 encoding, we can use Buffer as in the method below

export function simplyrets2(factor1, factor2) {
  let url = 'https://api.simplyrets.com/properties?limit=500&lastId=0';
  let username = 'simplyrets';
  let password = 'simplyrets';
  let buff = Buffer.from(username + ":" + password);  
  let base64data = buff.toString('base64');
  let headers = {
        'Authorization': 'Basic ' + base64data
  };
  let options = {
        method: 'POST',
        headers: headers
  }
  return fetch(url, options)
        .then((httpResponse) => {
           return httpResponse.json();
        })
        .catch(err => {
           return err;
        })
}

Now, when we run the code we get

{ 
  error: "Bad method",
  message: "Bad method"
}

Why is that? because we should be using HTTP method GET and not HTTP method POST with simplyrest. Changing the function again, to the below - works

export function simplyrets2(factor1, factor2) {
  let url = 'https://api.simplyrets.com/properties?limit=500&lastId=0';
  let username = 'simplyrets';
  let password = 'simplyrets';
  let buff = Buffer.from(username + ":" + password);  
  let base64data = buff.toString('base64');
  let headers = {
        'Authorization': 'Basic ' + base64data
  };
  let options = {
        method: 'GET',
        headers: headers
  }
  return fetch(url, options)
        .then((httpResponse) => {
            return httpResponse.json();
        })
        .catch(err => {
            return err;
        })
}