Categorygithub.com/gsmservice-pl/messaging-sdk-go
modulepackage
1.2.5
Repository: https://github.com/gsmservice-pl/messaging-sdk-go.git
Documentation: pkg.go.dev

# README

Go Reference GitHub Release GitHub License Static Badge

GSMService.pl Messaging REST API SDK for Go

This package includes Messaging SDK for Go to send SMS & MMS messages directly from your app via https://bramka.gsmservice.pl messaging platform.

Additional documentation:

A documentation of all methods and types is available below in section Available Resources and Operations .

Also you can refer to the REST API documentation for additional details about the basics of this SDK.

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get github.com/gsmservice-pl/messaging-sdk-go

SDK Example Usage

Sending single SMS Message

This example demonstrates simple sending SMS message to a single recipient:

package main

import (
	"context"
	messagingsdkgo "github.com/gsmservice-pl/messaging-sdk-go"
	"github.com/gsmservice-pl/messaging-sdk-go/models/components"
	"log"
	"os"
)

func main() {
	s := messagingsdkgo.New(
		messagingsdkgo.WithSecurity(os.Getenv("GATEWAY_API_BEARER")),
	)

	ctx := context.Background()
	res, err := s.Outgoing.Sms.Send(ctx, operations.CreateSendSmsRequestBodyArrayOfSmsMessage(
		[]components.SmsMessage{
			components.SmsMessage{
				Recipients: components.CreateSmsMessageRecipientsArrayOfStr(
					[]string{
						"+48999999999",
					},
				),
				Message: "To jest treść wiadomości",
				Sender:  messagingsdkgo.String("Bramka SMS"),
				Type:    components.SmsTypeSmsPro.ToPointer(),
				Unicode: messagingsdkgo.Bool(true),
				Flash:   messagingsdkgo.Bool(false),
				Date:    nil,
			},
		},
	))
	if err != nil {
		log.Fatal(err)
	}
	if res.Messages != nil {
		// handle response
	}
}

Sending single MMS Message

This example demonstrates simple sending MMS message to a single recipient:

package main

import (
	"context"
	messagingsdkgo "github.com/gsmservice-pl/messaging-sdk-go"
	"github.com/gsmservice-pl/messaging-sdk-go/models/components"
	"log"
	"os"
)

func main() {
	s := messagingsdkgo.New(
		messagingsdkgo.WithSecurity(os.Getenv("GATEWAY_API_BEARER")),
	)

	ctx := context.Background()
	res, err := s.Outgoing.Mms.Send(ctx, operations.CreateSendMmsRequestBodyArrayOfMmsMessage(
		[]components.MmsMessage{
			components.MmsMessage{
				Recipients: components.CreateRecipientsArrayOfStr(
					[]string{
						"+48999999999",
					},
				),
				Subject: messagingsdkgo.String("To jest temat wiadomości"),
				Message: messagingsdkgo.String("To jest treść wiadomości"),
				Attachments: messagingsdkgo.Pointer(components.CreateAttachmentsArrayOfStr(
					[]string{
						"<file_body in base64 format>",
					},
				)),
				Date: nil,
			},
		},
	))
	if err != nil {
		log.Fatal(err)
	}
	if res.Messages != nil {
		// handle response
	}
}

Available Resources and Operations

Available methods

Accounts

Common

  • Ping - Checks API availability and version

Incoming

  • List - List the received SMS messages
  • GetByIds - Get the incoming messages by IDs

Outgoing

  • GetByIds - Get the messages details and status by IDs
  • CancelScheduled - Cancel a scheduled messages
  • List - Lists the history of sent messages

Outgoing.Mms

  • GetPrice - Check the price of MMS Messages
  • Send - Send MMS Messages

Outgoing.Sms

  • GetPrice - Check the price of SMS Messages
  • Send - Send SMS Messages

Senders

  • List - List allowed senders names
  • Add - Add a new sender name
  • Delete - Delete a sender name
  • SetDefault - Set default sender name

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a retry.Config object to the call by using the WithRetries option:

package main

import (
	"context"
	messagingsdkgo "github.com/gsmservice-pl/messaging-sdk-go"
	"github.com/gsmservice-pl/messaging-sdk-go/retry"
	"log"
	"models/operations"
	"os"
)

