// Code generated by go-swagger; DO NOT EDIT.

package operations

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"strconv"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/runtime"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag"

	"github.com/fiskerinc/cloud-services/pkg/ota_api/models"
)

// GetApitokensReader is a Reader for the GetApitokens structure.
type GetApitokensReader struct {
	formats strfmt.Registry
}

// ReadResponse reads a server response into the received o.
func (o *GetApitokensReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
	switch response.Code() {
	case 200:
		result := NewGetApitokensOK()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return result, nil
	case 400:
		result := NewGetApitokensBadRequest()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result
	case 401:
		result := NewGetApitokensUnauthorized()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result
	case 503:
		result := NewGetApitokensServiceUnavailable()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result
	default:
		return nil, runtime.NewAPIError("[GET /apitokens] GetApitokens", response, response.Code())
	}
}

// NewGetApitokensOK creates a GetApitokensOK with default headers values
func NewGetApitokensOK() *GetApitokensOK {
	return &GetApitokensOK{}
}

/*
GetApitokensOK describes a response with status code 200, with default header values.

OK
*/
type GetApitokensOK struct {
	Payload *GetApitokensOKBody
}

// IsSuccess returns true when this get apitokens o k response has a 2xx status code
func (o *GetApitokensOK) IsSuccess() bool {
	return true
}

// IsRedirect returns true when this get apitokens o k response has a 3xx status code
func (o *GetApitokensOK) IsRedirect() bool {
	return false
}

// IsClientError returns true when this get apitokens o k response has a 4xx status code
func (o *GetApitokensOK) IsClientError() bool {
	return false
}

// IsServerError returns true when this get apitokens o k response has a 5xx status code
func (o *GetApitokensOK) IsServerError() bool {
	return false
}

// IsCode returns true when this get apitokens o k response a status code equal to that given
func (o *GetApitokensOK) IsCode(code int) bool {
	return code == 200
}

// Code gets the status code for the get apitokens o k response
func (o *GetApitokensOK) Code() int {
	return 200
}

func (o *GetApitokensOK) Error() string {
	payload, _ := json.Marshal(o.Payload)
	return fmt.Sprintf("[GET /apitokens][%d] getApitokensOK %s", 200, payload)
}

func (o *GetApitokensOK) String() string {
	payload, _ := json.Marshal(o.Payload)
	return fmt.Sprintf("[GET /apitokens][%d] getApitokensOK %s", 200, payload)
}

func (o *GetApitokensOK) GetPayload() *GetApitokensOKBody {
	return o.Payload
}

func (o *GetApitokensOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	o.Payload = new(GetApitokensOKBody)

	// response payload
	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}

	return nil
}

// NewGetApitokensBadRequest creates a GetApitokensBadRequest with default headers values
func NewGetApitokensBadRequest() *GetApitokensBadRequest {
	return &GetApitokensBadRequest{}
}

/*
GetApitokensBadRequest describes a response with status code 400, with default header values.

Bad request
*/
type GetApitokensBadRequest struct {
	Payload *models.CommonJSONError
}

// IsSuccess returns true when this get apitokens bad request response has a 2xx status code
func (o *GetApitokensBadRequest) IsSuccess() bool {
	return false
}

// IsRedirect returns true when this get apitokens bad request response has a 3xx status code
func (o *GetApitokensBadRequest) IsRedirect() bool {
	return false
}

// IsClientError returns true when this get apitokens bad request response has a 4xx status code
func (o *GetApitokensBadRequest) IsClientError() bool {
	return true
}

// IsServerError returns true when this get apitokens bad request response has a 5xx status code
func (o *GetApitokensBadRequest) IsServerError() bool {
	return false
}

// IsCode returns true when this get apitokens bad request response a status code equal to that given
func (o *GetApitokensBadRequest) IsCode(code int) bool {
	return code == 400
}

// Code gets the status code for the get apitokens bad request response
func (o *GetApitokensBadRequest) Code() int {
	return 400
}

func (o *GetApitokensBadRequest) Error() string {
	payload, _ := json.Marshal(o.Payload)
	return fmt.Sprintf("[GET /apitokens][%d] getApitokensBadRequest %s", 400, payload)
}

func (o *GetApitokensBadRequest) String() string {
	payload, _ := json.Marshal(o.Payload)
	return fmt.Sprintf("[GET /apitokens][%d] getApitokensBadRequest %s", 400, payload)
}

