@@ -2,7 +2,7 @@ __precompile__()
2
2
3
3
module OAuth
4
4
5
- using URIParser, Requests , Nettle
5
+ using HTTP , Nettle
6
6
7
7
export
8
8
oauth_timestamp,
@@ -19,39 +19,94 @@ oauth_body_hash_encode,
19
19
oauth_header,
20
20
oauth_request_resource
21
21
22
+ """
23
+ oauth_timestamp()
22
24
23
- # ############################################################
24
- #
25
- # OAuth Client Functions
26
- #
27
- # ############################################################
25
+ Returns current unix timestamp as String.
28
26
29
- # Get current timestamp
27
+ # Examples
28
+ ```julia-repl
29
+ julia> oauth_timestamp()
30
+ "1512235859"
31
+ ```
32
+ """
30
33
function oauth_timestamp ()
31
34
" $(round (Int, time ())) "
32
35
end
33
36
34
- # Generate random string
37
+ """
38
+ oauth_nonce(length::Int)
39
+
40
+ Returns a random string of a given length.
41
+
42
+ # Examples
43
+ ```julia-repl
44
+ julia> oauth_nonce(10)
45
+ "aQb2FVkrYi"
46
+ ```
47
+ """
35
48
function oauth_nonce (length:: Int )
36
49
randstring (length)
37
50
end
38
51
39
- # HMAC-SHA1 sign message
40
- function oauth_sign_hmac_sha1 (message:: String ,signingkey:: String )
52
+ """
53
+ oauth_sign_hmac_sha1(message::String, signingkey::String)
54
+
55
+ Takes a message and signing key, converts to a SHA-1 digest, then encodes to base64.
56
+
57
+ # Examples
58
+ ```jldoctest
59
+ julia> oauth_sign_hmac_sha1("foo", "bar")
60
+ "hdFVxV7ShqMAvRzxJN4I2H6RTzo="
61
+ ```
62
+ """
63
+ function oauth_sign_hmac_sha1 (message:: String , signingkey:: String )
41
64
base64encode (digest (" sha1" , signingkey, message))
42
65
end
43
66
44
- # Create signing key
67
+ """
68
+ oauth_signing_key(oauth_consumer_secret::String, oauth_token_secret::String)
69
+
70
+ Returns a signing key based on a consumer secret and token secret.
71
+
72
+ # Examples
73
+ ```jldoctest
74
+ julia> oauth_signing_key("foo", "bar")
75
+ "foo&bar"
76
+ ```
77
+ """
45
78
function oauth_signing_key (oauth_consumer_secret:: String , oauth_token_secret:: String )
46
79
" $(oauth_consumer_secret) &$(oauth_token_secret) "
47
80
end
48
81
49
- # Create signature_base_string
82
+ """
83
+ oauth_signature_base_string(httpmethod::String, url::String, parameterstring::String)
84
+
85
+ Returns encoded HTTP method, url and parameters.
86
+
87
+ # Examples
88
+ ```jldoctest
89
+ julia> oauth_signature_base_string("POST", "https://julialang.org", "foo&bar")
90
+ "POST&https%3A%2F%2Fjulialang.org&foo%26bar"
91
+ ```
92
+ """
50
93
function oauth_signature_base_string (httpmethod:: String , url:: String , parameterstring:: String )
51
94
" $(httpmethod) &$(encodeURI (url)) &$(encodeURI (parameterstring)) "
52
95
end
53
96
54
- # URL-escape keys
97
+ """
98
+ oauth_percent_encode_keys!(options::Dict)
99
+
100
+ Returns dict where keys and values are URL-encoded.
101
+
102
+ # Examples
103
+ ```jldoctest
104
+ julia> oauth_percent_encode_keys!(Dict("key 1" => "value1", "key 2" => "value 2"))
105
+ Dict{String,String} with 2 entries:
106
+ "key%20%20%20%202" => "value%202"
107
+ "key%201" => "value1"
108
+ ```
109
+ """
55
110
function oauth_percent_encode_keys! (options:: Dict )
56
111
# options encoded
57
112
originalkeys = collect (keys (options))
74
129
oauth_percent_encode_keys! (options:: Dict )
75
130
)
76
131
77
- # Create query string from dictionary keys
132
+ """
133
+ oauth_serialize_url_parameters(options::Dict)
134
+
135
+ Returns query string by concatenating dictionary keys/values.
136
+
137
+ # Examples
138
+ ```jldoctest
139
+ julia> bar([1, 2], [1, 2])
140
+ 1
141
+ ```
142
+ """
78
143
oauth_serialize_url_parameters (options:: Dict ) = join (
79
144
[" $key =$(options[key]) " for key in sort! (collect (keys (options)))],
80
145
" &"
81
146
)
82
147
83
148
# See: https://github.com/randyzwitch/OAuth.jl/issues/3
84
- encodeURI (s) = URIParser. escape (s)
149
+ """
150
+ encodeURI(s)
151
+
152
+ Convenience function for `HTTP.escape`.
85
153
154
+ # Examples
155
+ ```jldoctest
156
+ julia> encodeURI("hello, world!")
157
+ "hello%2C%20world%21"
158
+ ```
159
+ """
160
+ encodeURI (s) = HTTP. escape (s)
161
+
162
+ """
163
+ encodeURI!(dict_of_parameters::Dict)
164
+
165
+ Mutates dict_of_parameters using `encodeURI` on strings.
166
+
167
+ # Examples
168
+ ```jldoctest
169
+ julia> encodeURI!(Dict("iv" => 10, "s" => "value!"))
170
+ Dict{String,Any} with 2 entries:
171
+ "iv" => 10
172
+ "s" => "value%21"
173
+ ```
174
+ """
86
175
function encodeURI! (dict_of_parameters:: Dict )
87
176
for (k, v) in dict_of_parameters
88
177
if typeof (v) <: String
97
186
encodeURI! (dict_of_parameters:: Dict )
98
187
)
99
188
189
+ """
190
+ oauth_body_hash_file(filename::String)
191
+
192
+ Compute the Bar index between `x` and `y`. If `y` is missing, compute
193
+ the Bar index between all pairs of columns of `x`.
194
+
195
+ # Examples
196
+ ```jldoctest
197
+ julia> oauth_body_hash_file(joinpath(dirname(@__FILE__), "auth_body_hash_file.txt"))
198
+ "oauth_body_hash=CgqfKmdylCVXq1NV12r0Qvj2XgE="
199
+ ```
200
+ """
100
201
function oauth_body_hash_file (filename:: String )
101
202
oauth_body_hash_data (readstring (open (filename)))
102
203
end
103
204
205
+ """
206
+ oauth_body_hash_data(data::String)
207
+
208
+ Returns `oauth_body_hash=` along with base64 encoded SHA-1 from input.
209
+
210
+ # Examples
211
+ ```jldoctest
212
+ julia> oauth_body_hash_data("Hello, World!")
213
+ "oauth_body_hash=CgqfKmdylCVXq1NV12r0Qvj2XgE="
214
+ ```
215
+ """
104
216
function oauth_body_hash_data (data:: String )
105
217
" oauth_body_hash=$(oauth_body_hash_encode (data)) "
106
218
end
107
219
220
+ """
221
+ oauth_body_hash_encode(data::String)
222
+
223
+ Convenience function for SHA-1 and base64 encoding.
224
+
225
+ # Examples
226
+ ```jldoctest
227
+ julia> oauth_body_hash_encode("julialang")
228
+ "Lsztg2byou89Y8lBoH3G8v3vjbw="
229
+ ```
230
+ """
108
231
function oauth_body_hash_encode (data:: String )
109
232
base64encode (digest (" SHA1" , data))
110
233
end
111
234
112
- # Use this function to build the header for every OAuth call
113
- # This function assumes that options Dict has already been run through encodeURI!
114
- function oauth_header (httpmethod, baseurl, options, oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret;
115
- oauth_signature_method = " HMAC-SHA1" ,
116
- oauth_version = " 1.0" )
235
+ """
236
+ function oauth_header(httpmethod, baseurl, options, oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret; oauth_signature_method = "HMAC-SHA1", oauth_version = "1.0")
237
+
238
+ Builds OAuth header, defaulting to OAuth 1.0. Function assumes `options` has already
239
+ been run through `encodeURI!`.
240
+
241
+ """
242
+ function oauth_header (httpmethod, baseurl, options, oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret; oauth_signature_method = " HMAC-SHA1" , oauth_version = " 1.0" )
117
243
118
244
# keys for parameter string
119
245
options[" oauth_consumer_key" ] = oauth_consumer_key
@@ -142,9 +268,15 @@ function oauth_header(httpmethod, baseurl, options, oauth_consumer_key, oauth_co
142
268
143
269
end
144
270
271
+ """
272
+ oauth_request_resource(endpoint::String, httpmethod::String, options::Dict, oauth_consumer_key::String, oauth_consumer_secret::String, oauth_token::String, oauth_token_secret::String)
273
+
274
+ Makes `GET` or `POST` call to OAuth API.
275
+
276
+ """
145
277
function oauth_request_resource (endpoint:: String , httpmethod:: String , options:: Dict , oauth_consumer_key:: String , oauth_consumer_secret:: String , oauth_token:: String , oauth_token_secret:: String )
146
278
# Build query string
147
- query_str = Requests . format_query_str (options)
279
+ query_str = HTTP . escape (options)
148
280
149
281
# Build oauth_header
150
282
oauth_header_val = oauth_header (httpmethod, endpoint, options, oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret)
@@ -157,9 +289,9 @@ function oauth_request_resource(endpoint::String, httpmethod::String, options::D
157
289
)
158
290
159
291
if uppercase (httpmethod) == " POST"
160
- return Requests . post (URI ( endpoint) , query_str; headers = headers)
292
+ return HTTP . post (endpoint, query_str; headers = headers)
161
293
elseif uppercase (httpmethod) == " GET"
162
- return Requests . get (URI ( " $(endpoint) ?$query_str " ) ; headers = headers)
294
+ return HTTP . get (" $(endpoint) ?$query_str " ; headers = headers)
163
295
end
164
296
end
165
297
0 commit comments