title: Async Vala Example
body: <p>In the Book "Introduction to Vala" by Dr <strong>Michael Lauer</strong>, he has mentioned that the lib Soup async api is broken. I'm struggling to write a simple example using <code>session.queue_message</code> that query radio stations using the service from <a href="https://www.radio-browser.info/" rel="nofollow noreferrer">radio-browser</a>. Here is my code. I would appreciate any help form experienced Programmers like "Al Thomas". Thank you.</p>
  
  <pre><code>public class Station : Object {
  
      // A globally unique identifier for the change of the station information
      public string changeuuid { get; set; default = ""; }
  
      // A globally unique identifier for the station
      public string stationuuid { get; set; default = ""; }
  
      // The name of the station
      public string name { get; set; default = ""; }
  
      // The stream URL provided by the user
      public string url { get; set; default = ""; }
      // and so on ... many properties
      public string to_string () {
          var builder = new StringBuilder ();
          builder.append_printf ("\nchangeuuid = %s\n", changeuuid);
          builder.append_printf ("stationuuid  = %s\n", stationuuid);
          builder.append_printf ("name = %s\n", name);
          builder.append_printf ("url = %s\n", url);
          return (owned) builder.str;
      }
  }
  
  public class RadioBrowser : Object {
      private static Soup.Session session;
      // private static MainLoop main_loop;
      public const string API_URL = "https://de1.api.radio-browser.info/json/stations";
      public const string USER_AGENT = "github.aeldemery.radiolibrary";
  
      public RadioBrowser (string user_agent = USER_AGENT, uint timeout = 50)
      requires (timeout &gt; 0)
      {
          Intl.setlocale ();
          session = new Soup.Session ();
          session.timeout = timeout;
          session.user_agent = user_agent;
          session.use_thread_context = true;
  
          // main_loop = new MainLoop ();
      }
  
      private void check_response_status (Soup.Message msg) {
          if (msg.status_code != 200) {
              var str = "Error: Status message error %s.".printf (msg.reason_phrase);
              error (str);
          }
      }
  
      public Gee.ArrayList&lt;Station&gt; listStations () {
          var stations = new Gee.ArrayList&lt;Station&gt; ();
          var data_list = Datalist&lt;string&gt; ();
          data_list.set_data ("limit", "100");
  
          var parser = new Json.Parser ();
          parser.array_element.connect ((pars, array, index) =&gt; {
              var station = Json.gobject_deserialize (typeof (Station), array.get_element (index)) as Station;
              assert_nonnull (station);
              stations.add (station);
          });
  
          var msg = Soup.Form.request_new_from_datalist (
              "POST",
              API_URL,
              data_list
          );
          // send_message works but not queue_message
          // session.send_message (msg);
  
          session.queue_message (msg, (sess, mess) =&gt; {
              check_response_status (msg);
              try {
                  parser.load_from_data ((string) msg.response_body.flatten ().data);
              } catch (Error e) {
                  error ("Failed to parse data, error:" + e.message);
              }
          });
  
          return stations;
         }
      }
  
      int main (string[] args) {
          var radio_browser = new RadioBrowser ();
          var stations = radio_browser.listStations ();
          assert_nonnull (stations);
  
         foreach (var station in stations) {
             print (station.to_string ());
         }
         return 0;
      }
  </code></pre>