This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | func getRandomJoke(config Config) (string, error) { | |
| request, err := buildJokeRequest(config) | |
| if err != nil { | |
| return "", err | |
| } | |
| response, err := getJoke(request) | |
| if err != nil { | |
| return "", err | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | func readJokeFromResponse(response *http.Response) (string, error) { | |
| if response.StatusCode != http.StatusOK { | |
| return "", fmt.Errorf("server returned an error: %s", response.Status) | |
| } | |
| content, err := ioutil.ReadAll(response.Body) | |
| return string(content), err | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | func getJoke(request *http.Request) (*http.Response, error) { | |
| client := &http.Client{} | |
| client.Timeout = 20 * time.Second | |
| return client.Do(request) | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | func buildJokeRequest(config Config) (*http.Request, error) { | |
| jokeURL, err := buildJokeURL(config) | |
| if err != nil { | |
| return nil, err | |
| } | |
| request, err := http.NewRequest("GET", jokeURL.String(), nil) | |
| if err != nil { | |
| return nil, err | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var config Config | |
| if err := stepconf.Parse(&config); err != nil { | |
| log.Errorf("Could not validate config, error: %s\n", err) | |
| os.Exit(1) | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Config ... | |
| type Config struct { | |
| APIBaseURL string `env:"api_base_url,required"` | |
| Category string `env:"category"` | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | inputs: | |
| - category: | |
| opts: | |
| title: "Joke category, optional." | |
| summary: | | |
| Optional category of the joke, see [list of available categories](https://api.chucknorris.io/jokes/categories). | |
| If empty joke won't be restricted to any category. | |
| is_expand: true | |
| is_required: false | |
| - api_base_url: "https://api.chucknorris.io" | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | func TestDownloadFileUnreachableURL(t *testing.T) { | |
| dummyFile, err := os.Open("/dev/null") | |
| require.NoError(t, err) | |
| err = downloadFile("http://unreachable.invalid", dummyFile) | |
| require.Error(t, err) | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | - change-workdir: | |
| title: Switch working dir to test / _tmp dir | |
| description: |- | |
| To prevent step testing issues, like referencing relative | |
| files with just './some-file' in the step's code, which would | |
| work for testing the step from this directory directly | |
| but would break if the step is included in another `bitrise.yml`. | |
| run_if: "true" | |
| inputs: | |
| - path: ./_tmp | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | func downloadFile(downloadURL string, outFile *os.File) error { | |
| response, err := http.Get(downloadURL) | |
| if err != nil { | |
| return err | |
| } | |
| defer func() { | |
| if err := response.Body.Close(); err != nil { | |
| log.Warnf("Failed to close (%s) body", downloadURL) | |
| } |