Пример #1
0
// This example uses a Decoder to decode a stream of distinct JSON values.
func ExampleDecoder() {
	const jsonStream = `
		{"Name": "Ed", "Text": "Knock knock."}
		{"Name": "Sam", "Text": "Who's there?"}
		{"Name": "Ed", "Text": "Go fmt."}
		{"Name": "Sam", "Text": "Go fmt who?"}
		{"Name": "Ed", "Text": "Go fmt yourself!"}
	`
	type Message struct {
		Name, Text string
	}
	dec := json.NewDecoder(strings.NewReader(jsonStream))
	for {
		var m Message
		if err := dec.Decode(&m); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s: %s\n", m.Name, m.Text)
	}
	// Output:
	// Ed: Knock knock.
	// Sam: Who's there?
	// Ed: Go fmt.
	// Sam: Go fmt who?
	// Ed: Go fmt yourself!
}
Пример #2
0
func (self *DefaultJSONContext) DecodeJSON(i interface{}) error {
	buf := &bytes.Buffer{}
	bodyReader := io.TeeReader(self.Req().Body, buf)
	err := json.NewDecoder(bodyReader).Decode(i)
	if err != nil {
		return err
	}
	self.decodedBody = buf.Bytes()

	// skip checking errors since it can be an unsupported type
	_ = trimmer.TrimStrings(i)

	return nil
}
Пример #3
0
func GetSoundZones(c ServiceConnector, account_id key.Key, token AccessToken) (result RemoteSoundZones, err error) {
	request, response, err := DoRequest(c, "GET", c.GetAuthService(), fmt.Sprintf("accounts/%v/sound_zones", account_id.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = RemoteSoundZones{}
	err = json.NewDecoder(response.Body).Decode(&result)
	return
}
Пример #4
0
func GetSpotifyAccount(c ServiceConnector, soundZone key.Key, token AccessToken) (result *RemoteSpotifyAccount, err error) {
	request, response, err := DoRequest(c, "GET", c.GetPaymentService(), fmt.Sprintf("sound_zones/%v/spotify_account", soundZone.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteSpotifyAccount{}
	err = json.NewDecoder(response.Body).Decode(result)
	return
}
Пример #5
0
func GetSpotifyAccounts(c ServiceConnector, account key.Key, token AccessToken) (result RemoteSpotifyAccounts, err error) {
	request, response, err := DoRequest(c, "GET", c.GetPaymentService(), fmt.Sprintf("accounts/%v/payment_method/spotify_accounts", account.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = RemoteSpotifyAccounts{}
	err = json.NewDecoder(response.Body).Decode(&result)
	return
}
Пример #6
0
func GetAccounts(c ServiceConnector, user key.Key, token AccessToken) (result []RemoteAccount, err error) {
	request, response, err := DoRequest(c, "GET", c.GetAuthService(), fmt.Sprintf("users/%v/accounts", user.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = []RemoteAccount{}
	err = json.NewDecoder(response.Body).Decode(&result)
	return
}
Пример #7
0
func GetPendingByExternalId(c ServiceConnector, externalId string, token AccessToken) (result *RemotePending, err error) {
	request, response, err := DoRequest(c, "GET", c.GetAuthService(), fmt.Sprintf("pending/%v", externalId), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemotePending{}
	err = json.NewDecoder(response.Body).Decode(&result)
	return
}
Пример #8
0
func UpdateSoundZone(c ServiceConnector, token AccessToken, updatedSoundZone RemoteSoundZone) (result *RemoteSoundZone, err error) {
	request, response, err := DoRequest(c, "PUT", c.GetAuthService(), fmt.Sprintf("sound_zones/%v", updatedSoundZone.Id.Encode()), token, updatedSoundZone)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteSoundZone{}
	err = json.NewDecoder(response.Body).Decode(result)
	return
}
Пример #9
0
func CreateBusinessAccount(c ServiceConnector, token AccessToken, account RemoteAccount, owner key.Key) (result *RemoteAccount, err error) {
	request, response, err := DoRequest(c, "POST", c.GetAuthService(), fmt.Sprintf("users/%v/accounts/business", owner.Encode()), token, account)
	if err != nil {
		return
	}
	if response.StatusCode != 201 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteAccount{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #10
0
func GetAccountContact(c ServiceConnector, account key.Key, token AccessToken) (result *RemoteUser, err error) {
	request, response, err := DoRequest(c, "GET", c.GetAuthService(), fmt.Sprintf("accounts/%v/contact", account.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteUser{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #11
0
func CreateLocation(c ServiceConnector, token AccessToken, remoteLocation RemoteLocation) (result *RemoteLocation, err error) {
	request, response, err := DoRequest(c, "POST", c.GetAuthService(), fmt.Sprintf("accounts/%v/locations", remoteLocation.Account.Encode()), token, remoteLocation)
	if err != nil {
		return
	}
	if response.StatusCode != 201 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteLocation{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #12
0
func GetBillingGroupsByAccountId(c ServiceConnector, account key.Key, token AccessToken) (result RemoteBillingGroups, err error) {
	request, response, err := DoRequest(c, "GET", c.GetPaymentService(), fmt.Sprintf("accounts/%v/billing_groups", account.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = RemoteBillingGroups{}
	err = json.NewDecoder(response.Body).Decode(&result)

	return
}
Пример #13
0
func UpdateUser(c ServiceConnector, user *RemoteUser, token AccessToken) (result *RemoteUser, err error) {
	request, response, err := DoRequest(c, "PUT", c.GetAuthService(), fmt.Sprintf("users/%v", user.Id.Encode()), token, user)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteUser{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #14
0
func CreateUser(c ServiceConnector, user RemoteUser) (result *RemoteUser, err error) {
	request, response, err := DoRequest(c, "POST", c.GetAuthService(), "users", nil, user)
	if err != nil {
		return
	}
	if response.StatusCode != 201 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteUser{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #15
0
func CreateSlot(c ServiceConnector, token AccessToken, slot RemoteSlot) (result *RemoteSlot, err error) {
	request, response, err := DoRequest(c, "POST", c.GetRadioService(), fmt.Sprintf("schedules/%v/slots", slot.Schedule.Encode()), token, slot)
	if err != nil {
		return
	}
	if response.StatusCode != 201 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteSlot{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #16
0
func CreateSoundZoneError(c ServiceConnector, token AccessToken, e RemoteSoundZoneError, soundZoneId key.Key) (result *RemoteSoundZoneError, err error) {
	request, response, err := DoRequest(c, "POST", c.GetAuthService(), fmt.Sprintf("sound_zones/%v/sound_zone_errors", soundZoneId.Encode()), token, e)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteSoundZoneError{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #17
0
func CreateSoundZone(c ServiceConnector, token AccessToken, remoteSoundZone RemoteSoundZone) (result *RemoteSoundZone, err error) {
	request, response, err := DoRequest(c, "POST", c.GetAuthService(), fmt.Sprintf("locations/%v/sound_zones", remoteSoundZone.Location.Encode()), token, remoteSoundZone)
	if err != nil {
		return
	}
	if response.StatusCode != 201 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteSoundZone{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #18
0
func GetLocation(c ServiceConnector, location key.Key, token AccessToken) (result *RemoteLocation, err error) {
	request, response, err := DoRequest(c, "GET", c.GetAuthService(), fmt.Sprintf("locations/%v", location.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteLocation{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #19
0
func GetProductQueue(c ServiceConnector, productQueueId key.Key, token AccessToken) (result *RemoteProductQueue, err error) {
	request, response, err := DoRequest(c, "GET", c.GetPaymentService(), fmt.Sprintf("product_queues/%v", productQueueId.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteProductQueue{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #20
0
func CreatePriceModel(c ServiceConnector, paymentMethodId key.Key, isoCountry string, priceModel *RemotePriceModel, token AccessToken) (result *RemotePriceModel, err error) {
	request, response, err := DoRequest(c, "POST", c.GetPaymentService(), fmt.Sprintf("accounts/%v/payment_method/price_models/%v", paymentMethodId.Encode(), isoCountry), token, priceModel)
	if err != nil {
		return
	}
	if response.StatusCode != 201 {
		err = errorFor(request, response)
		return
	}

	result = &RemotePriceModel{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #21
0
func IsApplicableForInvoice(c ServiceConnector) (result *RemoteIsApplicableForInvoiceResponse, err error) {
	request, response, err := DoRequest(c, "POST", c.GetPaymentService(), "register/applicable_for_invoice", nil, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteIsApplicableForInvoiceResponse{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #22
0
func Auth(c ServiceConnector, auth_request AuthRequest) (result *DefaultAccessToken, encoded string, err error) {
	request, response, err := DoRequest(c, "POST", c.GetAuthService(), "auth", nil, auth_request)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &DefaultAccessToken{}
	err = json.NewDecoder(response.Body).Decode(result)
	result.Encoded = strings.Join(response.Header["X-Access-Token-Issued"], "")
	result.Encoded = strings.Replace(result.Encoded, ",", "", -1)
	result.Encoded = strings.Replace(result.Encoded, " ", "", -1)
	return
}
Пример #23
0
func SetPassword(c ServiceConnector, user key.Key, password string, token AccessToken) (result *RemoteUser, err error) {
	request, response, err := DoRequest(c, "PUT", c.GetAuthService(), fmt.Sprintf("users/%s/password", user.Encode()), token, map[string]string{
		"password": password,
	})
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &RemoteUser{}
	err = json.NewDecoder(response.Body).Decode(result)

	return
}
Пример #24
0
func GetDeviceHierarchyByVendor(c ServiceConnector, vendor_id string, mac string, token AccessToken) (result *DeviceHierarchy, err error) {
	request, response, err := DoRequest(c, "GET", c.GetAuthService(), fmt.Sprintf("/device_hierarchy/vendor_id/%v/%v", vendor_id, mac), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	result = &DeviceHierarchy{
		Device:    &RemoteDevice{},
		SoundZone: &RemoteSoundZone{},
		Location:  &RemoteLocation{},
		Account:   &RemoteAccount{},
	}
	err = json.NewDecoder(response.Body).Decode(&result)
	return
}
Пример #25
0
func CountSoundZonesForSchedule(c ServiceConnector, schedule key.Key, token AccessToken) (result int, err error) {
	request, response, err := DoRequest(c, "GET", c.GetAuthService(), fmt.Sprintf("schedules/%v/sound_zone_count", schedule.Encode()), token, nil)
	if err != nil {
		return
	}
	if response.StatusCode != 200 {
		err = errorFor(request, response)
		return
	}

	container := &CountContainer{}
	if err = json.NewDecoder(response.Body).Decode(container); err != nil {
		return
	}

	result = container.Count

	return
}