github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/raw.go (about)

     1  // Copyright 2021 LINE Corporation
     2  //
     3  // LINE Corporation licenses this file to you under the Apache License,
     4  // version 2.0 (the "License"); you may not use this file except in compliance
     5  // with the License. You may obtain a copy of the License at:
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package linebot
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"net/http"
    21  )
    22  
    23  // NewRawCall method
    24  func (client *Client) NewRawCall(method string, endpoint string) (*RawCall, error) {
    25  	req, err := http.NewRequest(method, client.url(client.endpointBase, endpoint), nil)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	return &RawCall{
    30  		c:   client,
    31  		req: req,
    32  	}, nil
    33  }
    34  
    35  // NewRawCallWithBody method
    36  func (client *Client) NewRawCallWithBody(method string, endpoint string, body io.Reader) (*RawCall, error) {
    37  	req, err := http.NewRequest(method, client.url(client.endpointBase, endpoint), body)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return &RawCall{
    42  		c:   client,
    43  		req: req,
    44  	}, nil
    45  }
    46  
    47  // RawCall type
    48  type RawCall struct {
    49  	c   *Client
    50  	ctx context.Context
    51  
    52  	req *http.Request
    53  }
    54  
    55  // AddHeader method
    56  func (call *RawCall) AddHeader(key string, value string) {
    57  	call.req.Header.Add(key, value)
    58  }
    59  
    60  // WithContext method
    61  func (call *RawCall) WithContext(ctx context.Context) *RawCall {
    62  	call.ctx = ctx
    63  	return call
    64  }
    65  
    66  // Do method. Callee must close response object.
    67  func (call *RawCall) Do() (*http.Response, error) {
    68  	return call.c.do(call.ctx, call.req)
    69  }