@@ -9,15 +9,16 @@ oauth_nonce,
9
9
oauth_sign_hmac_sha1,
10
10
oauth_signing_key,
11
11
oauth_signature_base_string,
12
- oauth_percent_encode_keys,
12
+ oauth_percent_encode_keys! ,
13
13
oauth_serialize_url_parameters,
14
- encodeURI,
14
+ encodeURI! ,
15
15
oauth_body_hash_file,
16
16
oauth_body_hash_data,
17
17
oauth_body_hash_encode,
18
18
oauth_header,
19
19
oauth_request_resource
20
20
21
+
21
22
# ############################################################
22
23
#
23
24
# OAuth Client Functions
@@ -52,71 +53,67 @@ function oauth_signature_base_string(httpmethod::String, url::String, parameters
52
53
end
53
54
54
55
# URL-escape keys
55
- function oauth_percent_encode_keys (options:: Dict )
56
+ function oauth_percent_encode_keys! (options:: Dict )
56
57
# options encoded
57
58
originalkeys = collect (keys (options))
58
59
59
60
for key in originalkeys
60
- options[encodeURI (" $key " )] = encodeURI (options[" $key " ])
61
- if encodeURI (" $key " ) != key
62
- delete! (options, " $key " )
63
- end
61
+ key_str = string (key)
62
+ encoded_key = encodeURI (key_str)
63
+
64
+ options[encoded_key] = encodeURI (options[key_str])
65
+ if encodeURI (key_str) != key
66
+ delete! (options, key_str)
67
+ end
64
68
end
65
69
66
70
options
67
71
end
68
72
69
- # Create query string from dictionary keys
70
- function oauth_serialize_url_parameters (options:: Dict )
71
- # Sort keys
72
- keyssorted = sort! (collect (keys (options)))
73
-
74
- # Build query string, remove trailing &
75
- parameterstring = " "
76
- for key in keyssorted
77
- parameterstring *= " $key =$(options[" $key " ]) &"
78
- end
73
+ @deprecate (
74
+ oauth_percent_encode_keys (options:: Dict ),
75
+ oauth_percent_encode_keys! (options:: Dict )
76
+ )
79
77
80
- chop (parameterstring)
81
- end
78
+ # Create query string from dictionary keys
79
+ oauth_serialize_url_parameters (options:: Dict ) = join (
80
+ [" $key =$(options[key]) " for key in sort! (collect (keys (options)))],
81
+ " &"
82
+ )
82
83
83
84
# See: https://github.com/randyzwitch/OAuth.jl/issues/3
84
85
encodeURI (s) = URIParser. escape (s)
85
86
86
- function encodeURI (dict_of_parameters:: Dict )
87
+ function encodeURI! (dict_of_parameters:: Dict )
87
88
for (k, v) in dict_of_parameters
88
89
if typeof (v) <: String
89
- dict_of_parameters[" $k " ] = encodeURI (v)
90
- else
91
- dict_of_parameters[" $k " ] = v
90
+ dict_of_parameters[k] = encodeURI (v)
92
91
end
93
92
end
94
93
return dict_of_parameters
95
94
end
96
95
97
- # Combine with oauth_body_hash_file as one function with two methods?
96
+ @deprecate (
97
+ encodeURI (dict_of_parameters:: Dict ),
98
+ encodeURI! (dict_of_parameters:: Dict )
99
+ )
100
+
98
101
function oauth_body_hash_file (filename:: String )
99
- filecontents = readall (open (filename))
100
- oauth_body_hash_data (filecontents)
102
+ oauth_body_hash_data (readall (open (filename)))
101
103
end
102
104
103
- # Combine with oauth_body_hash_file as one function with two methods?
104
105
function oauth_body_hash_data (data:: String )
105
- bodyhash = oauth_body_hash_encode (data)
106
- " oauth_body_hash=$(bodyhash) "
106
+ " oauth_body_hash=$(oauth_body_hash_encode (data)) "
107
107
end
108
108
109
- # Use functions from Nettle
110
109
function oauth_body_hash_encode (data:: String )
111
110
h = HashState (SHA1)
112
111
update! (h, data)
113
112
base64encode (digest! (h))
114
113
end
115
114
116
115
# Use this function to build the header for every OAuth call
117
- # This function assumes that options Dict has already been run through encodeURI
118
- # Use this function to build the header for every OAuth call
119
- # This function assumes that options Dict has already been run through encodeURI
116
+ # This function assumes that options Dict has already been run through encodeURI!
120
117
function oauth_header (httpmethod, baseurl, options, oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret;
121
118
oauth_signature_method = " HMAC-SHA1" ,
122
119
oauth_version = " 1.0" )
@@ -130,7 +127,7 @@ function oauth_header(httpmethod, baseurl, options, oauth_consumer_key, oauth_co
130
127
options[" oauth_version" ] = oauth_version
131
128
132
129
# options encoded
133
- options = oauth_percent_encode_keys (options)
130
+ oauth_percent_encode_keys! (options)
134
131
135
132
# Create ordered query string
136
133
parameterstring = oauth_serialize_url_parameters (options)
@@ -149,31 +146,26 @@ function oauth_header(httpmethod, baseurl, options, oauth_consumer_key, oauth_co
149
146
end
150
147
151
148
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 )
152
-
153
149
# Build query string
154
150
query_str = Requests. format_query_str (options)
155
151
156
152
# Build oauth_header
157
- # oauth_header_val = oauth_header(httpmethod, endpoint, options)
158
153
oauth_header_val = oauth_header (httpmethod, endpoint, options, oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret)
159
154
160
155
# Make request
161
- if uppercase (httpmethod) == " POST "
162
- return Requests . post ( URI (endpoint),
163
- query_str;
164
- headers = @compat ( Dict {String,String} (
165
- " Content-Type " => " application/x-www-form-urlencoded " ,
166
- " Authorization " => oauth_header_val,
167
- " Accept " => " */* " )) )
156
+ headers = @compat (
157
+ Dict {String,String} (
158
+ " Content-Type " => " application/x-www-form-urlencoded " ,
159
+ " Authorization " => oauth_header_val,
160
+ " Accept " => " */* "
161
+ )
162
+ )
168
163
164
+ if uppercase (httpmethod) == " POST"
165
+ return Requests. post (URI (endpoint), query_str; headers = headers)
169
166
elseif uppercase (httpmethod) == " GET"
170
- return Requests. get (URI (" $(endpoint) ?$query_str " );
171
- headers = @compat (Dict {String,String} (
172
- " Content-Type" => " application/x-www-form-urlencoded" ,
173
- " Authorization" => oauth_header_val,
174
- " Accept" => " */*" )))
167
+ return Requests. get (URI (" $(endpoint) ?$query_str " ); headers = headers)
175
168
end
176
-
177
169
end
178
170
179
- end # module
171
+ end
0 commit comments