pushy

package module
v0.0.0-...-4c6045d Latest Latest
Warning

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

Go to latest
Published: May 20, 2018 License: MIT Imports: 6 Imported by: 2

README

Pushy.me strongly typed SDK for golang

Go Report CardBuild Statuscodecov

Visit godoc for documentation

Installation

go get github.com/fossapps/pushy

Usage:

package main

import (
	"github.com/fossapps/pushy"
	"log"
	"time"
)

func main() {
	sdk := pushy.Create("API_TOKEN", pushy.GetDefaultAPIEndpoint())
	sdk.SetHTTPClient(pushy.GetDefaultHTTPClient(10 * time.Second))
	res, requestErr, networkErr := sdk.DeviceInfo("DEVICE_ID")
	if networkErr != nil {
	  log.Println(networkErr)
	}
	if requestErr != nil {
	  log.Println(requestErr)
	}
	log.Println(res)
}

Documentation

Overview

Package pushy can be used to communicate with pushy service easily, saving time which would require to figure out the data type, endpoint, http method and shape of request to make

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDefaultAPIEndpoint

func GetDefaultAPIEndpoint() string

GetDefaultAPIEndpoint returns the default api endpoint where they're hosted.

Example
package main

import (
	"fmt"

	"github.com/fossapps/pushy"
)

func main() {
	fmt.Print(pushy.GetDefaultAPIEndpoint())
}
Output:

https://api.pushy.me

Types

type Device

type Device struct {
	Date     int    `json:"date"`
	Platform string `json:"platform"`
}

Device is basic representation of a device

type DeviceInfo

type DeviceInfo struct {
	Device        Device   `json:"device"`
	Subscriptions []string `json:"subscriptions"`
	Presence      struct {
		Online     bool `json:"online"`
		LastActive struct {
			Date       int `json:"date"`
			SecondsAgo int `json:"seconds_ago"`
		} `json:"last_active"`
	} `json:"presence"`
	PendingNotifications []Notification `json:"pending_notifications"`
}

DeviceInfo is a basic structure which has additional info

type DevicePresenceRequest

type DevicePresenceRequest struct {
	Tokens []string `json:"tokens"`
}

DevicePresenceRequest is representation of request needed to get information of device(s)'s presence

type DevicePresenceResponse

type DevicePresenceResponse struct {
	Presence []Presence `json:"presence"`
}

DevicePresenceResponse is representation of device presence response from pushy

type DeviceSubscriptionRequest

type DeviceSubscriptionRequest struct {
	Token  string   `json:"token"`
	Topics []string `json:"topics"`
}

DeviceSubscriptionRequest is representation of a request we send for subscribing a new device to topic

type Error

type Error struct {
	Error string `json:"error"`
}

Error are simple error responses returned from pushy if request isn't valid

type IHTTPClient

type IHTTPClient interface {
	Get(string) (*http.Response, error)
	Post(string, string, io.Reader) (*http.Response, error)
	Do(*http.Request) (*http.Response, error)
}

IHTTPClient is signature needed for an object to be acceptable as a http client.

func GetDefaultHTTPClient

func GetDefaultHTTPClient(timeout time.Duration) IHTTPClient

GetDefaultHTTPClient returns a httpClient with configured timeout

type IOSNotification

type IOSNotification struct {
	Body         string   `json:"body"`
	Badge        int      `json:"badge"`
	Sound        string   `json:"sound"`
	Title        string   `json:"title"`
	Category     string   `json:"category"`
	LocKey       string   `json:"loc_key"`
	LocArgs      []string `json:"loc_args"`
	TitleLocKey  string   `json:"title_loc_key"`
	TitleLocArgs []string `json:"title_loc_args"`
}

IOSNotification is a basic data for notification for iOS devices it's internally called notification, is represented as IOSNotification to communicate that this only applies for iOS devices

type IPushyClient

