How to do it...

These steps cover writing and running of your application:

  1. From your Terminal or console application, create a new directory called ~/projects/go-programming-cookbook/chapter7/client, and navigate to this directory.
  2. Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter7/client

You should see a file called go.mod containing the following:

module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter7/client 
  1. Copy tests from ~/projects/go-programming-cookbook-original/chapter7/client, or use this as an exercise to write some code of your own!
  1. Create a file called client.go with the following content:
        package client

import (
"crypto/tls"
"net/http"
)

// Setup configures our client and redefines
// the global DefaultClient
func Setup(isSecure, nop bool) *http.Client {
c := http.DefaultClient

// Sometimes for testing, we want to
// turn off SSL verification
if !isSecure {
c.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
}
}
if nop {
c.Transport = &NopTransport{}
}
http.DefaultClient = c
return c
}

// NopTransport is a No-Op Transport
type NopTransport struct {
}

// RoundTrip Implements RoundTripper interface
func (n *NopTransport) RoundTrip(*http.Request)
(*http.Response, error) {
// note this is an uninitialized Response
// if you're looking at headers and so on.
return &http.Response{StatusCode: http.StatusTeapot}, nil
}
  1. Create a file called exec.go with the following content:
        package client

import (
"fmt"
"net/http"
)

// DoOps takes a client, then fetches
// google.com
func DoOps(c *http.Client) error {
resp, err := c.Get("http://www.google.com")
if err != nil {
return err
}
fmt.Println("results of DoOps:", resp.StatusCode)

return nil
}

// DefaultGetGolang uses the default client
// to get golang.org
func DefaultGetGolang() error {
resp, err := http.Get("https://www.golang.org")
if err != nil {
return err
}
fmt.Println("results of DefaultGetGolang:",
resp.StatusCode)
return nil
}
  1. Create a file called store.go with the following content:
        package client

import (
"fmt"
"net/http"
)

// Controller embeds an http.Client
// and uses it internally
type Controller struct {
*http.Client
}

// DoOps with a controller object
func (c *Controller) DoOps() error {
resp, err := c.Client.Get("http://www.google.com")
if err != nil {
return err
}
fmt.Println("results of client.DoOps", resp.StatusCode)
return nil
}
  1. Create a new directory called example and navigate to it.
  2. Create a file named main.go with the following content:
        package main

import "github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter7/client"

func main() {
// secure and op!
cli := client.Setup(true, false)

if err := client.DefaultGetGolang(); err != nil {
panic(err)
}

if err := client.DoOps(cli); err != nil {
panic(err)
}

c := client.Controller{Client: cli}
if err := c.DoOps(); err != nil {
panic(err)
}

// secure and noop
// also modifies default
client.Setup(true, true)

if err := client.DefaultGetGolang(); err != nil {
panic(err)
}
}
  1. Run go run main.go.
  2. You may also run the following command:
$ go build
$ ./example

You should now see the following output:

$ go run main.go
results of DefaultGetGolang: 200
results of DoOps: 200
results of client.DoOps 200
results of DefaultGetGolang: 418
  1. If you copied or wrote your own tests, go up one directory and run go test. Ensure that all the tests pass.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.239.46