WARNING: alpha quality code; use at your own risk.
Generic https://rest.gadventures.com G API Document
datatype
It extracts the common code for dealing with resource (JSON) "documents" from the G API.
- Calculating checksums (
ETag
) - Specific behaviour on how the G API de/serializes JSON (e.g. nil slices are [], not null)
Equal
method to be able to compare oneDocument
to another- Binary serialization, which is faster than JSON and thanks to
golang/snappy
, results in smaller payload size - Evaluating if the response is a
GAPIError
The core datatype Document
, is an alias for map[string]interface{}
. While
this will be slower than using custom structs, the tradeoff is that Document
instances do not require customization based on the requested resource. In
other words adding/removing attributes from G API resources does not require
modifications to the gadventures/apidoc
module.
specify the following in your go.mod
file
require github.com/gadventures/apidoc v0.2.0
package main
import (
"encoding/json"
"log"
"github.com/gadventures/apidoc"
)
const blob := `{
"street": "Stand 1385",
"city": "Victoria Falls",
"country": {
"id": "ZW",
"href": "https://rest.gadventures.com/countries/ZW",
"name": "Zimbabwe"
}
}`
func main() {
doc = apidoc.New()
err := json.Unmarshal([]byte(blob), &doc)
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(doc["city"].(string)) // will print "Victoria Falls"
id, ok := doc.GetPath("country", "id")
if !ok {
log.Fatal("could not find country.id in document")
}
fmt.Println(countryID.(string)) // will print "ZW"
}
This library can be combined with gadventures/httpclient to build a rudimentary REST Client in Go for the G API.