Giter Site home page Giter Site logo

autopoll's Introduction

autopoll

Small library to start/stop polling of a REST API

Setup

import pack into application:

import "github.com/3cb/autopoll"

install package onto system:

go get "github.com/3cb/autopoll"

Poller will return data to caller application in the form of a PollMsg:

type PollMsg struct {
	Payload []byte
	Error   []error
}

If there is no error from the http request or from reading the response body, PollMsg.Error will have a length of 0. It is up to caller application to Unmarshal data into appropriate data structure.

Example Usage

func main() {
    api := "https://data.melbourne.vic.gov.au/resource/vh2v-4nfs.json?$limit=5"
    interval := time.Second * 30
    out := make(chan PollMsg)
    shutdown := make(chan *sync.WaitGroup)

    poller := autopoll.NewPoller(api, interval, out, shutdown)

    type Spot struct {
        StMarkerID string `json:"st_marker_id"`
        BayID      string `json:"bay_id"`
        Location   struct {
            Latitude      string `json:"latitude"`
            HumanAddress  string `json:"human_address"`
            NeedsRecoding bool   `json:"needs_recoding"`
            Longitude     string `json:"longitude"`
        } `json:"location"`
        Lon    string `json:"lon"`
        Lat    string `json:"lat"`
        Status string `json:"status"`
    }
    spots := []Spot{}

    poller.Start()

    for {
        msg :=<-out
        if len(msg.Error) > 0 {
            poller.Stop()
            break
        }
        err := json.Unmarshal(msg.Payload, &spots)
        if err != nil {
            poller.Stop()
            break
        }

        // Do something useful with "spots" data

    }
}

Above example is runnable here: https://github.com/3cb/autopoll/examples/melbourne_parking.go

API docs: https://dev.socrata.com/foundry/data.melbourne.vic.gov.au/dtpv-d4pf

autopoll's People

Contributors

3cb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.