reqxml

package
v0.23.5 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package reqxml contains utilities for sending and receiving XML.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Body

func Body(v any) requests.BodyGetter

Body is a BodyGetter that marshals a XML object.

Example
package main

import (
	"context"
	"encoding/xml"
	"fmt"
	"net/http/httputil"
	"strings"

	"github.com/carlmjohnson/requests"
	"github.com/carlmjohnson/requests/reqxml"
)

func main() {
	// Make an XML object
	type Object struct {
		XMLName xml.Name `xml:"object"`
		ID      int      `xml:"id,attr"`
		Verbose bool     `xml:"communication>mode>verbosity>high"`
		Comment string   `xml:",comment"`
	}

	v := &Object{
		ID:      42,
		Verbose: true,
		Comment: "Hello!",
	}

	req, err := requests.
		URL("http://example.com").
		// reqxml.BodyConfig sets the content type for us
		Config(reqxml.BodyConfig(&v)).
		Request(context.Background())
	if err != nil {
		fmt.Println("Error!", err)
	}

	// Pretty print the request
	b, _ := httputil.DumpRequestOut(req, true)
	fmt.Println(strings.ReplaceAll(string(b), "\r", ""))

}
Output:

POST / HTTP/1.1
Host: example.com
User-Agent: Go-http-client/1.1
Content-Length: 122
Content-Type: application/xml
Accept-Encoding: gzip

<object id="42"><communication><mode><verbosity><high>true</high></verbosity></mode></communication><!--Hello!--></object>

func BodyConfig

func BodyConfig(v any) requests.Config

BodyConfig sets the Builder's request body to the marshaled XML. It also sets ContentType to "application/xml".

func Error

func Error(v any) requests.ResponseHandler

Error is a ValidatorHandler that applies DefaultValidator and decodes the response as an XML object if the DefaultValidator check fails.

Example
package main

import (
	"context"
	"encoding/xml"
	"errors"
	"fmt"

	"github.com/carlmjohnson/requests"
	"github.com/carlmjohnson/requests/reqxml"
)

func main() {
	type ErrorXML struct {
		XMLName   xml.Name `xml:"Error"`
		Code      string   `xml:"Code"`
		Message   string   `xml:"Message"`
		RequestID string   `xml:"RequestId"`
		HostID    string   `xml:"HostId"`
	}

	var errObj ErrorXML
	err := requests.
		URL("http://example.s3.us-east-1.amazonaws.com").
		AddValidator(reqxml.Error(&errObj)).
		Fetch(context.Background())
	switch {
	case errors.Is(err, requests.ErrInvalidHandled):
		fmt.Println(errObj.Message)
	case err != nil:
		fmt.Println("Error!", err)
	case err == nil:
		fmt.Println("unexpected success")
	}

}
Output:

Access Denied

func To

To decodes a response as an XML object.

Example
package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/carlmjohnson/requests"
	"github.com/carlmjohnson/requests/reqxml"
)

func init() {
	http.DefaultClient.Transport = requests.Replay("testdata")
	// http.DefaultClient.Transport = requests.Caching(nil, "testdata")
}

func main() {
	type CD struct {
		Title   string  `xml:"TITLE"`
		Artist  string  `xml:"ARTIST"`
		Country string  `xml:"COUNTRY"`
		Price   float64 `xml:"PRICE"`
		Year    int     `xml:"YEAR"`
	}
	type Catalog struct {
		CDs []CD `xml:"CD"`
	}
	var cat Catalog
	err := requests.
		URL("https://www.w3schools.com/xml/cd_catalog.xml").
		Handle(reqxml.To(&cat)).
		Fetch(context.Background())
	if err != nil {
		fmt.Println("Error!", err)
	}
	for _, cd := range cat.CDs {
		if cd.Price > 10 && cd.Year < 1990 {
			fmt.Printf("%s - %s $%.2f", cd.Artist, cd.Title, cd.Price)
		}
	}
}
Output:

Bob Dylan - Empire Burlesque $10.90

Types

This section is empty.

Jump to

Keyboard shortcuts

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