1
0
Fork 0

Updates of Lego.

This commit is contained in:
Ludovic Fernandez 2019-02-11 08:52:03 +01:00 committed by Traefiker Bot
parent 5f4d440493
commit 2b2cfdfb32
102 changed files with 8355 additions and 902 deletions

View file

@ -6,8 +6,11 @@ import (
"errors"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
@ -16,7 +19,7 @@ import (
)
var (
libraryVersion = "0.6.0"
libraryVersion = "0.6.2"
// UserAgent is the User-Agent value sent for all requests
UserAgent = "Akamai-Open-Edgegrid-golang/" + libraryVersion + " golang/" + strings.TrimPrefix(runtime.Version(), "go")
// Client is the *http.Client to use
@ -61,13 +64,21 @@ func NewRequest(config edgegrid.Config, method, path string, body io.Reader) (*h
// NewJSONRequest creates an HTTP request that can be sent to the Akamai APIs with a JSON body
// The JSON body is encoded and the Content-Type/Accept headers are set automatically.
func NewJSONRequest(config edgegrid.Config, method, path string, body interface{}) (*http.Request, error) {
jsonBody, err := jsonhooks.Marshal(body)
if err != nil {
return nil, err
var req *http.Request
var err error
if body != nil {
jsonBody, err := jsonhooks.Marshal(body)
if err != nil {
return nil, err
}
buf := bytes.NewReader(jsonBody)
req, err = NewRequest(config, method, path, buf)
} else {
req, err = NewRequest(config, method, path, nil)
}
buf := bytes.NewReader(jsonBody)
req, err := NewRequest(config, method, path, buf)
if err != nil {
return nil, err
}
@ -78,6 +89,36 @@ func NewJSONRequest(config edgegrid.Config, method, path string, body interface{
return req, nil
}
// NewMultiPartFormDataRequest creates an HTTP request that uploads a file to the Akamai API
func NewMultiPartFormDataRequest(config edgegrid.Config, uriPath, filePath string, otherFormParams map[string]string) (*http.Request, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// TODO: make this field name configurable
part, err := writer.CreateFormFile("importFile", filepath.Base(filePath))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range otherFormParams {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := NewRequest(config, "POST", uriPath, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}
// Do performs a given HTTP Request, signed with the Akamai OPEN Edgegrid
// Authorization header. An edgegrid.Response or an error is returned.
func Do(config edgegrid.Config, req *http.Request) (*http.Response, error) {

View file

@ -12,22 +12,42 @@ import (
// APIError exposes an Akamai OPEN Edgegrid Error
type APIError struct {
error
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
Instance string `json:"instance"`
Method string `json:"method"`
ServerIP string `json:"serverIp"`
ClientIP string `json:"clientIp"`
RequestID string `json:"requestId"`
RequestTime string `json:"requestTime"`
Response *http.Response `json:"-"`
RawBody string `json:"-"`
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
Errors []APIErrorDetail `json:"errors"`
Problems []APIErrorDetail `json:"problems"`
Instance string `json:"instance"`
Method string `json:"method"`
ServerIP string `json:"serverIp"`
ClientIP string `json:"clientIp"`
RequestID string `json:"requestId"`
RequestTime string `json:"requestTime"`
Response *http.Response `json:"-"`
RawBody string `json:"-"`
}
type APIErrorDetail struct {
Type string `json:"type"`
Title string `json:"title"`
Detail string `json:"detail"`
RejectedValue string `json:"rejectedValue"`
}
func (error APIError) Error() string {
return strings.TrimSpace(fmt.Sprintf("API Error: %d %s %s More Info %s", error.Status, error.Title, error.Detail, error.Type))
var errorDetails string
if len(error.Errors) > 0 {
for _, e := range error.Errors {
errorDetails = fmt.Sprintf("%s \n %s", errorDetails, e)
}
}
if len(error.Problems) > 0 {
for _, e := range error.Problems {
errorDetails = fmt.Sprintf("%s \n %s", errorDetails, e)
}
}
return strings.TrimSpace(fmt.Sprintf("API Error: %d %s %s More Info %s\n %s", error.Status, error.Title, error.Detail, error.Type, errorDetails))
}
// NewAPIError creates a new API error based on a Response,
@ -45,7 +65,6 @@ func NewAPIError(response *http.Response) APIError {
// other purposes.
func NewAPIErrorFromBody(response *http.Response, body []byte) APIError {
error := APIError{}
if err := jsonhooks.Unmarshal(body, &error); err == nil {
error.Status = response.StatusCode
error.Title = response.Status