Search…
Fluent API
πŸ„ Fluent, chainable, IntelliSense powered syntax to master SharePoint API
Provides a simple way of constructing API endpoint calls with IntelliSense and chainable syntax.

Usage sample

1
package main
2
​
3
import (
4
"encoding/json"
5
"fmt"
6
"log"
7
​
8
"github.com/koltyakov/gosip"
9
"github.com/koltyakov/gosip/api"
10
strategy "github.com/koltyakov/gosip/auth/addin"
11
)
12
​
13
func main() {
14
// Getting auth params and client
15
client, err := getAuthClient()
16
if err != nil {
17
log.Fatalln(err)
18
}
19
​
20
// Binding SharePoint API
21
sp := api.NewSP(client)
22
​
23
// Custom headers (optional)
24
headers := map[string]string{
25
"Accept": "application/json;odata=minimalmetadata",
26
"Accept-Language": "de-DE,de;q=0.9",
27
}
28
config := &api.RequestConfig{Headers: headers}
29
​
30
// Chainable request sample
31
data, err := sp.Conf(config).Web().Lists().Select("Id,Title").Get()
32
if err != nil {
33
log.Fatalln(err)
34
}
35
​
36
// Response object unmarshalling
37
// struct depends on OData mode and API method
38
res := &struct {
39
Value []struct {
40
ID string `json:"Id"`
41
Title string `json:"Title"`
42
} `json:"value"`
43
}{}
44
​
45
if err := json.Unmarshal(data, &res); err != nil {
46
log.Fatalf("unable to parse the response: %v", err)
47
}
48
​
49
for _, list := range res.Value {
50
fmt.Printf("%+v\n", list)
51
}
52
}
53
​
54
func getAuthClient() (*gosip.SPClient, error) {
55
configPath := "./config/private.spo-addin.json"
56
auth := &strategy.AuthCnfg{}
57
if err := auth.ReadConfig(configPath); err != nil {
58
return nil, fmt.Errorf("unable to get config: %v", err)
59
}
60
return &gosip.SPClient{AuthCnfg: auth}, nil
61
}
Copied!

Main concepts

  • Get authenticated
  • Construct root SP object using api.NewSP(client)
  • Construct API calls in a fluent way
  • Parse responses in the Go way
  • Embrase strongly typed generic responses
  • Build awesome apps in Go for SharePoint
Last modified 1yr ago
Export as PDF
Copy link