-
Notifications
You must be signed in to change notification settings - Fork 5
/
dweet.py
94 lines (70 loc) · 2.54 KB
/
dweet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import requests
#A Class for using the Dweet.io servers for data communication between devices
class Dweet(object):
#dweet root domain and endpoints
BASE_URL = "http://dweet.io/"
#creates a name for the thing dweeting.
#for anonymous or first time use.
#assigns a random dweet name to the thing.
DWEET = "{0}{1}".format(BASE_URL , "dweet?")
#dweet by thing name.
DWEET_BY_NAME = "{0}{1}".format(BASE_URL, "dweet/for/{name}?")
#get latest dweets by name.
LATEST_DWEET = "{0}{1}".format(BASE_URL, "get/latest/dweet/for/{name}")
#get all dweets by name.
ALL_DWEETS = "{0}{1}".format(BASE_URL, "get/dweets/for/{name}")
def dweet(self, data):
"""
Make a dweet without a thing name.
Assigns a random thing name which is returned
in the response body.
Returns a dict type.
Parameter name is a string type.
Parameter data is a dict type.
Usage:
data = {"foo": "bar"}
is turned into:
/dweet?foo=bar
"""
try:
return requests.get(self.DWEET, params=data).json()
except requests.exceptions.ConnectionError, e:
raise e
def dweet_by_name(self, name, data):
"""
Make a dweet with a named thing.
Returns a dict type.
Parameter name is a string type.
Parameter data is a dict type.
Usage:
data = {"foo": "bar"}
is turned into:
/{name}?foo=bar
"""
try:
return requests.get(self.DWEET_BY_NAME.format(name=name),
params=data).json()
except requests.exceptions.ConnectionError, e:
raise e
def latest_dweet(self, name):
"""
Get the latest dweet by thing name.
Only returns one dweet as response.
Returns dict type.
Parameter name is a string type.
"""
try:
return requests.get(self.LATEST_DWEET.format(name=name)).json()
except requests.exceptions.ConnectionError, e:
raise e
def all_dweets(self, name):
"""
Get dweets in last 24 hours by thing name.
Dweet limit currently is 500 dweets.
Returns dict type.
Parameter name is a string type.
"""
try:
return requests.get(self.ALL_DWEETS.format(name=name)).json()
except requests.exceptions.ConnectionError, e:
raise e