Skip to content

Go client library for the CloudKit Web Services API.

License

Notifications You must be signed in to change notification settings

lukasmalkmus/icloud-go

Repository files navigation

iCloud Go

Go Reference Go Workflow Coverage Status Go Report Latest Release License


Table of Contents

  1. Introduction
  2. Installation
  3. Authentication
  4. Usage
  5. Contributing
  6. License

Introduction

iCloud Go is a Go client library for using the CloudKit Web Services API. While the foundation of this package is powerfull enough to support the whole API, its main purpose is to provide enough features to create records in CloudKit. This serves my personal purpose of using this package for building various kinds of importers.

Installation

Install using go get

$ go get github.com/lukasmalkmus/icloud-go/icloud

Install from source

$ git clone https://github.com/lukasmalkmus/icloud-go.git
$ cd icloud-go
$ make

Authentication

This package only supports Server-to-Server authentication.

You can create a keypair using openssl:

$ openssl ecparam -name prime256v1 -genkey -noout -out eckey.pem

To get the public key (to enter it on iCloud Dashboard):

$ openssl ec -in eckey.pem -pubout

To get the private key (used by the client to sign requests):

$ openssl ec -in eckey.pem

Usage

import "github.com/lukasmalkmus/icloud-go/icloud"

var (
	keyID         = os.Getenv("ICLOUD_KEY_ID")
	container     = os.Getenv("ICLOUD_CONTAINER")
	rawPrivateKey = os.Getenv("ICLOUD_PRIVATE_KEY")
)

// 1. Parse the private key.
privateKey, err := x509.ParseECPrivateKey([]byte(rawPrivateKey))
if err != nil {
	log.Fatal(err)
}

// 2. Create the iCloud client.
client, err := icloud.NewClient(container, keyID, privateKey, icloud.Development)
if err != nil {
	log.Fatal(err)
}

// 3. Create a record.
if _, err = client.Records.Modify(context.Background(), icloud.Public, icloud.RecordsRequest{
	Operations: []icloud.RecordOperation{
		{
			Type: icloud.Create,
			Record: icloud.Record{
				Type: "MyRecord",
				Fields: icloud.Fields{
					{
						Name:  "MyField",
						Value: "Hello, World!",
					},
					{
						Name:  "MyOtherField",
						Value: 1000,
					},
				},
			},
		},
	},
}); err != nil {
	log.Fatal(err)
}

Contributing

Feel free to submit PRs or to fill issues. Every kind of help is appreciated.

Before committing, make should run without any issues.

License

© Lukas Malkmus, 2021

Distributed under MIT License (The MIT License).

See LICENSE for more information.

License Status