Beispiel #1
0
func (c Client) GetRatePlans(ctx context.Context, hotelIDs []int32, inDate string, outDate string) RatePlanReply {
	md, _ := metadata.FromContext(ctx)

	trace.Req(md["traceID"], md["from"], "service.rate", "GetRatePlans")
	defer trace.Rep(md["traceID"], "service.rate", md["from"], time.Now())

	args := &pb.Args{
		HotelIds: hotelIDs,
		InDate:   inDate,
		OutDate:  outDate,
	}

	reply, err := c.client.GetRates(ctx, args)

	if err != nil {
		return RatePlanReply{
			RatePlans: []*pb.RatePlan{},
			Err:       err,
		}
	}

	return RatePlanReply{
		RatePlans: reply.RatePlans,
		Err:       nil,
	}
}
Beispiel #2
0
func (c Client) VerifyToken(ctx context.Context, authToken string) error {
	md, _ := metadata.FromContext(ctx)

	trace.Req(md["traceID"], md["from"], "service.auth", "VerifyToken")
	defer trace.Rep(md["traceID"], "service.auth", md["from"], time.Now())

	args := &pb.Args{AuthToken: authToken}
	if _, err := c.client.VerifyToken(ctx, args); err != nil {
		return err
	}

	return nil
}
Beispiel #3
0
func (c Client) HotelsWithinBoundedBox(ctx context.Context, latitude int32, longitude int32) ([]int32, error) {
	md, _ := metadata.FromContext(ctx)

	trace.Req(md["traceID"], md["from"], "service.geo", "HotelsWithinBoundedBox")
	defer trace.Rep(md["traceID"], "service.geo", md["from"], time.Now())

	rect := &pb.Rectangle{
		Lo: &pb.Point{Latitude: 400000000, Longitude: -750000000},
		Hi: &pb.Point{Latitude: 420000000, Longitude: -730000000},
	}

	reply, err := c.client.BoundedBox(ctx, rect)

	if err != nil {
		return []int32{}, err
	}

	return reply.HotelIds, nil
}
Beispiel #4
0
func (c Client) GetHotels(ctx context.Context, hotelIDs []int32) ProfileReply {
	md, _ := metadata.FromContext(ctx)

	trace.Req(md["traceID"], md["from"], "service.profile", "GetHotels")
	defer trace.Rep(md["traceID"], "service.profile", md["from"], time.Now())

	args := &pb.Args{HotelIds: hotelIDs}
	reply, err := c.client.GetHotels(ctx, args)

	if err != nil {
		return ProfileReply{
			Hotels: []*pb.Hotel{},
			Err:    err,
		}
	}

	return ProfileReply{
		Hotels: reply.Hotels,
		Err:    nil,
	}
}
Beispiel #5
0
func (s apiServer) requestHandler(w http.ResponseWriter, r *http.Request) {
	// trace call to request handler
	traceID := trace.NewTraceID()
	trace.Req(traceID, "www", "api.v1", "")
	defer trace.Rep(traceID, "api.v1", "www", time.Now())
	log.Printf("traceId=%s", traceID)

	// context and metadata
	md := metadata.Pairs("traceID", traceID, "from", "api.v1")
	ctx := context.Background()
	ctx = metadata.NewContext(ctx, md)

	// parse token from Authorization header
	authToken, err := auth_token.Parse(r.Header.Get("Authorization"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}

	// verify auth token
	err = s.authClient.VerifyToken(ctx, authToken)
	if err != nil {
		http.Error(w, "Unauthorized", http.StatusForbidden)
		return
	}

	// read and validate in/out arguments
	inDate := r.URL.Query().Get("inDate")
	outDate := r.URL.Query().Get("outDate")
	if inDate == "" || outDate == "" {
		http.Error(w, "Please specify inDate / outDate", http.StatusBadRequest)
		return
	}

	// get hotels within geo box
	hotelIDs, err := s.geoClient.HotelsWithinBoundedBox(ctx, 100, 100)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	profileCh := s.getHotels(ctx, hotelIDs)
	rateCh := s.getRatePlans(ctx, hotelIDs, inDate, outDate)

	profileReply := <-profileCh
	if err := profileReply.Err; err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	rateReply := <-rateCh
	if err := rateReply.Err; err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	inventory := inventory{
		Hotels:    profileReply.Hotels,
		RatePlans: rateReply.RatePlans,
	}

	body, err := json.Marshal(inventory)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write(body)
}