-
Notifications
You must be signed in to change notification settings - Fork 92
/
join.go
49 lines (41 loc) · 1.06 KB
/
join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package osnadmin
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"mime/multipart"
"net/http"
)
// Joins an OSN to a new or existing channel.
func Join(osnURL string, blockBytes []byte, caCertPool *x509.CertPool, tlsClientCert tls.Certificate) (*http.Response, error) {
url := fmt.Sprintf("%s/participation/v1/channels", osnURL)
req, err := createJoinRequest(url, blockBytes)
if err != nil {
return nil, err
}
return httpDo(req, caCertPool, tlsClientCert)
}
func createJoinRequest(url string, blockBytes []byte) (*http.Request, error) {
joinBody := new(bytes.Buffer)
writer := multipart.NewWriter(joinBody)
part, err := writer.CreateFormFile("config-block", "config.block")
if err != nil {
return nil, err
}
part.Write(blockBytes)
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, url, joinBody)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, nil
}