Skip to content

Commit

Permalink
fix client EOF error
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Jun 15, 2021
1 parent 6d14280 commit a1e68cd
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions model/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"fmt"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"io/ioutil"
Expand All @@ -10,7 +11,7 @@ import (
)

type Client struct {
TgId int64 `gorm:"unique;not null"`
TgId int64 `gorm:"not null"`
RefreshToken string `gorm:"not null"`
MsId string `gorm:"unique;primaryKey;not null"`
Uptime int64 `gorm:"autoUpdateTime;not null"`
Expand Down Expand Up @@ -43,7 +44,7 @@ func GetMSRegisterAppUrl() string {
return appUrl
}

//return access_token and refresh_token
// GetTokenWithCode return access_token and refresh_token
func (c *Client) GetTokenWithCode(code string) (error error) {
var r http.Request
client := &http.Client{}
Expand All @@ -59,6 +60,11 @@ func (c *Client) GetTokenWithCode(code string) (error error) {
if err != nil {
return err
}

//prevents the connection from being re-used
////https://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi
req.Close = true

resp, err := client.Do(req)
if err != nil {
return err
Expand Down Expand Up @@ -92,6 +98,7 @@ func (c *Client) getToken() (access string) {
if err != nil {
return ""
}
req.Close = true
resp, err := client.Do(req)
if err != nil {
return ""
Expand All @@ -101,29 +108,31 @@ func (c *Client) getToken() (access string) {
if err != nil {
return ""
}
//fmt.Println(string(content))
//fmt.Println(gjson.Get(string(content), "access_token").String())
if gjson.Get(string(content), "token_type").String() == "Bearer" {
c.RefreshToken = gjson.Get(string(content), "refresh_token").String()
return gjson.Get(string(content), "access_token").String()
}
return ""
}

//Get User's Information
// GetUserInfo Get User's Information
func (c *Client) GetUserInfo() (json string, error error) {
client := http.Client{}
req, err := http.NewRequest("GET", msGraUrl+"/v1.0/me", nil)
if err != nil {
return "", err
}

req.Close = true

req.Header.Set("Authorization", c.getToken())
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(content))
if err != nil {
return "", err
}
Expand All @@ -137,10 +146,10 @@ func (c *Client) GetUserInfo() (json string, error error) {
func (c *Client) GetOutlookMails() error {
client := http.Client{}
req, err := http.NewRequest("GET", msGraUrl+"/v1.0/me/messages", nil)

if err != nil {
return err
}
req.Close = true
req.Header.Set("Authorization", c.getToken())
resp, err := client.Do(req)
if err != nil {
Expand Down

0 comments on commit a1e68cd

Please sign in to comment.