Skip to content

mrbenosborne/tripadvisor-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TripAdvisor Golang

A TripAdvisor API wrapper for Golang.

Build Status License: MIT Go Report Card

Godoc

Godoc reference is available here.

Installation

go get "github.com/mrbenosborne/tripadvisor-golang"

Options

SetKey

Set the TripAdvisor API key to use for all HTTP requests.

SetKey(string)

SetEndpoint

Set the TripAdvisor endpoint, only version 2 of the TripAdvisor API is currently supported so use at your own risk.

SetEndpoint(string)

SetLanguageCode

Set the language code for TripAdvisor responses, a full list of supported codes can be below:

TripAdvisor API documentation page.

SetLanguageCode(string)

SetTimeout

Set a timeout duration for all HTTP requests, the default is 30 seconds.

SetTimeout(time.Duration)

Usage

An example use of the library is below.

package main

import (
	"context"
	"log"

	"github.com/mrbenosborne/tripadvisor-golang/pkg/tripadvisor"
)

func main() {

    // Create a new client
	tClient := tripadvisor.New(
		tripadvisor.SetKey("XXXXXXXXXXXXXXXXXXXXXX"),
		tripadvisor.SetLanguageCode("en_UK"),
    )

    // Get reviews for a location (The View from the Shard)
	response, err := tClient.Location(context.Background(), 3539289)
	if err != nil {
		panic(err)
	}

    // Print location data
	log.Printf("%s - Total number of reviews: %s\n", response.Name, response.NumReviews)
	for _, review := range response.Reviews {
		log.Printf("Review:\n\t%s\n", review.Content)
	}
}

Example Response

The data below is an example output of the Location response in JSON format:

{
  "name": "The View from The Shard",
  "num_reviews": "17349",
  "category": {
    "name": "attraction",
    "localized_name": "Attraction"
  },
  "address_obj": {
    "street1": "Joiner Street",
    "city": "London",
    "country": "United Kingdom",
    "postalcode": "SE1",
    "address_string": "Joiner Street, London SE1 England"
  },
  "latitude": "51.5045",
  "longitude": "-0.0865",
  "rating": "4.5",
  "location_id": "3539289",
  "trip_types": [
    {
      "name": "business",
      "value": "318",
      "localized_name": "Business"
    },
    {
      "name": "couples",
      "value": "7458",
      "localized_name": "Couples"
    },
    {
      "name": "solo",
      "value": "960",
      "localized_name": "Solo travel"
    },
    {
      "name": "family",
      "value": "3426",
      "localized_name": "Family"
    },
    {
      "name": "friends"