/*
Anchore Engine API Server

This is the Anchore Engine API. Provides the primary external API for users of the service.

API version: 0.1.20
Contact: nurmi@anchore.com
*/

// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

package anchore

import (
	"bytes"
	"context"
	"io/ioutil"
	"net/http"
	"net/url"
)


// RepositoryCredentialsApiService RepositoryCredentialsApi service
type RepositoryCredentialsApiService service

type ApiAddRepositoryRequest struct {
	ctx context.Context
	ApiService *RepositoryCredentialsApiService
	repository *string
	autosubscribe *bool
	dryrun *bool
	xAnchoreAccount *string
}

// full repository to add e.g. docker.io/library/alpine
func (r ApiAddRepositoryRequest) Repository(repository string) ApiAddRepositoryRequest {
	r.repository = &repository
	return r
}

// flag to enable/disable auto tag_update activation when new images from a repo are added
func (r ApiAddRepositoryRequest) Autosubscribe(autosubscribe bool) ApiAddRepositoryRequest {
	r.autosubscribe = &autosubscribe
	return r
}

// flag to return tags in the repository without actually watching the repository, default is false
func (r ApiAddRepositoryRequest) Dryrun(dryrun bool) ApiAddRepositoryRequest {
	r.dryrun = &dryrun
	return r
}

// An account name to change the resource scope of the request to that account, if permissions allow (admin only)
func (r ApiAddRepositoryRequest) XAnchoreAccount(xAnchoreAccount string) ApiAddRepositoryRequest {
	r.xAnchoreAccount = &xAnchoreAccount
	return r
}

func (r ApiAddRepositoryRequest) Execute() ([]Subscription, *http.Response, error) {
	return r.ApiService.AddRepositoryExecute(r)
}

/*
AddRepository Add repository to watch

 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @return ApiAddRepositoryRequest
*/
func (a *RepositoryCredentialsApiService) AddRepository(ctx context.Context) ApiAddRepositoryRequest {
	return ApiAddRepositoryRequest{
		ApiService: a,
		ctx: ctx,
	}
}

// Execute executes the request
//  @return []Subscription
func (a *RepositoryCredentialsApiService) AddRepositoryExecute(r ApiAddRepositoryRequest) ([]Subscription, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodPost
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  []Subscription
	)

	localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "RepositoryCredentialsApiService.AddRepository")
	if err != nil {
		return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
	}

	localVarPath := localBasePath + "/repositories"

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}
	if r.repository == nil {
		return localVarReturnValue, nil, reportError("repository is required and must be specified")
	}

	localVarQueryParams.Add("repository", parameterToString(*r.repository, ""))
	if r.autosubscribe != nil {
		localVarQueryParams.Add("autosubscribe", parameterToString(*r.autosubscribe, ""))
	}
	if r.dryrun != nil {
		localVarQueryParams.Add("dryrun", parameterToString(*r.dryrun, ""))
	}
	// to determine the Content-Type header
	localVarHTTPContentTypes := []string{}

	// set Content-Type header
	localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
	if localVarHTTPContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHTTPContentType
	}

	// to determine the Accept header
	localVarHTTPHeaderAccepts := []string{"application/json"}

	// set Accept header
	localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
	if localVarHTTPHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
	}
	if r.xAnchoreAccount != nil {
		localVarHeaderParams["x-anchore-account"] = parameterToString(*r.xAnchoreAccount, "")
	}
	req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHTTPResponse, err := a.client.callAPI(req)
	if err != nil || localVarHTTPResponse == nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}

	localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body)
	localVarHTTPResponse.Body.Close()
	localVarHTTPResponse.Body = ioutil.NopCloser(bytes.NewBuffer(localVarBody))
	if err != nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}

	if localVarHTTPResponse.StatusCode >= 300 {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: localVarHTTPResponse.Status,
		}
		return localVarReturnValue, localVarHTTPResponse, newErr
	}

	err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
	if err != nil {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: err.Error(),
		}
		return localVarReturnValue, localVarHTTPResponse, newErr
	}

	return localVarReturnValue, localVarHTTPResponse, nil
}
