Saving signal metadata

This call creates or updates a set of signals with the provided metadata. Each signal is uniquely identified by its Input ID in combination with the Integration ID.

curl --request POST \
  --url '<<apiURL>>rpc' \
  --header 'content-type: application/json' \
  --header 'Authorization: Bearer <YOUR ACCESS TOKEN>' \
  --header 'X-API-Version: 1.0' \
  --data \
  '
    {
      "jsonrpc": "2.0",
      "method": "integration.SaveSignals",
      "id": "1",
      "params": {
        "integration": "<YOUR INTEGRATION ID>",
        "inputs": {
          "<your-input-id>" : {
            "name": "<your-signal-name>",
            "labels": {
              "data-source": [["<your-data-source-name>"],
              "location": [["<your-location-name>"]
            }
          }
        }
      }
    }
  '
package main

import (
    "bytes"
    "context"
    "encoding/json"
    "fmt"
    "net/http"
    "net/url"

    "golang.org/x/oauth2/clientcredentials"
)

type RPCRequest struct {
    JSONRPC string
    Method  string
    ID      int
    Params  Params
}

type RPCReponse struct {
    Jsonrpc string
    ID      int
    Error   *RPCError              `json:"error,omitempty"`
    Result  map[string]interface{} `json:"result,omitempty"`
}

type RPCError struct {
    Code    int
    Message string
}

type Params struct {
    Integration string
    Inputs      map[string]Signal `json:"inputs,omitempty"`
    Data        Data              `json:"data,omitempty"`
}

type Data struct {
    Times  []string             `json:"times,omitempty"`
    Series map[string][]float64 `json:"series,omitempty"`
}

type Signal struct {
    Name        string
        Labels      map[string][]string
        Annotations map[string]string

        Type       string          `json:",omitempty"`
        SourceType string          `json:",omitempty"`
        EngUnit    string          `json:",omitempty"`
        EnumValues map[int]string  `json:",omitempty"`

        // TODO: You can add more fields.
        // See reference docs.
}


var (
    clientID      = "<YOUR CLIENT ID>"
    clientSecret  = "<YOUR CLIENT SECRET>"
    integrationID = "<YOUR INTEGRATION ID>"
)

func main() {
    config := &clientcredentials.Config{
        ClientID:       clientID,
        ClientSecret:   clientSecret,
        TokenURL:       "<<tokenURL>>",
        EndpointParams: url.Values{"audience": []string{"<<apiURL>>"}},
    }

    ctx := context.Background()
    client := config.Client(ctx)

    // create some signals to save
    inputs := map[string]Signal{}
    inputs["test-id-1337"] = Signal{
        Name: "<my signal name>",
        Labels: map[string][]string{
            "data-source": {"<my data source name>"},
            "location": {"<my location name>"},
        }
        // Add more meta data here as needed
    }

    reqBody := RPCRequest{
        JSONRPC: "2.0",
        Method:  "integration.SaveSignals",
        ID:      1,
        Params: Params{
            Integration: integrationID,
            Inputs:      inputs,
        },
    }

    payloadBuf := new(bytes.Buffer)
    err := json.NewEncoder(payloadBuf).Encode(reqBody)
    if err != nil {
        //TODO: Proper error handling
        fmt.Println(err)
        return
    }

    req, err := http.NewRequest("POST", "<<apiURL>>rpc", payloadBuf)
    if err != nil {
        //TODO: Proper error handling
        fmt.Println(err)
        return
    }

    req.Header.Add("X-API-Version", "1.0")
    req.Header.Add("Content-Type", "application/json")

    resp, err := client.Do(req)
    if err != nil {
        //TODO: Proper error handling
        fmt.Println(err)
        return
    }

    // If response code is 200 it was successful
    if resp.StatusCode != 200 {
        fmt.Println("Could not perform request to the endpoint. Response below:")
        fmt.Println(resp.Status)
        return
    }

    rpcResp := RPCReponse{}
    err = json.NewDecoder(resp.Body).Decode(&rpcResp)
    if err != nil {
        //TODO: Proper error handling
        fmt.Println(err)
        return
    }

    if rpcResp.Error != nil {
        fmt.Println("The request was unsuccessful. Response below:")
        fmt.Println(rpcResp.Error.Code)
        fmt.Println(rpcResp.Error.Message)
        return
    }

    fmt.Println("The request was successful. Response below:")
    fmt.Println(rpcResp.Result)
}
from oauthlib.oauth2 import BackendApplicationClient
from oauthlib.oauth2 import TokenExpiredError
from requests_oauthlib import OAuth2Session
from pprint import pprint
import json

client_id = "<YOUR CLIENT ID>"
client_secret = "<YOUR CLIENT SECRET>"
integration_id = "<YOUR INTEGRATION ID>"

session = OAuth2Session(client=BackendApplicationClient(client_id=client_id))

try:
    session.fetch_token(
        body="audience=<<apiURL>>",
        token_url="<<tokenURL>>",
        client_secret=client_secret,
    )
except:
    raise Exception("failed to get access token: {}".format(format_exc()))

body = {
    "jsonrpc": "2.0",
    "method" : "integration.SaveSignals",
    "id": "1",
    "params": { 
        "integration": integration_id,
        "inputs": {
            "your-input-id" : {
                "name": "<your-signal-name>",
                "labels": {
                  "data-source": ["<your data source name>"],
                  "location": ["<your location name>"],
                }
            }
        }
    }
}

headers = {
    "Content-Type": "application/json",
    "X-API-Version": "1.0"
}

try:
    r = session.post("<<apiURL>>rpc", data=json.dumps(body), headers=headers)
except TokenExpiredError as e:
    token = session.fetch_token(
        body="audience=<<apiURL>>",
        token_url="<<tokenURL>>",
        client_secret=client_secret,
    )

pprint(r.json())

Did this page help you?