예제 #1
0
// findPlans fetches plans details from a third-party API
func findPlans(circle string, operator string) ([]Plan, error) {
	var apiURL *url.URL
	apiURL, err := url.Parse("http://api.dataweave.in/v1/telecom_data/listByCircle/?")
	if err != nil {
		return nil, err
	}

	parameters := url.Values{}
	parameters.Add("api_key", "4efd9ff6fefe4968624aa22362272d427dec05e0")
	parameters.Add("operator", operator)
	parameters.Add("circle", circle)
	apiURL.RawQuery = parameters.Encode()

	content, err := utils.GetContent(apiURL.String())

	var apiResp APIRresponse
	err = json.Unmarshal(content, &apiResp)

	if err != nil {
		return nil, err
	}

	if apiResp.Status != 200 {
		return nil, errors.New(apiResp.StatusText)
	}

	return apiResp.List, err
}
예제 #2
0
// lookupMovie fetches movie details from a third-party API
func lookupMovie(term string) (*Movie, error) {
	var apiURL *url.URL
	apiURL, err := url.Parse("http://www.omdbapi.com")
	if err != nil {
		return nil, err
	}

	parameters := url.Values{}
	parameters.Add("t", term)
	parameters.Add("plot", "full")
	parameters.Add("r", "json")
	parameters.Add("tomatoes", "true")
	parameters.Add("v", "1")
	apiURL.RawQuery = parameters.Encode()

	content, err := utils.GetContent(apiURL.String(), nil)

	if err != nil {
		return nil, err
	}

	var movie Movie
	err = json.Unmarshal(content, &movie)
	if err != nil {
		return nil, err
	}

	if movie.IsValidResponse == "False" {
		return nil, errors.New(movie.IsValidResponse)
	}

	return &movie, err
}
예제 #3
0
파일: posts.go 프로젝트: Balamir/cowboy
func getPosts(daysAgo int) (*Posts, error) {
	var apiURL *url.URL
	apiURL, err := url.Parse(APIBaseURL + "/v1/posts")
	if err != nil {
		return nil, err
	}

	parameters := url.Values{}
	parameters.Add("days_ago", strconv.Itoa(daysAgo))
	apiURL.RawQuery = parameters.Encode()

	content, err := utils.GetContent(apiURL.String(), requestHeaders)

	if err != nil {
		return nil, err
	}

	var posts Posts
	err = json.Unmarshal(content, &posts)
	if err != nil {
		return nil, err
	}

	return &posts, err
}
예제 #4
0
파일: excuse.go 프로젝트: Balamir/cowboy
func getAnExcuse() (*Excuse, error) {
	var endpointURL *url.URL
	endpointURL, err := url.Parse("http://www.programmerexcuses.com/")
	if err != nil {
		return nil, err
	}

	content, err := utils.GetContent(endpointURL.String(), nil)
	if err != nil {
		return nil, err
	}

	doc, err := html.Parse(strings.NewReader(string(content)))
	if err != nil {
		return nil, err
	}

	var excuseText string

	var f func(*html.Node, bool)
	f = func(n *html.Node, canExtractText bool) {
		// Extract text content if node is a TextNode
		if canExtractText && n.Type == html.TextNode {
			excuseText = n.Data
		}

		// Tell whether text content can be extracted or not.
		// Here, we will recursively traverse for 'a' ElementNode.
		canExtractText = canExtractText || (n.Type == html.ElementNode && n.Data == "a")

		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c, canExtractText)
		}
	}
	f(doc, false)

	return &Excuse{Text: excuseText, Response: "true"}, err
}