# README
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
- SDK Example Usage
- Available Resources and Operations
- Retries
- Error Handling
- Server Selection
- Custom HTTP Client
- Authentication
- Special Types
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
- Get - Get account details
- GetSubaccount - Get subaccount details
Common
- Ping - Checks API availability and version
Incoming
Outgoing
- GetByIds - Get the messages details and status by IDs
- CancelScheduled - Cancel a scheduled messages
- List - Lists the history of sent messages
Outgoing.Mms
Outgoing.Sms
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 Type | Status Code | Content Type |
---|---|---|
sdkerrors.ErrorResponse | 401, 403, 4XX, 5XX | application/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:
Name | Server | Variables |
---|---|---|
prod | https://api.gsmservice.pl/rest | None |
sandbox | https://api.gsmservice.pl/rest-sandbox | None |
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:
Name | Type | Scheme | Environment Variable |
---|---|---|---|
Bearer | http | HTTP Bearer | GATEWAY_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.