func (o *GetApitokensBadRequest) GetPayload() *models.CommonJSONError {
	return o.Payload
}

func (o *GetApitokensBadRequest) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	o.Payload = new(models.CommonJSONError)

	// response payload
	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}

	return nil
}

// NewGetApitokensUnauthorized creates a GetApitokensUnauthorized with default headers values
func NewGetApitokensUnauthorized() *GetApitokensUnauthorized {
	return &GetApitokensUnauthorized{}
}

/*
GetApitokensUnauthorized describes a response with status code 401, with default header values.

Unauthorized
*/
type GetApitokensUnauthorized struct {
	Payload *models.CommonJSONError
}

// IsSuccess returns true when this get apitokens unauthorized response has a 2xx status code
func (o *GetApitokensUnauthorized) IsSuccess() bool {
	return false
}

// IsRedirect returns true when this get apitokens unauthorized response has a 3xx status code
func (o *GetApitokensUnauthorized) IsRedirect() bool {
	return false
}

// IsClientError returns true when this get apitokens unauthorized response has a 4xx status code
func (o *GetApitokensUnauthorized) IsClientError() bool {
	return true
}

// IsServerError returns true when this get apitokens unauthorized response has a 5xx status code
func (o *GetApitokensUnauthorized) IsServerError() bool {
	return false
}

// IsCode returns true when this get apitokens unauthorized response a status code equal to that given
func (o *GetApitokensUnauthorized) IsCode(code int) bool {
	return code == 401
}

// Code gets the status code for the get apitokens unauthorized response
func (o *GetApitokensUnauthorized) Code() int {
	return 401
}

func (o *GetApitokensUnauthorized) Error() string {
	payload, _ := json.Marshal(o.Payload)
	return fmt.Sprintf("[GET /apitokens][%d] getApitokensUnauthorized %s", 401, payload)
}

func (o *GetApitokensUnauthorized) String() string {
	payload, _ := json.Marshal(o.Payload)
	return fmt.Sprintf("[GET /apitokens][%d] getApitokensUnauthorized %s", 401, payload)
}

func (o *GetApitokensUnauthorized) GetPayload() *models.CommonJSONError {
	return o.Payload
}

func (o *GetApitokensUnauthorized) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	o.Payload = new(models.CommonJSONError)

	// response payload
	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}

	return nil
}

// NewGetApitokensServiceUnavailable creates a GetApitokensServiceUnavailable with default headers values
func NewGetApitokensServiceUnavailable() *GetApitokensServiceUnavailable {
	return &GetApitokensServiceUnavailable{}
}

/*
GetApitokensServiceUnavailable describes a response with status code 503, with default header values.

Service unavailable
*/
type GetApitokensServiceUnavailable struct {
	Payload *models.CommonJSONError
}

// IsSuccess returns true when this get apitokens service unavailable response has a 2xx status code
func (o *GetApitokensServiceUnavailable) IsSuccess() bool {
	return false
}

// IsRedirect returns true when this get apitokens service unavailable response has a 3xx status code
func (o *GetApitokensServiceUnavailable) IsRedirect() bool {
	return false
}

// IsClientError returns true when this get apitokens service unavailable response has a 4xx status code
func (o *GetApitokensServiceUnavailable) IsClientError() bool {
	return false
}

// IsServerError returns true when this get apitokens service unavailable response has a 5xx status code
func (o *GetApitokensServiceUnavailable) IsServerError() bool {
	return true
}

// IsCode returns true when this get apitokens service unavailable response a status code equal to that given
func (o *GetApitokensServiceUnavailable) IsCode(code int) bool {
	return code == 503
}

// Code gets the status code for the get apitokens service unavailable response
func (o *GetApitokensServiceUnavailable) Code() int {
	return 503
}

func (o *GetApitokensServiceUnavailable) Error() string {
	payload, _ := json.Marshal(o.Payload)
	return fmt.Sprintf("[GET /apitokens][%d] getApitokensServiceUnavailable %s", 503, payload)
}

func (o *GetApitokensServiceUnavailable) String() string {
	payload, _ := json.Marshal(o.Payload)
	return fmt.Sprintf("[GET /apitokens][%d] getApitokensServiceUnavailable %s", 503, payload)
}

