WIP: Connecter /admin/documents/upload à handleV1DocumentCreate
This commit is contained in:
parent
fd3eebb68c
commit
07005c8753
3 changed files with 188 additions and 8 deletions
139
api/api.go
139
api/api.go
|
@ -1,9 +1,13 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -15,8 +19,11 @@ type API struct {
|
|||
}
|
||||
|
||||
type APIOptions struct {
|
||||
KeyAuth bool
|
||||
Key string
|
||||
KeyAuth bool
|
||||
Key string
|
||||
BasicAuth bool
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func New(protocol, host string, port int, opts APIOptions) (*API, error) {
|
||||
|
@ -97,5 +104,133 @@ func (a *API) Call(method, route string) ([]byte, error) {
|
|||
}
|
||||
return respBody, nil
|
||||
}
|
||||
|
||||
//return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
|
||||
return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
|
||||
}
|
||||
|
||||
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (string, error) {
|
||||
endpoint := fmt.Sprintf("%s://%s:%d",
|
||||
a.Protocol,
|
||||
a.Host,
|
||||
a.Port,
|
||||
)
|
||||
|
||||
route := fmt.Sprintf("/v1/bucket/%s", bucket)
|
||||
|
||||
request := fmt.Sprintf("%s%s", endpoint, route)
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
// set the HTTP method, url, and request body
|
||||
req, err := http.NewRequest(http.MethodPost, request, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if a.Opts.KeyAuth {
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Opts.Key))
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", "multipart/form-data")
|
||||
|
||||
// CreateFormFile with multipart.NewWriter
|
||||
//TODO
|
||||
/*
|
||||
buf := new(bytes.Buffer)
|
||||
form := multipart.NewWriter(buf)
|
||||
form.CreateFormFile("document", )
|
||||
*/
|
||||
|
||||
//TODO
|
||||
log.Println("req: ", req)
|
||||
|
||||
file, file_header, err := req.FormFile("document")
|
||||
if err != nil {
|
||||
log.Println("Error during http#Request.Formfile")
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Println("file: ", file, "file_header: ", file_header)
|
||||
log.Println("file_header: ", file_header)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Println("Error during http#Client.Do")
|
||||
return "", err
|
||||
}
|
||||
|
||||
var res map[string]interface{}
|
||||
|
||||
json.NewDecoder(resp.Body).Decode(&res)
|
||||
return fmt.Sprintf("%s", res), nil
|
||||
}
|
||||
|
||||
// CallWithData takes data and returns a string representing a response body.
|
||||
// Can be used for POST or PUT methods
|
||||
func (a *API) CallWithData(method, route string, data []byte) (string, error) {
|
||||
endpoint := fmt.Sprintf("%s://%s:%d",
|
||||
a.Protocol,
|
||||
a.Host,
|
||||
a.Port,
|
||||
)
|
||||
request := fmt.Sprintf("%s%s", endpoint, route)
|
||||
|
||||
switch method {
|
||||
case http.MethodPost:
|
||||
// initialize http client
|
||||
client := &http.Client{}
|
||||
|
||||
// set the HTTP method, url, and request body
|
||||
req, err := http.NewRequest(http.MethodPost, request, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if a.Opts.KeyAuth {
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Opts.Key))
|
||||
}
|
||||
|
||||
// set the request header Content-Type for json
|
||||
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var res map[string]interface{}
|
||||
|
||||
json.NewDecoder(resp.Body).Decode(&res)
|
||||
return fmt.Sprintf("%s\n", res["message"]), nil
|
||||
/*
|
||||
case http.MethodPut:
|
||||
// initialize http client
|
||||
client := &http.Client{}
|
||||
|
||||
// set the HTTP method, url, and request body
|
||||
req, err := http.NewRequest(http.MethodPut, request, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if a.Opts.KeyAuth {
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Opts.Key))
|
||||
}
|
||||
|
||||
// set the request header Content-Type for json
|
||||
//req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var res map[string]interface{}
|
||||
|
||||
json.NewDecoder(resp.Body).Decode(&res)
|
||||
return fmt.Sprintf("%s\n", res["message"]), nil
|
||||
*/
|
||||
}
|
||||
|
||||
//return "", errors.New(fmt.Sprintf("method must be 'POST' or 'PUT', got '%s'", method))
|
||||
return "", errors.New(fmt.Sprintf("method must be 'POST', got '%s'", method))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue