Categorygithub.com/kylebevans/twitapi
repositorypackage
0.0.0-20201204234051-ecec2e804999
Repository: https://github.com/kylebevans/twitapi.git
Documentation: pkg.go.dev

# README

Go API client for twitapi

API Reference — v2

Quick example to get tweets from the last 7 days that match a search query

package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/antihax/optional"
	"github.com/kylebevans/twitapi"
)

func main() {

	ctx := context.WithValue(context.Background(), twitapi.ContextAccessToken, os.Getenv("TWITTER_BEARER_TOKEN"))
	cfg := twitapi.NewConfiguration()
	apiClient := twitapi.NewAPIClient(cfg)
	var searchOpts *twitapi.TweetsRecentSearchOpts
	searchOpts = new(twitapi.TweetsRecentSearchOpts)
	var tweets twitapi.TweetSearchResponse
	var nextToken optional.String
	var err error

	// API is paginated, so print pages of recent tweets in the last week
	// that mention "tasty sandwich" until they are all done
	for ok := true; ok; ok = (tweets.Meta.NextToken != "") {

		tweets, _, err = apiClient.SearchApi.TweetsRecentSearch(ctx, "\"tasty sandwich\"", searchOpts)

		if err != nil {
			panic(err)
		}

		for _, v := range tweets.Data {
			fmt.Printf("%v\n\u001b[31m--------\n\u001b[0m", v.Text)
		}

		nextToken = optional.NewString(tweets.Meta.NextToken)
		searchOpts.NextToken = nextToken
		time.Sleep(2 * time.Second) // Twitter API is rate limited to 450 requests per 15 min
	}
}

Overview

This API client was generated by the OpenAPI Generator project. By using the OpenAPI-spec from a remote server, you can easily generate an API client.

  • API version: 2.2
  • Package version: 1.0.0
  • Build package: org.openapitools.codegen.languages.GoClientCodegen For more information, please visit https://developer.twitter.com/

Installation

Install the following dependencies:

go get github.com/stretchr/testify/assert
go get golang.org/x/oauth2
go get golang.org/x/net/context
go get github.com/antihax/optional

Put the package under your project folder and add the following in import:

import "./twitapi"

Documentation for API Endpoints

All URIs are relative to https://api.twitter.com

ClassMethodHTTP requestDescription
GeneralApiGetOpenApiSpecGet /2/openapi.jsonReturns the open api spec document.
RulesApiAddOrDeleteRulesPost /2/tweets/search/stream/rulesAdd or delete rules from a user's active rule set.
RulesApiGetRulesGet /2/tweets/search/stream/rulesReturns rules from a user's active rule set.
SearchApiTweetsFullarchiveSearchGet /2/tweets/search/allReturns Tweets that match a search query.
SearchApiTweetsRecentSearchGet /2/tweets/search/recentReturns Tweets from the last 7 days that match a search query.
TweetsApiAddOrDeleteRulesPost /2/tweets/search/stream/rulesAdd or delete rules from a user's active rule set.
TweetsApiFindTweetByIdGet /2/tweets/{id}Returns hydrated Tweet objects
TweetsApiFindTweetsByIdGet /2/tweetsReturns hydrated Tweet objects
TweetsApiGetRulesGet /2/tweets/search/stream/rulesReturns rules from a user's active rule set.
TweetsApiHideReplyByIdPut /2/tweets/{id}/hiddenHides or unhides a reply to an owned conversation.
TweetsApiSampleStreamGet /2/tweets/sample/streamStreams a deterministic 1% of public tweets.
TweetsApiSearchStreamGet /2/tweets/search/streamStreams tweets matching a user's active rule set.
TweetsApiTweetsFullarchiveSearchGet /2/tweets/search/allReturns Tweets that match a search query.
TweetsApiTweetsRecentSearchGet /2/tweets/search/recentReturns Tweets from the last 7 days that match a search query.
UsersApiFindUserByIdGet /2/users/{id}Return details for the specified users
UsersApiFindUserByUsernameGet /2/users/by/username/{username}Return details for the specified users
UsersApiFindUsersByIdGet /2/usersReturn details for the specified users
UsersApiFindUsersByUsernameGet /2/users/byReturn details for the specified users

Documentation For Models

Documentation For Authorization

BearerToken

  • Type: HTTP basic authentication

Example

auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
    UserName: "username",
    Password: "password",
})
r, err := client.Service.Operation(auth, args)

UserToken

  • Type: HTTP basic authentication

Example

auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
    UserName: "username",
    Password: "password",
})
r, err := client.Service.Operation(auth, args)

Author