2023-04-26 16:51:13 -04:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-04-28 15:57:09 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-04-26 16:51:13 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-04-28 16:52:16 -04:00
|
|
|
"io"
|
2023-04-26 16:51:13 -04:00
|
|
|
"io/ioutil"
|
2023-04-28 15:57:09 -04:00
|
|
|
"mime/multipart"
|
2023-04-26 16:51:13 -04:00
|
|
|
"net/http"
|
2023-07-14 20:43:40 -04:00
|
|
|
"net/url"
|
2023-07-14 20:14:15 -04:00
|
|
|
|
|
|
|
"git.agecem.com/agecem/agecem-org/config"
|
|
|
|
"github.com/spf13/viper"
|
2023-04-26 16:51:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
|
|
|
Protocol string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Opts APIOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type APIOptions struct {
|
2023-04-28 15:57:09 -04:00
|
|
|
KeyAuth bool
|
|
|
|
Key string
|
|
|
|
BasicAuth bool
|
|
|
|
Username string
|
|
|
|
Password string
|
2023-04-26 16:51:13 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 19:30:31 -04:00
|
|
|
type UploadDocumentResponse struct {
|
|
|
|
Info UploadDocumentResponseInfo `json:"info"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UploadDocumentResponseInfo struct {
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
Object string `json:"key"`
|
|
|
|
Size float64 `json:"size"`
|
|
|
|
}
|
|
|
|
|
2023-08-19 14:45:07 -04:00
|
|
|
// NewApiClientFromViper returns a pointer to a new API object,
|
|
|
|
// provided the configuration options are managed by
|
|
|
|
// https://git.agecem.com/agecem/agecem-org/config
|
2023-07-14 20:14:15 -04:00
|
|
|
func NewApiClientFromViper() (*API, error) {
|
|
|
|
var config config.Config
|
|
|
|
|
|
|
|
if err := viper.Unmarshal(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
api, err := New(config.Server.Api.Protocol, config.Server.Api.Host, config.Server.Api.Port, APIOptions{
|
|
|
|
KeyAuth: config.Server.Api.Auth,
|
|
|
|
Key: config.Server.Api.Key,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return api, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api, nil
|
|
|
|
}
|
|
|
|
|
2023-04-26 16:51:13 -04:00
|
|
|
func New(protocol, host string, port int, opts APIOptions) (*API, error) {
|
|
|
|
api := API{
|
|
|
|
Protocol: protocol,
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
Opts: opts,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call returns a []byte representing a response body.
|
|
|
|
// Can be used for GET or DELETE methods
|
|
|
|
func (a *API) Call(method, route string) ([]byte, error) {
|
|
|
|
endpoint := fmt.Sprintf("%s://%s:%d",
|
|
|
|
a.Protocol,
|
|
|
|
a.Host,
|
|
|
|
a.Port,
|
|
|
|
)
|
2023-07-14 20:43:40 -04:00
|
|
|
prerequest := fmt.Sprintf("%s%s", endpoint, route)
|
|
|
|
request, err := url.QueryUnescape(prerequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-26 16:51:13 -04:00
|
|
|
|
|
|
|
switch method {
|
|
|
|
case http.MethodGet:
|
|
|
|
// Create client
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
// Create request
|
|
|
|
request, err := http.NewRequest(http.MethodGet, request, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Opts.KeyAuth {
|
|
|
|
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Opts.Key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch Request
|
|
|
|
response, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
case http.MethodDelete:
|
|
|
|
// Create client
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
// Create request
|
|
|
|
req, err := http.NewRequest(http.MethodDelete, request, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Opts.KeyAuth {
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Opts.Key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch Request
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Read Response Body
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return respBody, nil
|
|
|
|
}
|
2023-04-28 15:57:09 -04:00
|
|
|
|
|
|
|
//return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
|
2023-04-26 16:51:13 -04:00
|
|
|
return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
|
|
|
|
}
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 19:30:31 -04:00
|
|
|
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (UploadDocumentResponse, error) {
|
2023-08-19 15:35:56 -04:00
|
|
|
var response UploadDocumentResponse
|
2023-04-28 15:57:09 -04:00
|
|
|
endpoint := fmt.Sprintf("%s://%s:%d",
|
|
|
|
a.Protocol,
|
|
|
|
a.Host,
|
|
|
|
a.Port,
|
|
|
|
)
|
|
|
|
|
2023-07-14 20:43:40 -04:00
|
|
|
current_url := fmt.Sprintf("%s/v1/bucket/%s", endpoint, bucket)
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
// Create a new multipart writer
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(body)
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
// Add the file to the request
|
|
|
|
file, err := file_header.Open()
|
2023-04-28 15:57:09 -04:00
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
2023-04-28 16:52:16 -04:00
|
|
|
defer file.Close()
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-07-14 20:43:40 -04:00
|
|
|
filename_processed, err := url.QueryUnescape(file_header.Filename)
|
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err)
|
2023-07-14 20:43:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
part, err := writer.CreateFormFile("document", filename_processed)
|
2023-04-28 16:52:16 -04:00
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
_, err = io.Copy(part, file)
|
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#io.Copy: %s", err)
|
2023-04-28 16:52:16 -04:00
|
|
|
}
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#writer.Close: %s", err)
|
2023-04-28 16:52:16 -04:00
|
|
|
}
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
// Create a new HTTP request with the multipart body
|
2023-07-14 20:43:40 -04:00
|
|
|
req, err := http.NewRequest(http.MethodPost, current_url, body)
|
2023-04-28 15:57:09 -04:00
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
if a.Opts.KeyAuth {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.Opts.Key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the HTTP request
|
|
|
|
client := &http.Client{}
|
2023-04-28 15:57:09 -04:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#client.Do: %s", err)
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
2023-04-28 16:52:16 -04:00
|
|
|
defer resp.Body.Close()
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-08-19 15:35:56 -04:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
return response, err
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|