2

I'm working with the Nest API, which supports REST Streaming via Firebase. I have REST working, however I cannot get it to stream correctly. This is very important for my app, and REST just isn't effective for what I want to do.

I'm using Hammock for the requests, and here's the code:

public class NestAPI
{
    private RestClient client { get; set; }

    public NestAPI()
    {
        this.client = new RestClient();
        this.client.Authority = "https://developer-api.nest.com/";
        this.client.HasElevatedPermissions = true;
    }

    public void BeginStreaming()
    {
        RestRequest request = new RestRequest();
        request.AddParameter("auth", App.accessToken);
        request.RetryPolicy = new RetryPolicy() { RetryCount = 3 };
        //Enables streaming
        //request.AddHeader("Accept", "text/event-stream");
        //request.StreamOptions = new StreamOptions() { Duration = new TimeSpan(96, 0, 0), ResultsPerCallback = 1 };
        this.client.BeginRequest<object>(request, new RestCallback<object>(this.StreamCompletedEvent));
    }

    private void StreamCompletedEvent(RestRequest request, RestResponse<object> response, object userState)
    {
        //TO DO: check for errors first
        string json = response.Content;
    }

    public void EndStreaming()
    {
        this.client.CancelStreaming();
    }
}

This code works and does return JSON, however I can't seem to enable streaming. When I uncomment the lines below "Enables streaming", the callback event never fires. It's important to note that authentication is done using the uri parameter, "auth".

Unfortunately, there doesn't seem to be Firebase libraries available, and REST is my only option. I want to know when JSON properties change and want to set different values while streaming.

Bailey Stein
  • 331
  • 2
  • 12
  • Are we working with Hammock or [hammock2](https://github.com/danielcrenna/hammock2)? The former was sunsetted. Did you make any progress on this? Tried Nest support yet? I'm curious to hear the outcome. – Kato Jul 10 '14 at 16:20

2 Answers2

2

I'm not familiar with Hammock, but can you make sure that it's set to follow redirects? The streaming endpoint typically issues HTTP 307 to get inform the client of the correct server to connect to.

Greg Soltis
  • 1,434
  • 9
  • 10
1

I've never used Hammock, but looking through source code (briefly) it appears you need to set it up as a streaming request with StreamOptions. Twitter has some open source that uses this here https://github.com/camertron/twitter-windows/blob/master/Source/Twitter/Classes/API/Streaming/UserStream.cs.

The way you have Hammock configured here it's waiting for an entire request to complete before calling your callback. This will (almost) never happen with a streaming request as the server keeps the connection open to push new results.

mccv
  • 386
  • 1
  • 2
  • 7
  • Thank you for your response, but I still can't get it to work. You mentioned setting it up with StreamOptions - I am, see the code in the OP. – Bailey Stein Jun 30 '14 at 23:06
  • oops, didn't read the commented code thoroughly enough. That pretty much exhausts my expertise with Hammock – mccv Jul 01 '14 at 17:24