client

package
v1.67.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 5 Imported by: 0

README

Package client

Пакет client предоставляет SOAP-клиент для взаимодействия с SOAP-сервисами. Поддерживает отправку запросов, обработку ответов и ошибок в формате SOAP Fault.

Types

Client

Структура для отправки SOAP-запросов. Использует httpcli.Client для HTTP-взаимодействия.

Methods:

New(cli *httpcli.Client) Client

Конструктор клиента.

(c Client) Invoke(ctx context.Context, url string, soapAction string, extraHeaders map[string]string, requestBody any) (*Response, error)

Отправляет SOAP-запрос. Автоматически формирует SOAP-конверт.

Usage

Default usage flow
package main

import (
	"context"
	"encoding/xml"
	"log"

	"github.com/txix-open/isp-kit/http/httpclix"
	"github.com/txix-open/isp-kit/http/soap/client"
)

type userRequest struct {
	XMLName xml.Name `xml:"UserRequest"`
	Name    string   `xml:"name"`
}

type userResponse struct {
	XMLName xml.Name `xml:"UserResponse"`
	Id      int      `xml:"id"`
}

func main() {
	cli := client.New(httpclix.Default())
	resp, err := cli.Invoke(
		context.Background(),
		"https://api.example.com/users",
		"CreateUser",
		nil,
		userRequest{Name: "Alice"},
	)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Close()

	var res userResponse
	err = resp.UnmarshalPayload(&res)
	if err == nil {
		log.Printf("id: %d\n", res.Id)
		return
	}

	fault, err := resp.Fault()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("SOAP Fault: %s\n", fault.String)
}

Documentation

Overview

Package client provides a SOAP client for invoking SOAP web services. It handles XML envelope creation, SOAP action headers, and response parsing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a SOAP client that wraps an httpcli.Client for making SOAP requests.

func New

func New(cli *httpcli.Client) Client

New creates a new SOAP client with the specified HTTP client.

func (Client) Invoke

func (c Client) Invoke(
	ctx context.Context,
	url string,
	soapAction string,
	extraHeaders map[string]string,
	requestBody any,
) (*Response, error)

Invoke sends a SOAP request to the specified URL with the given action and headers. It automatically wraps the request body in a SOAP envelope and parses the response. Returns a Response object for parsing the SOAP response or fault.

type PlainXml

type PlainXml struct {
	Value []byte `xml:",innerxml"`
}

PlainXml represents raw XML content for SOAP requests or responses. It is used when the SOAP body contains non-structured XML data. The Value field captures the inner XML without envelope wrapping.

type Response

type Response struct {
	Http *httpcli.Response
}

Response represents a SOAP response received from a web service. It provides methods for parsing the response payload and checking for faults.

func (*Response) Close

func (r *Response) Close()

Close closes the underlying HTTP response body.

func (*Response) Fault

func (r *Response) Fault() (*soap.Fault, error)

Fault checks if the response contains a SOAP fault and returns it. It returns nil if no fault is present. Returns an error if the response body cannot be read or decoded.

func (*Response) UnmarshalPayload

func (r *Response) UnmarshalPayload(responseBody any) error

UnmarshalPayload decodes the SOAP response body into the provided struct. It handles both standard SOAP envelopes and raw XML (PlainXml) responses. Returns an error if the response body cannot be read or decoded.

Jump to

Keyboard shortcuts

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