mastering bridging
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"satusehat-rssa/internal/model"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type OauthInterface interface {
|
||||
GenerateToken(req model.OauthRequest) (*model.OauthResponse, error)
|
||||
}
|
||||
|
||||
type OauthRepository struct {
|
||||
akses *model.Akses
|
||||
}
|
||||
|
||||
// GenerateToken implements OauthInterface.
|
||||
func (o *OauthRepository) GenerateToken(req model.OauthRequest) (*model.OauthResponse, error) {
|
||||
var (
|
||||
data *model.OauthResponse
|
||||
)
|
||||
url := o.akses.AuthUrl + "/accesstoken?grant_type=client_credentials"
|
||||
method := "POST"
|
||||
|
||||
req_data := "client_id=" + req.ClientId + "&client_secret=" + req.ClientSecret
|
||||
payload := strings.NewReader(req_data)
|
||||
|
||||
client := &http.Client{}
|
||||
request, err := http.NewRequest(method, url, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
res, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
err = json.NewDecoder(res.Body).Decode(&data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func NewOauthRequestRepo(akses *model.Akses) OauthInterface {
|
||||
return &OauthRepository{
|
||||
akses: akses,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user