Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bloveless committed May 4, 2020
1 parent ca2d962 commit 9c4208c
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ modeled this library after aws-sdk-go where the input to every endpoint is a wel
every endpoint is also a well-defined struct.

The easiest way to run the examples is to create an example/config.json then cd into the `example` directory and run
`go run .`. Put the following in the config.json files.
`go run .`. Put the following in the config.json file.

```json
{
Expand All @@ -16,9 +16,10 @@ The easiest way to run the examples is to create an example/config.json then cd
```

Below is a quick example of posting a tweet. You'll have to provide the consumer key, consumer secret, access token, and
access token secret yourself. The endpoints for getting the oauth request token and converting that into and oauth
access token have been implemented as OAuthRequestTokenGet and OAuthAccessTokenGet. In case you want to have your
application automatically authorize the user and retrieve access tokens on their behalf. Checkout the provided examples.
access token secret yourself. The endpoints for getting the oauth request token and converting that into an oauth
access token have been implemented as `OAuthRequestTokenGet` and `OAuthAccessTokenGet`. When you want to have your
application to automatically authorize the user and retrieve access tokens on their behalf checkout the provided
examples.

```go
package main
Expand All @@ -32,7 +33,7 @@ func main() {
tc.SetAccessKeys("OAuthAccessToken", "OAuthAccessTokenSecret")

input := tweetgo.StatusesUpdateInput{
Status: "Hello Ladies + Gentlemen, a signed OAuth request!",
Status: tweetgo.String("Hello Ladies + Gentlemen, a signed OAuth request!"),
}

output, err := tc.StatusesUpdatePost(input)
Expand All @@ -44,8 +45,8 @@ func main() {
}
```

If you look in the model.go you'll see that every option for updating a status is in the struct StatusesUpdateInput and
all the possible fields have been documented in the StatusesUpdateOutput.
If you look in the model.go you'll see that every option for updating a status is in the struct `StatusesUpdateInput`
and all the possible fields have been documented in the `StatusesUpdateOutput`.

If you follow these steps you can add any endpoints that you need easily and give back to the community!

Expand All @@ -57,10 +58,11 @@ If you are using Go mod in your project you can add something like the following
replace github.com/bloveless/tweetgo => ../tweetgo
```

to your go.mod file before any require lines in your go.mod (replace ../tweetgo with the path to your local copy of
to your go.mod file before any "require" lines in your go.mod (replace ../tweetgo with the path to your local copy of
tweetgo).

I learned about that command from this blog post. [Using "replace" in go.mod to point to your local module](https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/)
. Credit where credit is due!

## How to add a new endpoint

Expand All @@ -71,12 +73,12 @@ endpoint.
### Step 1: Create the input model

Refer to the link above to generate the input model. Below is the input I created for the statuses/user_timeline
endpoint. Twitter users form encoded input when receiving a request and responds in JSON. This doesn't change much
endpoint. Twitter uses form encoded input when receiving a request and responds in JSON. This doesn't change much
about implementing a new endpoint other than you'll use the "schema" tag when you are writing the input structs, and the
"json" tag when you are writing the output structs. Every input field needs to be a pointer so we can handle null
values. There are helper function for converting between standard types and pointers to make this a little easier.
Checkout `value.go` to see what I'm talking about. If you add a type that hasn't been used before, it will be helpful
for you to add a conversion func to `value.go`.
"json" tag when you are writing the output structs. Every input field needs to be a pointer so we can use nil values to
decide if a values should be encoded and sent to the endpoint or not. There are helper function for converting between
standard types and pointers to make this a little easier. Checkout `value.go` to see what I'm talking about. If you add
a type that hasn't been used before, it will be helpful for you to add a conversion func to `value.go`.

```go
type StatusesUserTimelineInput struct {
Expand All @@ -93,10 +95,10 @@ type StatusesUserTimelineInput struct {

### Step 2: Create the output model

Refer to the link above to generate the output model. There are only a few different types of returns from Twitter API's
so there are some utility structs that you can compose responses from. They are unexported and are intended to be
composed within other structs. The statuses user timeline endpoint returns a list of tweets so the output struct is very
simple.
Refer to the Twitter docs link above to see which fields are returned from the endpoint. There are only a few different
types of returns from Twitter API's so there are some utility structs that you can compose responses from. They are
unexported and are intended to be composed within other structs. The statuses user timeline endpoint returns a list of
Tweets so the output struct is very simple.

```go
type StatusesUserTimelineOutput struct {
Expand All @@ -106,7 +108,7 @@ type StatusesUserTimelineOutput struct {

### Step 3: Create endpoint code

All of the request signing and execution has been wrapped up into utility functions which you can find in `request.go`.
The request signing and execution has been wrapped up into utility functions which you can find in `request.go`.
Hopefully this will make adding new endpoints extremely simple. For this endpoint I added the following code to
`client.go`.

Expand Down Expand Up @@ -135,9 +137,9 @@ func (c Client) StatusesUserTimelineGet(input StatusesUserTimelineInput) ([]Stat

Follow the six steps above, and you'll have your own endpoint up and running.

NOTE: As far as I can tell twitter uses JSON output on nearly all of it's endpoints. The oauth endpoints use form value
rather than JSON output so support functions exist for processing that type of data as well. Look at OAuthRequestTokenGet
which uses gorilla/scheme for parsing the data into the output format if you find another endpoint that uses form values
rather than JSON.
NOTE: As far as I can tell twitter uses JSON output on nearly all of its endpoints. The oauth endpoints use form values
rather than JSON output so support functions exist for processing that type of data as well. Look at
OAuthRequestTokenGet which uses gorilla/scheme for parsing the data into the output format if you find another endpoint
that uses form values rather than JSON.

There you go! You've added your own endpoint!

0 comments on commit 9c4208c

Please sign in to comment.