func main() {
	s := messagingsdkgo.New(
		messagingsdkgo.WithSecurity(os.Getenv("GATEWAY_API_BEARER")),
	)

	ctx := context.Background()
	res, err := s.Accounts.Get(ctx, operations.WithRetries(
		retry.Config{
			Strategy: "backoff",
			Backoff: &retry.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res.AccountResponse != nil {
		// handle response
	}
}

If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:

package main

import (
	"context"
	messagingsdkgo "github.com/gsmservice-pl/messaging-sdk-go"
	"github.com/gsmservice-pl/messaging-sdk-go/retry"
	"log"
	"os"
)

func main() {
	s := messagingsdkgo.New(
		messagingsdkgo.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		messagingsdkgo.WithSecurity(os.Getenv("GATEWAY_API_BEARER")),
	)

	ctx := context.Background()
	res, err := s.Accounts.Get(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.AccountResponse != nil {
		// handle response
	}
}

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both.

By Default, an API error will return sdkerrors.SDKError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.

For example, the Get function may return the following errors:

Error TypeStatus CodeContent Type
sdkerrors.ErrorResponse401, 403, 4XX, 5XXapplication/problem+json

Example

package main

import (
	"context"
	"errors"
	messagingsdkgo "github.com/gsmservice-pl/messaging-sdk-go"
	"github.com/gsmservice-pl/messaging-sdk-go/models/sdkerrors"
	"log"
	"os"
)

func main() {
	s := messagingsdkgo.New(
		messagingsdkgo.WithSecurity(os.Getenv("GATEWAY_API_BEARER")),
	)

	ctx := context.Background()
	res, err := s.Accounts.Get(ctx)
	if err != nil {

		var e *sdkerrors.ErrorResponse
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *sdkerrors.SDKError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}
	}
}

Server Selection

Select Server by Name

You can override the default server globally using the WithServer option when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the names associated with the available servers:

NameServerVariables
prodhttps://api.gsmservice.pl/restNone
sandboxhttps://api.gsmservice.pl/rest-sandboxNone

Example

package main

import (
	"context"
	messagingsdkgo "github.com/gsmservice-pl/messaging-sdk-go"
	"log"
	"os"
)

func main() {
	s := messagingsdkgo.New(
		messagingsdkgo.WithServer("sandbox"),
		messagingsdkgo.WithSecurity(os.Getenv("GATEWAY_API_BEARER")),
	)

	ctx := context.Background()
	res, err := s.Accounts.Get(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.AccountResponse != nil {
		// handle response
	}
}

Override Server URL Per-Client

The default server can also be overridden globally using the WithServerURL option when initializing the SDK client instance. For example:

package main

import (
	"context"
	messagingsdkgo "github.com/gsmservice-pl/messaging-sdk-go"
	"log"
	"os"
)

func main() {
	s := messagingsdkgo.New(
		messagingsdkgo.WithServerURL("https://api.gsmservice.pl/rest"),
		messagingsdkgo.WithSecurity(os.Getenv("GATEWAY_API_BEARER")),
	)

	ctx := context.Background()
	res, err := s.Accounts.Get(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.AccountResponse != nil {
		// handle response
	}
}

Custom HTTP Client

The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.

import (
	"net/http"
	"time"
	"github.com/myorg/your-go-sdk"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = sdk.New(sdk.WithClient(httpClient))
)

This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

NameTypeSchemeEnvironment Variable
BearerhttpHTTP BearerGATEWAY_API_BEARER

You can configure it using the WithSecurity option when initializing the SDK client instance. For example:

package main

import (
	"context"
	messagingsdkgo "github.com/gsmservice-pl/messaging-sdk-go"
	"log"
	"os"
)

func main() {
	s := messagingsdkgo.New(
		messagingsdkgo.WithSecurity(os.Getenv("GATEWAY_API_BEARER")),
	)

	ctx := context.Background()
	res, err := s.Accounts.Get(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.AccountResponse != nil {
		// handle response
	}
}

Special Types

Development

Maturity

This SDK is in continuous development and there may be breaking changes between a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Bool provides a helper function to return a pointer to a bool.
Float32 provides a helper function to return a pointer to a float32.
Float64 provides a helper function to return a pointer to a float64.
Int provides a helper function to return a pointer to an int.
Int64 provides a helper function to return a pointer to an int64.
New creates a new instance of the SDK with the provided options.
Pointer provides a helper function to return a pointer to a type.
String provides a helper function to return a pointer to a string.
WithClient allows the overriding of the default HTTP client used by the SDK.
No description provided by the author
WithSecurity configures the SDK to use the provided security details.
WithSecuritySource configures the SDK to invoke the Security Source function on each method call to determine authentication.
WithServer allows the overriding of the default server by name.
WithServerURL allows the overriding of the default server URL.
WithTemplatedServerURL allows the overriding of the default server URL with a templated URL populated with the provided parameters.
WithTimeout Optional request timeout applied to each operation.

# Constants

Production system.
Test system (SANDBOX).

# Variables

ServerList contains the list of servers available to the SDK.

# Structs

No description provided by the author
Client - Messaging Gateway GSMService.pl: This package includes Messaging SDK for GO to send SMS and MMS messages directly from your app via [https://bramka.gsmservice.pl](https://bramka.gsmservice.pl) messaging platform.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

HTTPClient provides an interface for suplying the SDK with a custom HTTP client.

# Type aliases

No description provided by the author