Skip to content

Commit 142fe5d

Browse files
authored
Merge pull request #14 from randyzwitch/remove-requests
WIP: Remove requests.jl, add docstrings
2 parents 92c2095 + 472a701 commit 142fe5d

File tree

5 files changed

+159
-30
lines changed

5 files changed

+159
-30
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
language: julia
22
os:
33
- linux
4-
- osx
54
julia:
65
- 0.6
7-
- nightly
86
notifications:
97
email: false
108
script:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OAuth
22

3-
OSX/Linux: [![Build Status](https://travis-ci.org/randyzwitch/OAuth.jl.svg?branch=master)](https://travis-ci.org/randyzwitch/OAuth.jl) <br>
3+
Linux: [![Build Status](https://travis-ci.org/randyzwitch/OAuth.jl.svg?branch=master)](https://travis-ci.org/randyzwitch/OAuth.jl) <br>
44
Windows:
55
[![Build status](https://ci.appveyor.com/api/projects/status/lu2jwu1yh464bdm4/branch/master?svg=true)](https://ci.appveyor.com/project/randyzwitch/oauth-jl/branch/master)
66
<br>

REQUIRE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
julia 0.6
22
FactCheck
33
Nettle 0.2
4-
Requests
5-
URIParser
4+
HTTP

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ environment:
22
matrix:
33
- JULIAVERSION: "julialang/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
44
- JULIAVERSION: "julialang/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
5-
- JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe"
6-
- JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe"
5+
# - JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe"
6+
# - JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe"
77

88
branches:
99
only:

src/OAuth.jl

Lines changed: 155 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ __precompile__()
22

33
module OAuth
44

5-
using URIParser, Requests, Nettle
5+
using HTTP, Nettle
66

77
export
88
oauth_timestamp,
@@ -19,39 +19,94 @@ oauth_body_hash_encode,
1919
oauth_header,
2020
oauth_request_resource
2121

22+
"""
23+
oauth_timestamp()
2224
23-
#############################################################
24-
#
25-
# OAuth Client Functions
26-
#
27-
#############################################################
25+
Returns current unix timestamp as String.
2826
29-
#Get current timestamp
27+
# Examples
28+
```julia-repl
29+
julia> oauth_timestamp()
30+
"1512235859"
31+
```
32+
"""
3033
function oauth_timestamp()
3134
"$(round(Int, time()))"
3235
end
3336

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+
"""
3548
function oauth_nonce(length::Int)
3649
randstring(length)
3750
end
3851

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)
4164
base64encode(digest("sha1", signingkey, message))
4265
end
4366

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+
"""
4578
function oauth_signing_key(oauth_consumer_secret::String, oauth_token_secret::String)
4679
"$(oauth_consumer_secret)&$(oauth_token_secret)"
4780
end
4881

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+
"""
5093
function oauth_signature_base_string(httpmethod::String, url::String, parameterstring::String)
5194
"$(httpmethod)&$(encodeURI(url))&$(encodeURI(parameterstring))"
5295
end
5396

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+
"""
55110
function oauth_percent_encode_keys!(options::Dict)
56111
#options encoded
57112
originalkeys = collect(keys(options))
@@ -74,15 +129,49 @@ end
74129
oauth_percent_encode_keys!(options::Dict)
75130
)
76131

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+
"""
78143
oauth_serialize_url_parameters(options::Dict) = join(
79144
["$key=$(options[key])" for key in sort!(collect(keys(options)))],
80145
"&"
81146
)
82147

83148
# 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`.
85153
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+
"""
86175
function encodeURI!(dict_of_parameters::Dict)
87176
for (k, v) in dict_of_parameters
88177
if typeof(v) <: String
@@ -97,23 +186,60 @@ end
97186
encodeURI!(dict_of_parameters::Dict)
98187
)
99188

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+
"""
100201
function oauth_body_hash_file(filename::String)
101202
oauth_body_hash_data(readstring(open(filename)))
102203
end
103204

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+
"""
104216
function oauth_body_hash_data(data::String)
105217
"oauth_body_hash=$(oauth_body_hash_encode(data))"
106218
end
107219

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+
"""
108231
function oauth_body_hash_encode(data::String)
109232
base64encode(digest("SHA1", data))
110233
end
111234

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")
117243

118244
#keys for parameter string
119245
options["oauth_consumer_key"] = oauth_consumer_key
@@ -142,9 +268,15 @@ function oauth_header(httpmethod, baseurl, options, oauth_consumer_key, oauth_co
142268

143269
end
144270

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+
"""
145277
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)
146278
#Build query string
147-
query_str = Requests.format_query_str(options)
279+
query_str = HTTP.escape(options)
148280

149281
#Build oauth_header
150282
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
157289
)
158290

159291
if uppercase(httpmethod) == "POST"
160-
return Requests.post(URI(endpoint), query_str; headers = headers)
292+
return HTTP.post(endpoint, query_str; headers = headers)
161293
elseif uppercase(httpmethod) == "GET"
162-
return Requests.get(URI("$(endpoint)?$query_str"); headers = headers)
294+
return HTTP.get("$(endpoint)?$query_str"; headers = headers)
163295
end
164296
end
165297

0 commit comments

Comments
 (0)