Skip to content

Addressbook Service

Maciej Mionskowski edited this page Nov 1, 2016 · 4 revisions

Addressbook service let's you manage(add,delete,update) your contacts.

Creating Service

import (
	"github.com/route4me/route4me-go-sdk"
	"github.com/route4me/route4me-go-sdk/addressbook"
)

func main() {
	client := route4me.NewClient("your-api-key")
	service := &addressbook.Service{Client: client}
}

Endpoints

Getting contacts

import (
	"github.com/route4me/route4me-go-sdk"
	"github.com/route4me/route4me-go-sdk/addressbook"
)

func main() {
	client := route4me.NewClient("your-api-key")
	service := &addressbook.Service{Client: client}
	query := &addressbook.Query{
		Limit:  10, //How many contacts to return
		Offset: 0, //The offset (how many to skip) of contacts.
	}
	contacts, total, err := service.Get(query)
	if err != nil {
		//handle errors
		return
	}
	//do something with contacts, it's []Contact
	//total represents the total number of contacts in your addressbook
}

Get location

GET an address book location by containing specified text in any field.

import (
	"github.com/route4me/route4me-go-sdk"
	"github.com/route4me/route4me-go-sdk/addressbook"
)

func main() {
	client := route4me.NewClient("your-api-key")
	service := &addressbook.Service{Client: client}
	query := &addressbook.Query{
		Query:  "technology",
		Offset: 0,
		Limit:  20,
	}
	locations, total, err := service.Get(query)
	if err != nil {
		//handle errors
		return
	}
	// do something with locations
}

Display locations included in the routes.

package test

import (
	"github.com/route4me/route4me-go-sdk"
	"github.com/route4me/route4me-go-sdk/addressbook"
)

func main() {
	client := route4me.NewClient("your-api-key")
	service := &addressbook.Service{Client: client}
	query := &addressbook.Query{
		Limit:  10, //How many contacts to return
		Offset: 0,  //The offset (how many to skip) of contacts.
		Display: "routed", //include locations in routes
	}
	contacts, total, err := service.Get(query)
	if err != nil {
		//handle errors
		return
	}
	//do something with contacts, it's []Contact
	//total represents the total number of contacts in your addressbook
}

Get locations by ids

GET locations from an address book by a specified list of locations IDs.

import (
	"github.com/route4me/route4me-go-sdk"
	"github.com/route4me/route4me-go-sdk/addressbook"
)

func main() {
	client := route4me.NewClient("your-api-key")
	service := &addressbook.Service{Client: client}
	query := &addressbook.Query{
		AddressID: "4623361,6281217" //comma separated
	}
	locations, total, err := service.Get(query) 
	if err != nil {
		//handle errors
		return
	}
	// do something with locations
}

Adding a contact

import (
	"github.com/route4me/route4me-go-sdk"
	"github.com/route4me/route4me-go-sdk/addressbook"
)

func main() {
	client := route4me.NewClient("your-api-key")
	service := &addressbook.Service{Client: client}
	//To add a contact you will first need to create it's instance.
	contact := &addressbook.Contact{
		FirstName:   "John",
		Alias:       "johny",
		Address1:    "Some address",
		CachedLat:   38.024654,
		CachedLng:   -77.338814,
		Email:       "[email protected]",
		PhoneNumber: "000-000-000",
		StateID:     "5",
		CountryID:   "3",
		City:        "City",
		ZIP:         "00-000",
	}
	newContact, err := service.Add(contact)
	if err != nil {
		//Handle errors
	}
}

Removing contacts

To remove a contact you will need it's ID.

import (
	"github.com/route4me/route4me-go-sdk"
	"github.com/route4me/route4me-go-sdk/addressbook"
)

func main() {
	client := route4me.NewClient("your-api-key")
	service := &addressbook.Service{Client: client}
	removed, err := service.Delete([]string{"contact-id"})//It's likely your contact id is in uint64. You will need to convert it to string by calling strconv.FormatUint(contact-id, 10)
	if err != nil {
		//handle errors
		return
	}
}

Updating contact

To update a contact you will need it's ID or instance, you can get it by calling Get() as stated above.

import (
	"github.com/route4me/route4me-go-sdk"
	"github.com/route4me/route4me-go-sdk/addressbook"
)

func main() {
	client := route4me.NewClient("your-api-key")
	service := &addressbook.Service{Client: client}
	contact, err := service.Update(&contact)
	if err != nil {
		//handle errors
		return
	}
}

You can look at service's test file for more implementation details.