csawyer

Announcing PI Web API Client Library for Go (golang)

Blog Post created by csawyer Employee on Jun 28, 2018

Introduction

 

We're excited to announce to PI Dev Club members that we now have a PI Web API Client Library for Go, the Google-sponsored programming language specifically designed around concurrent processing.

 

You can visit the GitHub repository of this library here.

 

Requirements

 

go1.9.7 or later

 

Installation

 

If you haven't already, install the Go software development kit.

 

Run this line to install the PI Web API Client for go

 

go get -u github.com/christoofar/gowebapi

 

Note: You don't need the Go SDK after you have compiled a go program and wish to deploy it somewhere.  Go creates self-reliant executable programs that compact dependent libraries inside them.

 

Getting Started

 

Here is a sample Go program for retrieving the links from the Web API home page.

 

Create a directory under %GOPATH% and let's call it webapitest. Then create a new code file with the name webapitest.go

This will print all the version numbers of your PI Web API server plugins. Replace the string literals {in braces} with the appropriate values for your environment.

 

// webapitest.go
package main

import (
    "context"
    "fmt"
    "log"

    pi "github.com/christoofar/gowebapi"
)

var cfg = pi.NewConfiguration()

var client *pi.APIClient
var auth context.Context

func Init() {
    cfg.BasePath = "https://{your web api server here}/piwebapi"

    auth = context.WithValue(context.Background(), pi.ContextBasicAuth, pi.BasicAuth{
        UserName: "{user name here}",
        Password: "{password here}",
    })

    client = pi.NewAPIClient(cfg)
}

func main() {
     Init()
    response, _, fail := client.SystemApi.SystemVersions(auth)
    if fail != nil {
        log.Fatal(fail)
    }

    fmt.Println("Here's all the plugin versions on PI Web API")
    for i := range response {
        fmt.Println(i, response[i].FullVersion)
    }
}

 

You can run the program by issuing the following commands

 

~/go/webapitest $ go build
~/go/webapitest $ ./webapitest

 

Your output should look something like this

 

~/go/webapitest $ ./webapitest
Here's all the plugin versions on PI Web API
OSIsoft.REST.Documentation 1.11.0.967
OSIsoft.REST.Services 1.11.0.967
OSIsoft.Search.SvcLib 1.8.0.3651
OSIsoft.PIDirectory 1.0.0.0
OSIsoft.REST.Core 1.11.0.967

 

Coding examples

 

There are some simple examples on how to start probing the PI Web API Client library over here.

 

Developing in Go

 

Golang programmers tend to develop using Visual Studio Code on Windows which has great golang support and is also available on MacOS and Linux.   There is great golang support available for emacs (configure emacs from scratch as a Go IDE) and vim as plugins which also give you function templates, IntelliSense, syntax checking, godoc (the documentation system for go), gofmt (code formatting/style) and support Delve, the debugger for the go language which cleanly handles the concept of go routines.

 

You can also build Go code with nothing but your web browser using the Go Playground.   This is a very handy tool where you can experiment with Go code snippets and compile and run them directly in a web browser, viewing the output.

 

Caveats

 

A WebID wrapper has been added to the library.  You can review the unit test code to see how you can create WebIDs from paths.

 

Final Thoughts

 

Most everyone's exposure with Go is minimal (including myself!).  But this language is expected to grow in popularity.   The reason: Go's awesome power to simplify concurrent programming is making it spread quickly, particularly within the realm of sensors and other lightweight devices.   Go code is also quite fast and produces programs that are lightweight yet powerful.

 

It's also a very simple programming language to learn (so simple you can become a Go programmer in 15 minutes if you have exposure to any other programming language).  Considering that there is also a rich library of data adapters written in Go, it made obvious sense to open a portal to the world of OSIsoft in golang.

 

Happy Gophering!

Outcomes