SDK Go – SoftAfrik Push API
Client Go officiel pour interagir avec SoftAfrik Push API (PUID, devices, notifications push).
1. Installation
Le SDK Go est volontairement simple : un seul fichier Go à importer dans vos projets.
/your-project
/sdk
/go
softafrikpush.go
README.md
examples/
basic_push.go
2. Fichier officiel : softafrikpush.go
Copiez ce fichier dans sdk/go/softafrikpush.go.
// sdk/go/softafrikpush.go
package softafrikpush
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
type Client struct {
ApiKey string
BaseURL string
}
func NewClient(apiKey string) *Client {
return &Client{
ApiKey: apiKey,
BaseURL: "https://softafrik-push-api.brillanciel.com",
}
}
func (c *Client) post(path string, payload interface{}) (map[string]interface{}, error) {
jsonData, err := json.Marshal(payload)
if err != nil { return nil, err }
req, err := http.NewRequest("POST", c.BaseURL+path, bytes.NewBuffer(jsonData))
if err != nil { return nil, err }
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-KEY", c.ApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { return nil, err }
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return result, nil
}
// RegisterUser -> returns PUID
func (c *Client) RegisterUser(ref string, meta map[string]interface{}) (map[string]interface{}, error) {
return c.post("/api/users/register", map[string]interface{}{
"external_user_ref": ref,
"meta": meta,
})
}
// RegisterDevice
func (c *Client) RegisterDevice(puid, app, platform, token string, extra map[string]string) (map[string]interface{}, error) {
payload := map[string]interface{}{
"puid": puid,
"app_name": app,
"platform": platform,
"device_token": token,
}
for k, v := range extra {
payload[k] = v
}
return c.post("/api/devices/register", payload)
}
// PushToUser
func (c *Client) PushToUser(puid, title, body string, data map[string]interface{}) (map[string]interface{}, error) {
return c.post("/api/push/to-user", map[string]interface{}{
"puid": puid,
"title": title,
"body": body,
"data": data,
})
}
// PushToDevice
func (c *Client) PushToDevice(token, title, body string, data map[string]interface{}) (map[string]interface{}, error) {
return c.post("/api/push/to-device", map[string]interface{}{
"device_token": token,
"title": title,
"body": body,
"data": data,
})
}
// Broadcast
func (c *Client) Broadcast(app, country, title, body string, data map[string]interface{}) (map[string]interface{}, error) {
payload := map[string]interface{}{
"title": title,
"body": body,
"data": data,
}
if app != "" { payload["app_name"] = app }
if country != "" { payload["country"] = country }
return c.post("/api/push/broadcast", payload)
}
3. Exemple complet
// sdk/go/examples/basic_push.go
package main
import (
"fmt"
"softafrikpush"
)
func main() {
client := softafrikpush.NewClient("VOTRE_API_KEY")
// 1) USER
user, _ := client.RegisterUser("user_12345", map[string]interface{}{
"full_name": "John Doe",
"email": "john@example.com",
})
fmt.Println("USER =>", user)
// 2) DEVICE
device, _ := client.RegisterDevice(
"sa_1001_xxxxxxxx",
"MyFintechApp",
"android",
"AAAAxxxxx",
map[string]string{
"device_uid": "device-uuid-123",
"lang": "fr",
"country": "TG",
},
)
fmt.Println("DEVICE =>", device)
// 3) PUSH TO USER
push, _ := client.PushToUser(
"sa_1001_xxxxxxxx",
"Transfert reçu",
"Vous avez reçu 20 000 XOF",
map[string]interface{}{
"transaction_id": 9932,
},
)
fmt.Println("PUSH =>", push)
}
Rappel des endpoints
POST /api/users/registerPOST /api/devices/registerPOST /api/devices/unregisterPOST /api/push/to-userPOST /api/push/to-devicePOST /api/push/broadcast
Test rapide
client := softafrikpush.NewClient("VOTRE_API_KEY")
resp, _ := client.PushToUser(
"sa_1001_xxxxxxxx",
"Ping",
"Hello from SoftAfrik Push API",
map[string]interface{}{},
)
fmt.Println(resp)