func (o *GetApitokensServiceUnavailable) GetPayload() *models.CommonJSONError {
	return o.Payload
}

func (o *GetApitokensServiceUnavailable) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	o.Payload = new(models.CommonJSONError)

	// response payload
	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}

	return nil
}

/*
GetApitokensOKBody get apitokens o k body
swagger:model GetApitokensOKBody
*/
type GetApitokensOKBody struct {
	models.CommonJSONDBQueryResult

	// data
	Data []*models.CommonAPIToken `json:"data"`
}

// UnmarshalJSON unmarshals this object from a JSON structure
func (o *GetApitokensOKBody) UnmarshalJSON(raw []byte) error {
	// GetApitokensOKBodyAO0
	var getApitokensOKBodyAO0 models.CommonJSONDBQueryResult
	if err := swag.ReadJSON(raw, &getApitokensOKBodyAO0); err != nil {
		return err
	}
	o.CommonJSONDBQueryResult = getApitokensOKBodyAO0

	// GetApitokensOKBodyAO1
	var dataGetApitokensOKBodyAO1 struct {
		Data []*models.CommonAPIToken `json:"data"`
	}
	if err := swag.ReadJSON(raw, &dataGetApitokensOKBodyAO1); err != nil {
		return err
	}

	o.Data = dataGetApitokensOKBodyAO1.Data

	return nil
}

// MarshalJSON marshals this object to a JSON structure
func (o GetApitokensOKBody) MarshalJSON() ([]byte, error) {
	_parts := make([][]byte, 0, 2)

	getApitokensOKBodyAO0, err := swag.WriteJSON(o.CommonJSONDBQueryResult)
	if err != nil {
		return nil, err
	}
	_parts = append(_parts, getApitokensOKBodyAO0)
	var dataGetApitokensOKBodyAO1 struct {
		Data []*models.CommonAPIToken `json:"data"`
	}

	dataGetApitokensOKBodyAO1.Data = o.Data

	jsonDataGetApitokensOKBodyAO1, errGetApitokensOKBodyAO1 := swag.WriteJSON(dataGetApitokensOKBodyAO1)
	if errGetApitokensOKBodyAO1 != nil {
		return nil, errGetApitokensOKBodyAO1
	}
	_parts = append(_parts, jsonDataGetApitokensOKBodyAO1)
	return swag.ConcatJSON(_parts...), nil
}

// Validate validates this get apitokens o k body
func (o *GetApitokensOKBody) Validate(formats strfmt.Registry) error {
	var res []error

	// validation for a type composition with models.CommonJSONDBQueryResult
	if err := o.CommonJSONDBQueryResult.Validate(formats); err != nil {
		res = append(res, err)
	}

	if err := o.validateData(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (o *GetApitokensOKBody) validateData(formats strfmt.Registry) error {

	if swag.IsZero(o.Data) { // not required
		return nil
	}

	for i := 0; i < len(o.Data); i++ {
		if swag.IsZero(o.Data[i]) { // not required
			continue
		}

		if o.Data[i] != nil {
			if err := o.Data[i].Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("getApitokensOK" + "." + "data" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("getApitokensOK" + "." + "data" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

// ContextValidate validate this get apitokens o k body based on the context it is used
func (o *GetApitokensOKBody) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

	// validation for a type composition with models.CommonJSONDBQueryResult
	if err := o.CommonJSONDBQueryResult.ContextValidate(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := o.contextValidateData(ctx, formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (o *GetApitokensOKBody) contextValidateData(ctx context.Context, formats strfmt.Registry) error {

	for i := 0; i < len(o.Data); i++ {

		if o.Data[i] != nil {

			if swag.IsZero(o.Data[i]) { // not required
				return nil
			}

			if err := o.Data[i].ContextValidate(ctx, formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("getApitokensOK" + "." + "data" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("getApitokensOK" + "." + "data" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

// MarshalBinary interface implementation
func (o *GetApitokensOKBody) MarshalBinary() ([]byte, error) {
	if o == nil {
		return nil, nil
	}
	return swag.WriteJSON(o)
}

// UnmarshalBinary interface implementation
func (o *GetApitokensOKBody) UnmarshalBinary(b []byte) error {
	var res GetApitokensOKBody
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*o = res
	return nil
}
