Skip to content

Commit

Permalink
http utils and http post request call
Browse files Browse the repository at this point in the history
  • Loading branch information
bhattaraibishal50 committed Sep 1, 2020
1 parent 42b0cce commit f078d4d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
24 changes: 23 additions & 1 deletion common/httpUtil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package common

import "github.com/gin-gonic/gin"
import (
"net/http"
"log"
"github.com/gin-gonic/gin"
"encoding/json"
)

// NewHTTPError example
func NewHTTPError(ctx *gin.Context, status int, err error) {
Expand All @@ -16,3 +21,20 @@ type HTTPError struct {
Code int `json:"code" example:"400"`
Message string `json:"message" example:"Request Failure"`
}

// MakeHTTPRequestWithJSONPayload makes json payload
func MakeHTTPRequestWithJSONPayload(payload map[string]interface{}, URI string) {
payloadBuffer, err := ConverMapToJSONPayload(payload)
if(err != nil){
log.Fatalln(err);
}
resp, err := http.Post(URI, "application/json; charset=UTF-8", payloadBuffer)
if err != nil {
log.Fatalln(err)
}

var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
log.Println(result)
log.Println("data: ", result["data"])
}
13 changes: 12 additions & 1 deletion common/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package common

import (
"bytes"
"log"

"encoding/json"
"github.com/google/uuid"
)

Expand All @@ -19,3 +20,13 @@ func ConvertStringToID(s string) uuid.UUID {
}
return uuid
}

// ConverMapToJSONPayload returns bytes buffer
func ConverMapToJSONPayload(payload map[string]interface{}) (*bytes.Buffer, error) {
bytesRepresentationPayload, err := json.Marshal(payload)
if err != nil {
return nil, err
}
buff :=bytes.NewBuffer(bytesRepresentationPayload)
return buff, nil
}
5 changes: 4 additions & 1 deletion services/firebaseLogin/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const functions = require('firebase-functions');

const express = require("express");
const app = express();
app.use(express.json());
const bodyParser = require("body-parser")
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

require('./routes/index')(app);

// API Endpoint : http://localhost:5000/blog-goland/asia-northeast3/api
Expand Down
Binary file modified tmp/runner-build
Binary file not shown.
6 changes: 6 additions & 0 deletions tmp/runner-build-errors.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# github/bhattaraibishal50/blog/user/repository
user/repository/userRepp.go:4:2: imported and not used: "bytes"
user/repository/userRepp.go:5:2: imported and not used: "net/http"
user/repository/userRepp.go:6:2: imported and not used: "log"
user/repository/userRepp.go:7:2: imported and not used: "encoding/json"
note: module requires Go 1.15
11 changes: 8 additions & 3 deletions user/repository/userRepp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type firebaseAuthRepo struct{}
var fb = common.NewFirebaseApp()
var fbAuth = fb.GetFirebaseAuth()
var ctx = context.Background()

var serviceURI = "https://5000-c6fca9c0-4e36-41d2-86db-511a4134a8bf.ws-us02.gitpod.io/blog-goland/asia-northeast3/api"
// NewFirebaseAuthRepo constrictore returns user repos
func NewFirebaseAuthRepo() UserRepository {
return &firebaseAuthRepo{}
Expand All @@ -27,9 +27,13 @@ func NewFirebaseAuthRepo() UserRepository {
// If you want to sign in as a user, you need to use the Firebase Authentication SDK.
func (*firebaseAuthRepo) Login() {
// checks the user by email and the password


// request body (payload)

payload := map[string]interface{}{
"email" : "[email protected]",
"password" : "password",
}
common.MakeHTTPRequestWithJSONPayload(payload, serviceURI)
usersRecord, err := fbAuth.GetUser(ctx, "5ZamU26mXsOYmCrankcEGTujgsj2")
if err != nil {
fmt.Println(err)
Expand All @@ -39,3 +43,4 @@ func (*firebaseAuthRepo) Login() {


// GO tokenization.

0 comments on commit f078d4d

Please sign in to comment.