utils

package
v0.0.0-...-81ebf7a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2019 License: MIT Imports: 7 Imported by: 0

README

Http Request

Usage

  • POST
package main

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/purwokertodev/go-backend/utils"
)

type Post struct {
	UserID int    `json:"userId"`
	ID     int    `json:"id"`
	Title  string `json:"title"`
	Body   string `json:"body"`
}

func main() {
	var post Post
	post.ID = 101
	post.UserID = 1
	post.Title = "Golang"
	post.Body = "Golang is awesome"

	payload, _ := json.Marshal(post)

	var resp Post
	req := utils.NewRequest(10)

	headers := make(map[string]string)
	headers["Content-Type"] = "application/json"
	headers["Accept"] = "application/json"

	err := req.Req("POST", "https://jsonplaceholder.typicode.com/users", bytes.NewBuffer(payload), &resp, headers)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(resp)
}
  • GET

    package main
    
    import (
    	"fmt"
    
    	"github.com/purwokertodev/go-backend/utils"
    )
    
    type User struct {
    	ID       int    `json:"id"`
    	Name     string `json:"name"`
    	Username string `json:"username"`
    	Email    string `json:"email"`
    	Address  struct {
    		Street  string `json:"street"`
    		Suite   string `json:"suite"`
    		City    string `json:"city"`
    		Zipcode string `json:"zipcode"`
    		Geo     struct {
    			Lat string `json:"lat"`
    			Lng string `json:"lng"`
    		} `json:"geo"`
    	} `json:"address"`
    	Phone   string `json:"phone"`
    	Website string `json:"website"`
    	Company struct {
    		Name        string `json:"name"`
    		CatchPhrase string `json:"catchPhrase"`
    		Bs          string `json:"bs"`
    	} `json:"company"`
    }
    
    type Users []User
    
    func main() {
    	var users Users
    
    	req := utils.NewRequest(10)
    
    	headers := make(map[string]string)
    	headers["Content-Type"] = "application/json"
    	headers["Accept"] = "application/json"
    
    	err := req.Req("GET", "https://jsonplaceholder.typicode.com/users", nil, &users, headers)
    	if err != nil {
    		fmt.Println(err)
    	}
    
    	fmt.Println(users)
    }
    

Email

Usage

  • Html Email Template

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    
    </head>
    
    <body>
    <p>
        Hi {{.Username}}
        <h4>Your Account Registration Success</h4>
        <p>Please follow link bellow, to complete your Registration</p>
        <a href="{{.URL}}">Confirm email address</a>
    </p>
    
    </body>
    
    </html>
    
  • Golang Code

    import (
    	"fmt"
    
    	"github.com/purwokertodev/go-backend/utils"
    )
    
    func main() {
    	authEmail := "wuriyanto007@gmail.com"
    	authPassword := "your email password"
    	authHost := "smtp.gmail.com"
    	address := "smtp.gmail.com:587"
    	to := []string{"wuriyanto48@yahoo.co.id"}
    	from := "wuriyanto007@gmail.com"
    	subject := "Golang email"
    	body := "Golang email sent..."
    	email := utils.NewEmail(to, address, from, subject, body, authEmail, authPassword, authHost)
    
    	emailData := struct {
    		Username string
    		URL      string
    	}{
    		Username: "Wuriyanto",
    		URL:      "wuriyanto.com",
    	}
    
    	err := execute(email, "email_template.html", emailData)
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println("email sent")
    
    }
    
    func execute(u utils.EmailSender, fileName string, data interface{}) error {
    	err := u.SetTemplate(fileName, data)
    	if err != nil {
    		return err
    	}
    
    	err = u.Send()
    	if err != nil {
    		return err
    	}
    
    	return nil
    }
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JsonResponse

func JsonResponse(res http.ResponseWriter, resp interface{}, httpCode int)

JsonResponse function for Marshal and format response using Json

func NewRequest

func NewRequest(timeout time.Duration) *httpRequest

NewRequest function for intialize httpRequest object Paramter, timeout in time.Duration

Types

type EmailSender

type EmailSender interface {
	Send() error
	SetTemplate(templateFile string, data interface{}) error
}

EmailSender interface

func NewEmail

func NewEmail(to []string, address, from, subject, body, authEmail, authPassword, authHost string) EmailSender

NewEmail function, for initialize email model

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL