Skip to content

Commit

Permalink
Merge pull request #56 from ctreminiom/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ctreminiom authored Jul 18, 2021
2 parents 1d18342 + a6e4a05 commit 17613e6
Show file tree
Hide file tree
Showing 420 changed files with 9,401 additions and 7,607 deletions.
139 changes: 55 additions & 84 deletions admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,27 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"reflect"
)

type Client struct {
HTTP *http.Client
Site *url.URL

HTTP *http.Client
Site *url.URL
Auth *AuthenticationService
Organization *OrganizationService
User *UserService

SCIM *SCIMService
SCIM *SCIMService
}

const ApiEndpoint = "https://api.atlassian.com/"

//New
func New(httpClient *http.Client) (client *Client, err error) {

if httpClient == nil {
httpClient = http.DefaultClient
}

siteAsURL, err := url.Parse(ApiEndpoint)
if err != nil {
return
}
siteAsURL, _ := url.Parse(ApiEndpoint)

client = &Client{}
client.HTTP = httpClient
Expand Down Expand Up @@ -65,31 +59,18 @@ func New(httpClient *http.Client) (client *Client, err error) {
return
}

func (c *Client) newRequest(ctx context.Context, method, urlAsString string, payload interface{}) (request *http.Request, err error) {
func (c *Client) newRequest(ctx context.Context, method, apiEndpoint string, payload io.Reader) (request *http.Request, err error) {

if ctx == nil {
return nil, errors.New("the context param is nil, please provide a valid one")
}

relativePath, err := url.Parse(urlAsString)
relativePath, err := url.Parse(apiEndpoint)
if err != nil {
return
return nil, fmt.Errorf(urlParsedError, err.Error())
}

relativePath.Path = strings.TrimLeft(relativePath.Path, "/")

endpointPath := c.Site.ResolveReference(relativePath)
var payloadBuffer io.ReadWriter
if payload != nil {
payloadBuffer = new(bytes.Buffer)
if err = json.NewEncoder(payloadBuffer).Encode(payload); err != nil {
return
}
}
var endpoint = c.Site.ResolveReference(relativePath).String()

request, err = http.NewRequestWithContext(ctx, method, endpointPath.String(), payloadBuffer)
request, err = http.NewRequestWithContext(ctx, method, endpoint, payload)
if err != nil {
return
return nil, fmt.Errorf(requestCreationError, err.Error())
}

if c.Auth.beaverToken != "" {
Expand All @@ -103,79 +84,69 @@ func (c *Client) newRequest(ctx context.Context, method, urlAsString string, pay
return
}

func (c *Client) Do(request *http.Request) (response *Response, err error) {
func (c *Client) call(request *http.Request, structure interface{}) (result *ResponseScheme, err error) {
response, _ := c.HTTP.Do(request)
return transformTheHTTPResponse(response, structure)
}

httpResponse, err := c.HTTP.Do(request)
if err != nil {
return
}
func transformStructToReader(structure interface{}) (reader io.Reader, err error) {

response, err = checkResponse(httpResponse, request.URL.String())
if err != nil {
return
if structure == nil || reflect.ValueOf(structure).IsNil() {
return nil, structureNotParsedError
}

response, err = newResponse(httpResponse, request.URL.String())
structureAsBodyBytes, err := json.Marshal(structure)
if err != nil {
return
return nil, err
}

return
return bytes.NewReader(structureAsBodyBytes), nil
}

type Response struct {
StatusCode int
Endpoint string
Headers map[string][]string
BodyAsBytes []byte
Method string
}

func newResponse(http *http.Response, endpoint string) (response *Response, err error) {
func transformTheHTTPResponse(response *http.Response, structure interface{}) (result *ResponseScheme, err error) {

var statusCode = http.StatusCode

var httpResponseAsBytes []byte
if http.ContentLength != 0 {
httpResponseAsBytes, err = ioutil.ReadAll(http.Body)
if err != nil {
return
}
if response == nil {
return nil, errors.New("validation failed, please provide a http.Response pointer")
}

newResponse := Response{
StatusCode: statusCode,
Headers: http.Header,
BodyAsBytes: httpResponseAsBytes,
Endpoint: endpoint,
Method: http.Request.Method,
}
responseTransformed := &ResponseScheme{}
responseTransformed.Code = response.StatusCode
responseTransformed.Endpoint = response.Request.URL.String()
responseTransformed.Method = response.Request.Method

return &newResponse, nil
}
var wasSuccess = response.StatusCode >= 200 && response.StatusCode < 300
if !wasSuccess {

func checkResponse(http *http.Response, endpoint string) (response *Response, err error) {
return responseTransformed, fmt.Errorf(requestFailedError, response.StatusCode)
}

var statusCode = http.StatusCode
if 200 <= statusCode && statusCode <= 299 {
return
responseAsBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return responseTransformed, err
}

var httpResponseAsBytes []byte
if http.ContentLength != 0 {
httpResponseAsBytes, err = ioutil.ReadAll(http.Body)
if err != nil {
return
if structure != nil {
if err = json.Unmarshal(responseAsBytes, &structure); err != nil {
return responseTransformed, err
}
}

newErrorResponse := Response{
StatusCode: statusCode,
Headers: http.Header,
BodyAsBytes: httpResponseAsBytes,
Endpoint: endpoint,
Method: http.Request.Method,
}
responseTransformed.Bytes.Write(responseAsBytes)

return responseTransformed, nil
}

return &newErrorResponse, fmt.Errorf("request failed. Please analyze the request body for more details. Status Code: %d", statusCode)
type ResponseScheme struct {
Code int
Endpoint string
Method string
Bytes bytes.Buffer
Headers map[string][]string
}

var (
requestCreationError = "request creation failed: %v"
urlParsedError = "URL parsing failed: %v"
requestFailedError = "request failed. Please analyze the request body for more details. Status Code: %d"
structureNotParsedError = errors.New("failed to parse the interface pointer, please provide a valid one")
)
Loading

0 comments on commit 17613e6

Please sign in to comment.