type IPushyClient interface {
	SetHTTPClient(client IHTTPClient)
	GetHTTPClient() IHTTPClient
	DeviceInfo(deviceID string) (*DeviceInfo, *Error, error)
	DevicePresence(deviceID ...string) (*DevicePresenceResponse, *Error, error)
	NotificationStatus(pushID string) (*NotificationStatus, *Error, error)
	DeleteNotification(pushID string) (*SimpleSuccess, *Error, error)
	SubscribeToTopic(deviceID string, topics ...string) (*SimpleSuccess, *Error, error)
	UnsubscribeFromTopic(token string, topics ...string) (*SimpleSuccess, *Error, error)
	NotifyDevice(request SendNotificationRequest) (*NotificationResponse, *Error, error)
}

IPushyClient interface to implement to qualify as a Pushy Client

type Notification

type Notification struct {
	ID      string      `json:"id"`
	Date    int         `json:"date"`
	Payload interface{} `json:"payload"`
}

Notification is a basic representation of a notification

type NotificationResponse

type NotificationResponse struct {
	Success bool   `json:"success"`
	ID      string `json:"id"`
}

NotificationResponse is a simple response from server when a new notification is created

type NotificationStatus

type NotificationStatus struct {
	Push struct {
		Date           int         `json:"date"`
		Payload        interface{} `json:"payload"`
		Expiration     int         `json:"expiration"`
		PendingDevices []string    `json:"pending_devices"`
	} `json:"push"`
}

NotificationStatus is a basic status info of a Notification

type Presence

type Presence struct {
	ID         string `json:"id"`
	Online     bool   `json:"online"`
	LastActive int    `json:"last_active"`
}

Presence is a basic representation of a device's presence

type Pushy

type Pushy struct {
	APIToken    string
	APIEndpoint string
	// contains filtered or unexported fields
}

Pushy is a basic struct with two configs: APIToken and APIEndpoint implements IPushyClient interface

func Create

func Create(APIToken string, APIEndpoint string) *Pushy

Create is a helper method to initialize a simple Pushy struct

&Pushy{APIToken: "token", APIEndpoint: "https://api.pushy.me"}

can be used

func (*Pushy) DeleteNotification

func (p *Pushy) DeleteNotification(pushID string) (*SimpleSuccess, *Error, error)

DeleteNotification deletes a created notification

Example
cleaner := setupNotifyDeletionStuff()
defer cleaner()
sdk := pushy.Create("API_TOKEN", pushy.GetDefaultAPIEndpoint())
sdk.SetHTTPClient(pushy.GetDefaultHTTPClient(100 * time.Millisecond))
status, _, _ := sdk.DeleteNotification("some_id")
fmt.Print(status.Success)
Output:

true

func (Pushy) DeviceInfo

func (p Pushy) DeviceInfo(deviceID string) (*DeviceInfo, *Error, error)

DeviceInfo returns information about a particular device

Example
cleaner := setupDeviceInfoStuff()
defer cleaner()
sdk := pushy.Create("API_TOKEN", pushy.GetDefaultAPIEndpoint())
sdk.SetHTTPClient(pushy.GetDefaultHTTPClient(10 * time.Millisecond))

res, _, _ := sdk.DeviceInfo("DEVICE_ID")
fmt.Println(res.Presence.Online)
fmt.Println(res.Presence.LastActive.SecondsAgo)
Output:

true
215

func (*Pushy) DevicePresence

func (p *Pushy) DevicePresence(deviceID ...string) (*DevicePresenceResponse, *Error, error)

DevicePresence returns data about presence of a data

Example
cleaner := setupDevicePresenceStuff()
defer cleaner()
sdk := pushy.Create("API_TOKEN", pushy.GetDefaultAPIEndpoint())
sdk.SetHTTPClient(pushy.GetDefaultHTTPClient(10 * time.Millisecond))
presence, _, _ := sdk.DevicePresence("DEVICE_ID")
fmt.Println(presence.Presence[0].ID)
fmt.Println(presence.Presence[0].Online)
fmt.Println(presence.Presence[0].LastActive)
Output:

