Skip to content

Commit

Permalink
Add DoRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
gfr10598 committed Dec 12, 2018
1 parent 1c666b2 commit 897ccf2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
49 changes: 49 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
package api

import (
"bytes"
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"regexp"
"time"
)
Expand Down Expand Up @@ -112,3 +116,48 @@ func ExtractDateFromFilename(filename string) (time.Time, error) {
}
return time.Parse(time.RFC3339, filedate[0][0:4]+"-"+filedate[0][4:6]+"-"+filedate[0][6:8]+"T00:00:00Z")
}

// DoRPC takes a url, and RequestV2, makes remote call, and returns parsed ResponseV2
// TODO(gfr) Should pass the annotator's request context through and use it here.
func DoRPC(ctx context.Context, url string, req RequestV2) (*ResponseV2, error) {
encodedData, err := json.Marshal(req)
if err != nil {
return nil, err
}

var netClient = &http.Client{
// Median response time is < 10 msec, but 99th percentile is 0.6 seconds.
Timeout: 2 * time.Second,
}

httpReq, err := http.NewRequest("POST", url, bytes.NewReader(encodedData))
if err != nil {
return nil, err
}

// Make the actual request
httpResp, err := netClient.Do(httpReq.WithContext(ctx))
if err != nil {
return nil, err
}
defer httpResp.Body.Close()

// Catch errors reported by the service
if httpResp.StatusCode != http.StatusOK {
return nil, errors.New("URL:" + url + " gave response code " + httpResp.Status)
}

// Copy response into a byte slice
body, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
return nil, err
}

resp := ResponseV2{}

err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
41 changes: 41 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package api_test

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/go-test/deep"
"github.com/m-lab/annotation-service/api"
)

Expand Down Expand Up @@ -53,3 +59,38 @@ func TestRequestWrapper(t *testing.T) {
t.Fatal("Should have produced json unmarshal error")
}
}

func init() {
// Always prepend the filename and line number.
log.SetFlags(log.LstdFlags | log.Lshortfile)
}

func TestDoRPC(t *testing.T) {
expectedJson := `{"AnnotatorDate":"2018-12-05T00:00:00Z","Annotations":{"147.1.2.3":{"Geo":{"continent_code":"NA","country_code":"US","country_name":"United States","latitude":37.751,"longitude":-97.822},"ASN":{}},"8.8.8.8":{"Geo":{"continent_code":"NA","country_code":"US","country_name":"United States","latitude":37.751,"longitude":-97.822},"ASN":{}}}}`

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, expectedJson)
}))
url := ts.URL

//url = "https://annotator-dot-mlab-sandbox.appspot.com/batch_annotate"
req := api.RequestV2{Date: time.Now()}
req.RequestType = api.RequestV2Tag
req.RequestInfo = "Test"
req.IPs = append(req.IPs, "8.8.8.8")
req.IPs = append(req.IPs, "147.1.2.3")
resp, err := api.DoRPC(context.Background(), url, req)
if err != nil {
t.Fatal(err)
}

expectedResponse := api.ResponseV2{}
err = json.Unmarshal([]byte(expectedJson), &expectedResponse)
if err != nil {
t.Fatal(err)
}

if diff := deep.Equal(expectedResponse, *resp); diff != nil {
t.Error(diff)
}
}

0 comments on commit 897ccf2

Please sign in to comment.