Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
Add LICENSE, gocover README badge, additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dghubble committed Apr 25, 2015
1 parent 8eea690 commit d3037cd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Dalton Hubble

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@


# go-twitter [![Build Status](https://travis-ci.org/dghubble/go-twitter.png)](https://travis-ci.org/dghubble/go-twitter) [![GoDoc](http://godoc.org/github.com/dghubble/go-twitter?status.png)](http://godoc.org/github.com/dghubble/go-twitter)
# go-twitter [![Build Status](https://travis-ci.org/dghubble/go-twitter.png)](https://travis-ci.org/dghubble/go-twitter) [![Coverage](http://gocover.io/_badge/github.com/dghubble/go-twitter/twitter)](http://gocover.io/github.com/dghubble/go-twitter/twitter) [![GoDoc](http://godoc.org/github.com/dghubble/go-twitter?status.png)](http://godoc.org/github.com/dghubble/go-twitter)

go-twitter is an (IN PROGRESS) Go client library for the [Twitter API](https://dev.twitter.com/rest/public).
go-twitter is an (in progress) Go client library for the [Twitter API](https://dev.twitter.com/rest/public).

## Features

Expand Down Expand Up @@ -41,13 +41,13 @@ params := &twitter.UserShowParams{ScreenName: "dghubble"}
user, resp, err := client.Users.Show(params)
```

Required parameters are passed as positional arguments. Optional parameters are passed via a typed params struct for each endpoint, which provides type safety, handles encoding, and accomodates future changes.
Required parameters are passed as positional arguments. Optional parameters are passed via a typed params struct for each endpoint.

Method names match the Twitter API endpoint names, except timeline-type endpoints are provided by `TimelineService` rather than `StatusService`.

## Authentication

By design, the `twitter` package client is decoupled from authentication concerns. Twitter "user auth" and "app auth" endpoints require [OAuth1](https://tools.ietf.org/html/rfc5849) and [OAuth2](https://tools.ietf.org/html/rfc6749), respectively. Use the [dghubble/oauth1](https://github.com/dghubble/oauth1) and [golang/oauth2](https://github.com/golang/oauth2/) libraries to obtain an `http.Client`, which transparently handles authorizing requests, and pass it to `twitter` `NewClient`.
By design, the `twitter` package client is decoupled from authentication concerns. Twitter "user auth" and "app auth" endpoints require [OAuth1](https://tools.ietf.org/html/rfc5849) and [OAuth2](https://tools.ietf.org/html/rfc6749), respectively. Use the [dghubble/oauth1](https://github.com/dghubble/oauth1) and [golang/oauth2](https://github.com/golang/oauth2/) libraries to obtain an `http.Client`, which transparently handles authorizing requests.

For example, make requests as a consumer on behalf of a user who has granted access, with OAuth1 "user auth":

Expand Down Expand Up @@ -87,3 +87,7 @@ client := twitter.NewClient(authClient)
```

Now use your `twitter` client to make requests.

## License

[MIT License](LICENSE)
28 changes: 26 additions & 2 deletions twitter/statuses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func TestStatusService_Update(t *testing.T) {
mux.HandleFunc("/1.1/statuses/update.json", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, "POST", r)
assertQuery(t, map[string]string{}, r)
assertPostForm(t, map[string]string{"status": "very informative tweet", "media_ids": "123456789,987654321"}, r)
assertPostForm(t, map[string]string{"status": "very informative tweet", "media_ids": "123456789,987654321", "lat": "37.826706", "long": "-122.42219"}, r)
fmt.Fprintf(w, `{"id": 581980947630845953, "text": "very informative tweet"}`)
})

client := NewClient(httpClient)
params := &StatusUpdateParams{MediaIds: []int64{123456789, 987654321}}
params := &StatusUpdateParams{MediaIds: []int64{123456789, 987654321}, Lat: Float(37.826706), Long: Float(-122.422190)}
tweet, _, err := client.Statuses.Update("very informative tweet", params)
if err != nil {
t.Errorf("Statuses.Update error %v", err)
Expand Down Expand Up @@ -128,6 +128,18 @@ func TestStatusService_Retweet(t *testing.T) {
}
}

func TestStatusService_RetweetHandlesNilParams(t *testing.T) {
httpClient, mux, server := testServer()
defer server.Close()

mux.HandleFunc("/1.1/statuses/retweet/20.json", func(w http.ResponseWriter, r *http.Request) {
assertPostForm(t, map[string]string{"id": "20"}, r)
})

client := NewClient(httpClient)
client.Statuses.Retweet(20, nil)
}

func TestStatusService_Destroy(t *testing.T) {
httpClient, mux, server := testServer()
defer server.Close()
Expand All @@ -151,3 +163,15 @@ func TestStatusService_Destroy(t *testing.T) {
t.Errorf("Statuses.Destroy expected:\n%+v, got:\n %+v", expected, tweet)
}
}

func TestStatusService_DestroyHandlesNilParams(t *testing.T) {
httpClient, mux, server := testServer()
defer server.Close()

mux.HandleFunc("/1.1/statuses/destroy/40.json", func(w http.ResponseWriter, r *http.Request) {
assertPostForm(t, map[string]string{"id": "40"}, r)
})

client := NewClient(httpClient)
client.Statuses.Destroy(40, nil)
}
16 changes: 16 additions & 0 deletions twitter/twitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import (
"testing"
)

func TestNewClient(t *testing.T) {
client := NewClient(nil)
if client.sling.HttpClient != http.DefaultClient {
t.Errorf("expected default client %v, got %v", http.DefaultClient, client.sling.HttpClient)
}
if client.Statuses.sling == client.sling {
t.Errorf("Must pass StatusService a derived sling copy.")
}
if client.Timelines.sling == client.sling {
t.Errorf("Must pass TimelineService a derived sling copy.")
}
if client.Users.sling == client.sling {
t.Errorf("Must pass UserService a derived sling copy.")
}
}

// testing utils

// testServer returns an http Client, ServeMux, and Server. The client proxies
Expand Down

0 comments on commit d3037cd

Please sign in to comment.