github.com/openshift-online/ocm-sdk-go@v0.1.473/jobqueue/v1/job_client.go (about)

     1  /*
     2  Copyright (c) 2020 Red Hat, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8    http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // IMPORTANT: This file has been generated automatically, refrain from modifying it manually as all
    18  // your changes will be lost when the file is generated again.
    19  
    20  package v1 // github.com/openshift-online/ocm-sdk-go/jobqueue/v1
    21  
    22  import (
    23  	"bufio"
    24  	"bytes"
    25  	"context"
    26  	"io"
    27  	"net/http"
    28  	"net/url"
    29  	"path"
    30  
    31  	"github.com/openshift-online/ocm-sdk-go/errors"
    32  	"github.com/openshift-online/ocm-sdk-go/helpers"
    33  )
    34  
    35  // JobClient is the client of the 'job' resource.
    36  //
    37  // Manages status of jobs on a job queue.
    38  type JobClient struct {
    39  	transport http.RoundTripper
    40  	path      string
    41  }
    42  
    43  // NewJobClient creates a new client for the 'job'
    44  // resource using the given transport to send the requests and receive the
    45  // responses.
    46  func NewJobClient(transport http.RoundTripper, path string) *JobClient {
    47  	return &JobClient{
    48  		transport: transport,
    49  		path:      path,
    50  	}
    51  }
    52  
    53  // Failure creates a request for the 'failure' method.
    54  //
    55  // Mark a job as Failed. This method returns '204 No Content'
    56  func (c *JobClient) Failure() *JobFailureRequest {
    57  	return &JobFailureRequest{
    58  		transport: c.transport,
    59  		path:      path.Join(c.path, "failure"),
    60  	}
    61  }
    62  
    63  // Success creates a request for the 'success' method.
    64  //
    65  // Mark a job as Successful. This method returns '204 No Content'
    66  func (c *JobClient) Success() *JobSuccessRequest {
    67  	return &JobSuccessRequest{
    68  		transport: c.transport,
    69  		path:      path.Join(c.path, "success"),
    70  	}
    71  }
    72  
    73  // JobFailureRequest is the request for the 'failure' method.
    74  type JobFailureRequest struct {
    75  	transport     http.RoundTripper
    76  	path          string
    77  	query         url.Values
    78  	header        http.Header
    79  	failureReason *string
    80  	receiptId     *string
    81  }
    82  
    83  // Parameter adds a query parameter.
    84  func (r *JobFailureRequest) Parameter(name string, value interface{}) *JobFailureRequest {
    85  	helpers.AddValue(&r.query, name, value)
    86  	return r
    87  }
    88  
    89  // Header adds a request header.
    90  func (r *JobFailureRequest) Header(name string, value interface{}) *JobFailureRequest {
    91  	helpers.AddHeader(&r.header, name, value)
    92  	return r
    93  }
    94  
    95  // Impersonate wraps requests on behalf of another user.
    96  // Note: Services that do not support this feature may silently ignore this call.
    97  func (r *JobFailureRequest) Impersonate(user string) *JobFailureRequest {
    98  	helpers.AddImpersonationHeader(&r.header, user)
    99  	return r
   100  }
   101  
   102  // FailureReason sets the value of the 'failure_reason' parameter.
   103  func (r *JobFailureRequest) FailureReason(value string) *JobFailureRequest {
   104  	r.failureReason = &value
   105  	return r
   106  }
   107  
   108  // ReceiptId sets the value of the 'receipt_id' parameter.
   109  //
   110  // A unique ID of a pop'ed job
   111  func (r *JobFailureRequest) ReceiptId(value string) *JobFailureRequest {
   112  	r.receiptId = &value
   113  	return r
   114  }
   115  
   116  // Send sends this request, waits for the response, and returns it.
   117  //
   118  // This is a potentially lengthy operation, as it requires network communication.
   119  // Consider using a context and the SendContext method.
   120  func (r *JobFailureRequest) Send() (result *JobFailureResponse, err error) {
   121  	return r.SendContext(context.Background())
   122  }
   123  
   124  // SendContext sends this request, waits for the response, and returns it.
   125  func (r *JobFailureRequest) SendContext(ctx context.Context) (result *JobFailureResponse, err error) {
   126  	query := helpers.CopyQuery(r.query)
   127  	header := helpers.CopyHeader(r.header)
   128  	buffer := &bytes.Buffer{}
   129  	err = writeJobFailureRequest(r, buffer)
   130  	if err != nil {
   131  		return
   132  	}
   133  	uri := &url.URL{
   134  		Path:     r.path,
   135  		RawQuery: query.Encode(),
   136  	}
   137  	request := &http.Request{
   138  		Method: "POST",
   139  		URL:    uri,
   140  		Header: header,
   141  		Body:   io.NopCloser(buffer),
   142  	}
   143  	if ctx != nil {
   144  		request = request.WithContext(ctx)
   145  	}
   146  	response, err := r.transport.RoundTrip(request)
   147  	if err != nil {
   148  		return
   149  	}
   150  	defer response.Body.Close()
   151  	result = &JobFailureResponse{}
   152  	result.status = response.StatusCode
   153  	result.header = response.Header
   154  	reader := bufio.NewReader(response.Body)
   155  	_, err = reader.Peek(1)
   156  	if err == io.EOF {
   157  		err = nil
   158  		return
   159  	}
   160  	if result.status >= 400 {
   161  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   162  		if err != nil {
   163  			return
   164  		}
   165  		err = result.err
   166  		return
   167  	}
   168  	return
   169  }
   170  
   171  // JobFailureResponse is the response for the 'failure' method.
   172  type JobFailureResponse struct {
   173  	status int
   174  	header http.Header
   175  	err    *errors.Error
   176  }
   177  
   178  // Status returns the response status code.
   179  func (r *JobFailureResponse) Status() int {
   180  	if r == nil {
   181  		return 0
   182  	}
   183  	return r.status
   184  }
   185  
   186  // Header returns header of the response.
   187  func (r *JobFailureResponse) Header() http.Header {
   188  	if r == nil {
   189  		return nil
   190  	}
   191  	return r.header
   192  }
   193  
   194  // Error returns the response error.
   195  func (r *JobFailureResponse) Error() *errors.Error {
   196  	if r == nil {
   197  		return nil
   198  	}
   199  	return r.err
   200  }
   201  
   202  // JobSuccessRequest is the request for the 'success' method.
   203  type JobSuccessRequest struct {
   204  	transport http.RoundTripper
   205  	path      string
   206  	query     url.Values
   207  	header    http.Header
   208  	receiptId *string
   209  }
   210  
   211  // Parameter adds a query parameter.
   212  func (r *JobSuccessRequest) Parameter(name string, value interface{}) *JobSuccessRequest {
   213  	helpers.AddValue(&r.query, name, value)
   214  	return r
   215  }
   216  
   217  // Header adds a request header.
   218  func (r *JobSuccessRequest) Header(name string, value interface{}) *JobSuccessRequest {
   219  	helpers.AddHeader(&r.header, name, value)
   220  	return r
   221  }
   222  
   223  // Impersonate wraps requests on behalf of another user.
   224  // Note: Services that do not support this feature may silently ignore this call.
   225  func (r *JobSuccessRequest) Impersonate(user string) *JobSuccessRequest {
   226  	helpers.AddImpersonationHeader(&r.header, user)
   227  	return r
   228  }
   229  
   230  // ReceiptId sets the value of the 'receipt_id' parameter.
   231  //
   232  // A unique ID of a pop'ed job
   233  func (r *JobSuccessRequest) ReceiptId(value string) *JobSuccessRequest {
   234  	r.receiptId = &value
   235  	return r
   236  }
   237  
   238  // Send sends this request, waits for the response, and returns it.
   239  //
   240  // This is a potentially lengthy operation, as it requires network communication.
   241  // Consider using a context and the SendContext method.
   242  func (r *JobSuccessRequest) Send() (result *JobSuccessResponse, err error) {
   243  	return r.SendContext(context.Background())
   244  }
   245  
   246  // SendContext sends this request, waits for the response, and returns it.
   247  func (r *JobSuccessRequest) SendContext(ctx context.Context) (result *JobSuccessResponse, err error) {
   248  	query := helpers.CopyQuery(r.query)
   249  	header := helpers.CopyHeader(r.header)
   250  	buffer := &bytes.Buffer{}
   251  	err = writeJobSuccessRequest(r, buffer)
   252  	if err != nil {
   253  		return
   254  	}
   255  	uri := &url.URL{
   256  		Path:     r.path,
   257  		RawQuery: query.Encode(),
   258  	}
   259  	request := &http.Request{
   260  		Method: "POST",
   261  		URL:    uri,
   262  		Header: header,
   263  		Body:   io.NopCloser(buffer),
   264  	}
   265  	if ctx != nil {
   266  		request = request.WithContext(ctx)
   267  	}
   268  	response, err := r.transport.RoundTrip(request)
   269  	if err != nil {
   270  		return
   271  	}
   272  	defer response.Body.Close()
   273  	result = &JobSuccessResponse{}
   274  	result.status = response.StatusCode
   275  	result.header = response.Header
   276  	reader := bufio.NewReader(response.Body)
   277  	_, err = reader.Peek(1)
   278  	if err == io.EOF {
   279  		err = nil
   280  		return
   281  	}
   282  	if result.status >= 400 {
   283  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   284  		if err != nil {
   285  			return
   286  		}
   287  		err = result.err
   288  		return
   289  	}
   290  	return
   291  }
   292  
   293  // JobSuccessResponse is the response for the 'success' method.
   294  type JobSuccessResponse struct {
   295  	status int
   296  	header http.Header
   297  	err    *errors.Error
   298  }
   299  
   300  // Status returns the response status code.
   301  func (r *JobSuccessResponse) Status() int {
   302  	if r == nil {
   303  		return 0
   304  	}
   305  	return r.status
   306  }
   307  
   308  // Header returns header of the response.
   309  func (r *JobSuccessResponse) Header() http.Header {
   310  	if r == nil {
   311  		return nil
   312  	}
   313  	return r.header
   314  }
   315  
   316  // Error returns the response error.
   317  func (r *JobSuccessResponse) Error() *errors.Error {
   318  	if r == nil {
   319  		return nil
   320  	}
   321  	return r.err
   322  }