go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/internal/logdog/logdog.go (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // 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,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package logdog contains logic of interacting with Logdog.
    16  package logdog
    17  
    18  import (
    19  	"context"
    20  	"net/http"
    21  	"time"
    22  
    23  	"go.chromium.org/luci/bisection/util"
    24  
    25  	"go.chromium.org/luci/common/logging"
    26  )
    27  
    28  var MockedLogdogClientKey = "mocked logdog client"
    29  
    30  // GetLogFromViewUrl gets the log from the log's viewURL
    31  func GetLogFromViewUrl(c context.Context, viewUrl string) (string, error) {
    32  	cl := GetClient(c)
    33  	return cl.GetLog(c, viewUrl)
    34  }
    35  
    36  type LogdogClient struct{}
    37  
    38  func (cl *LogdogClient) GetLog(c context.Context, viewUrl string) (string, error) {
    39  	req, err := http.NewRequest("GET", viewUrl, nil)
    40  	if err != nil {
    41  		return "", err
    42  	}
    43  
    44  	q := req.URL.Query()
    45  	q.Add("format", "raw")
    46  	req.URL.RawQuery = q.Encode()
    47  
    48  	logging.Infof(c, "Sending request to logdog %s", req.URL.String())
    49  	return util.SendHTTPRequest(c, req, 30*time.Second)
    50  }
    51  
    52  // We need the interface for testing purpose
    53  type Client interface {
    54  	GetLog(c context.Context, viewUrl string) (string, error)
    55  }
    56  
    57  func GetClient(c context.Context) Client {
    58  	if mockClient, ok := c.Value(MockedLogdogClientKey).(*MockedLogdogClient); ok {
    59  		return mockClient
    60  	}
    61  	return &LogdogClient{}
    62  }