Skip to content

Commit 11a2d9c

Browse files
committed
Merge pull request bitly#13 from ianozsvald/master
Username/AccessToken provided via Environment variables
2 parents d0895dd + f98f3e4 commit 11a2d9c

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ bitly API python library
55

66
pip install bitly_api
77

8+
## Run tests
9+
10+
Your username is the lowercase name shown when you login to bitly, your access token can be fetched using the following ( http://dev.bitly.com/authentication.html ):
11+
12+
curl -u "username:password" -X POST "https://api-ssl.bitly.com/oauth/access_token"
13+
14+
To run the tests either export the environment variable or set it up inline before calling `nosetests`:
15+
16+
bitly-api-python $ BITLY_ACCESS_TOKEN=<accesstoken> nosetests
17+
818
## API Documentation
919

1020
http://dev.bitly.com/

test/test_bitly_api.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,53 @@
11
#!/usr/local/bin/python
22
"""
33
This is a py.test script
4+
5+
Example usage on Unix:
6+
bitly-api-python $ BITLY_ACCESS_TOKEN=<accesstoken> nosetests
7+
or 'export' the two environment variables prior to running nosetests
48
"""
9+
import os
510
import sys
611
sys.path.append('../')
712
import bitly_api
813

14+
BITLY_ACCESS_TOKEN = "BITLY_ACCESS_TOKEN"
15+
16+
17+
def get_connection():
18+
"""Create a Connection base on username and access token credentials"""
19+
if BITLY_ACCESS_TOKEN not in os.environ:
20+
raise ValueError("Environment variable '{}' required".format(BITLY_ACCESS_TOKEN))
21+
access_token = os.getenv(BITLY_ACCESS_TOKEN)
22+
bitly = bitly_api.Connection(access_token=access_token)
23+
return bitly
24+
25+
926
def testApi():
10-
bitly = bitly_api.Connection('bitlyapidemo','R_0da49e0a9118ff35f52f629d2d71bf07')
27+
bitly = get_connection()
1128
data = bitly.shorten('http://google.com/')
12-
assert data != None
29+
assert data is not None
1330
assert data['long_url'] == 'http://google.com/'
14-
assert data['hash'] != None
15-
31+
assert data['hash'] is not None
32+
33+
1634
def testExpand():
17-
bitly = bitly_api.Connection('bitlyapidemo','R_0da49e0a9118ff35f52f629d2d71bf07')
18-
data = bitly.expand(hash='test1')
19-
assert data != None
35+
bitly = get_connection()
36+
data = bitly.expand(hash='test1_random_fjslfjieljfklsjflkas')
37+
assert data is not None
2038
assert len(data) == 1
2139
assert data[0]['error'] == 'NOT_FOUND'
22-
40+
41+
2342
def testReferrer():
24-
bitly = bitly_api.Connection('bitlyapidemo','R_0da49e0a9118ff35f52f629d2d71bf07')
43+
bitly = get_connection()
2544
data = bitly.referrers(hash='a')
26-
assert data != None
45+
assert data is not None
2746
assert len(data) > 1
2847

48+
2949
def testProDomain():
30-
bitly = bitly_api.Connection('bitlyapidemo','R_0da49e0a9118ff35f52f629d2d71bf07')
50+
bitly = get_connection()
3151
test_data = {
3252
'cnn.com': False,
3353
'nyti.ms': True,

0 commit comments

Comments
 (0)