restful

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

REST API Manager

Allows you to call any API and then transform the returned json data and return back. Restful also supports using JSONPath style xpath to filter the JSON data. Currently, if both XPath and Transformer are provided, restful pipes the output of XPath to Transformer and then returns the final result.

Example use

func main() {
	// Get a message and print it.
	options := Options{}
	options.Method = "GET"
	options.Headers = make(map[string]string)
	options.Headers["Content-Type"] = "application/json"
	options.XPath = "$.results[0].name"
	options.Transformer = `
	[
		{
			"operation": "delete",
			"spec": {
			  "paths": ["title"]
			}
		  }
	  ]	
	`
	message, _ := Call("https://randomuser.me/api/", &options)
	fmt.Print(message)
}

The above ex. makes the API call, extracts the name object from JSON, removes the title and returns back.

JSON Transformation supports Jolt like transformations using Kazaam XPath provides complete implementation of http://goessner.net/articles/JsonPath/ using JsonPath Supports Basic authorization, and token/key based authentication.

There is basic string interpolation support in url, raw body and headers. Tokens in the form of ${var} are replaced with os.Getenv(var). Below is one example:

func main() {
	options := restful.Options{}
	options.Method = "GET"
	options.Headers = make(map[string]string)
	options.Headers["Content-Type"] = "application/json"
	options.Transformer = `[
		{
			"operation": "shift", 
			  "spec": {
				"data.title": "joke",
				"data.subtitle": "category"
			}
		},
		{
		"operation": "default",
		"spec": {
		  "event": "jotd"
		}
	  }
	  ]`
	os.Setenv("type", "single")
	message, _ := restful.Call("https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=${type}", &options)
	fmt.Println(message)
}

Embedded JavaScript Engine

Starting from version 0.4, Restful introduces an integrated ES2015/ES6 JavaScript runtime for scenarios where basic XPath and Transforms fall short. The options.JS parameter accepts a JavaScript literal that enables the transformation of input data. The JSON obtained from the API call is accessible through the data variable within the JavaScript code block. The JavaScript string should conclude with the value to be exported, as illustrated in the example below with the result being the exported output.

	options := Options{}
	options.Method = "GET"
	options.Headers = make(map[string]string)
	options.Headers["Content-Type"] = "application/json"
	options.JS = `const x = data.results.map(u => u.name)[0];
	const { first, last } = x;` +
		"const result = {\"name\": `${last}, ${first}`};result;"
	message, _ := Call("https://randomuser.me/api/", &options)
	fmt.Print(message)
	t.Log(message)

Sequencing of transforms (output of each transform is fed into the next step as input):

  1. JS
  2. XPath
  3. Transformer

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call(url string, options *Options) (string, int)

Types

type Options

type Options struct {
	Method      string
	Payload     string
	Headers     map[string]string
	Transformer string
	XPath       string
	JS          string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址