a6f36efb913f1def30c6
false
1429406442

func (*Pushy) GetHTTPClient

func (p *Pushy) GetHTTPClient() IHTTPClient

GetHTTPClient returns the client which is being used with pushy

func (*Pushy) NotificationStatus

func (p *Pushy) NotificationStatus(pushID string) (*NotificationStatus, *Error, error)

NotificationStatus returns status of a particular notification

Example
cleaner := setupNotificationStatusStuff()
defer cleaner()
sdk := pushy.Create("API_TOKEN", pushy.GetDefaultAPIEndpoint())
sdk.SetHTTPClient(pushy.GetDefaultHTTPClient(10 * time.Millisecond))
status, _, _ := sdk.NotificationStatus("PUSH_ID")
fmt.Println(status.Push.Expiration)
fmt.Println(status.Push.Date)
fmt.Println(status.Push.Payload) // todo isn't working for some reason.
Output:

1466595935
1464003935
map[message:Hello World!]

func (*Pushy) NotifyDevice

func (p *Pushy) NotifyDevice(request SendNotificationRequest) (*NotificationResponse, *Error, error)

NotifyDevice sends notification data to devices

Example
cleaner := setupNotifyStuff()
defer cleaner()
sdk := pushy.Create("API_TOKEN", pushy.GetDefaultAPIEndpoint())
sdk.SetHTTPClient(pushy.GetDefaultHTTPClient(100 * time.Millisecond))
status, _, _ := sdk.NotifyDevice(pushy.SendNotificationRequest{})
fmt.Println(status.Success)
fmt.Println(status.ID)
Output:

true
some_id

func (*Pushy) SetHTTPClient

func (p *Pushy) SetHTTPClient(client IHTTPClient)

SetHTTPClient sets a http client, it's useful when you're using sandboxed env like appengine this is required to do, pushy won't automatically use the default http client.

func (*Pushy) SubscribeToTopic

func (p *Pushy) SubscribeToTopic(deviceID string, topics ...string) (*SimpleSuccess, *Error, error)

SubscribeToTopic subscribes a particular device to topics (when you want to do from backend)

Example
cleaner := setupSubscribeToTopicStuff()
defer cleaner()
sdk := pushy.Create("API_TOKEN", pushy.GetDefaultAPIEndpoint())
sdk.SetHTTPClient(pushy.GetDefaultHTTPClient(10 * time.Millisecond))
subscription, _, _ := sdk.SubscribeToTopic("DEVICE_ID", "TOPIC")
fmt.Println(subscription.Success)
Output:

true

func (*Pushy) UnsubscribeFromTopic

func (p *Pushy) UnsubscribeFromTopic(token string, topics ...string) (*SimpleSuccess, *Error, error)

UnsubscribeFromTopic un subscribes a particular device from topics (when you want to do from backend)

Example
cleaner := setupUnSubscribeFromTopicStuff()
defer cleaner()
sdk := pushy.Create("API_TOKEN", pushy.GetDefaultAPIEndpoint())
sdk.SetHTTPClient(pushy.GetDefaultHTTPClient(10 * time.Millisecond))
subscription, _, _ := sdk.UnsubscribeFromTopic("DEVICE_ID", "TOPIC")
fmt.Println(subscription.Success)
Output:

true

type SendNotificationRequest

type SendNotificationRequest struct {
	To                  []string        `json:"to"`
	Data                string          `json:"data"`
	TimeToLive          int             `json:"time_to_live"`
	IOSMutableContent   bool            `json:"mutable_content"`
	IOSContentAvailable bool            `json:"content_available"`
	IOSNotification     IOSNotification `json:"notification"`
}

SendNotificationRequest is representation of data to be sent to pushy service to create new notification

type SimpleSuccess

type SimpleSuccess struct {
	Success bool `json:"success"`
}

SimpleSuccess is a response from pushy when our request is accepted

Jump to

Keyboard shortcuts

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