Last active
April 14, 2020 07:13
-
-
Save Mukundhan-I2I/2ce26174deafa2313930f4d358582857 to your computer and use it in GitHub Desktop.
httpclient.go
This file contains 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
package client | |
import ( | |
"yourpackage/model" | |
"bytes" | |
"encoding/json" | |
"errors" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
"strings" | |
"time" | |
) | |
type HttpClientOptions struct { | |
Timeout time.Duration | |
} | |
type HttpClient struct { | |
*http.Client | |
} | |
func NewHttpClient() *HttpClient { | |
return &HttpClient{Client: &http.Client{}} | |
} | |
func (mobileClient *HttpClient) options(clientOptions HttpClientOptions) { | |
if clientOptions.Timeout > 0 { | |
mobileClient.Timeout = clientOptions.Timeout | |
} | |
} | |
var defaultHeaders = map[string]string{ | |
"Content-Type": "application/json", | |
"Response-Type": "application/json", | |
} | |
func (client *HttpClient) DoGetWithHeaders(url string, headers map[string]string) (body []byte, err error) { | |
const method = "GET" | |
request, err := http.NewRequest(method, url, nil) | |
for key, val := range headers { | |
request.Header.Add(key, val) | |
} | |
response, err := client.Do(request) | |
defer response.Body.Close() | |
if err != nil { | |
return | |
} | |
if response.StatusCode != 200 { | |
err = errors.New(response.Status) | |
} | |
body, err = ioutil.ReadAll(response.Body) | |
return | |
} | |
func (mobileClient *HttpClient) DoGet(url string) (body []byte, err error) { | |
return mobileClient.DoGetWithHeaders(url, defaultHeaders) | |
} | |
func (client *HttpClient) DoPostWithHeaders(url string, headers map[string]string, payload string) (body []byte, err error) { | |
var jsonStr = []byte(payload) | |
request, err := http.NewRequest("POST", fmt.Sprintf("%v", url), bytes.NewBuffer(jsonStr)) // URL-encoded payload | |
for key, val := range headers { | |
request.Header.Add(key, val) | |
} | |
if err != nil { | |
return | |
} | |
response, err := client.Do(request) | |
defer response.Body.Close() | |
if err != nil { | |
return | |
} | |
if response.StatusCode != 200 { | |
err = errors.New(response.Status) | |
} | |
body, err = ioutil.ReadAll(response.Body) | |
return | |
} | |
func (client *HttpClient) DoPost(url string, payload string) (body []byte, err error) { | |
return client.DoPostWithHeaders(url, defaultHeaders, payload) | |
} | |
func (client *HttpClient) Login(loginUrl string, values map[string]string) (authResponse model.AuthResponse, err error) { | |
resource := "/api/v1/login/" | |
data := url.Values{} | |
for key, val := range values { | |
data.Set(key, val) | |
} | |
u, err := url.ParseRequestURI(loginUrl) | |
if err != nil { | |
return | |
} | |
u.Path = resource | |
urlStr := u.String() // "${loginUrl}${resource}" | |
request, err := http.NewRequest("POST", fmt.Sprintf("%v", urlStr), strings.NewReader(data.Encode())) // URL-encoded payload | |
request.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
if err != nil { | |
return | |
} | |
response, err := client.Do(request) | |
defer response.Body.Close() | |
if err != nil { | |
return | |
} | |
body, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
return | |
} | |
if response.StatusCode != 200 { | |
err = errors.New(response.Status) | |
} | |
err = json.Unmarshal(body, &authResponse) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment