Skip to content

Commit

Permalink
[fix] stdlib: some quick fixes about oauth apis and http headers
Browse files Browse the repository at this point in the history
  • Loading branch information
BourgerieQuentin authored and Nicolas Glondu committed Sep 7, 2011
1 parent 65c62f3 commit 374c1c4
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 58 deletions.
4 changes: 2 additions & 2 deletions stdlib/apis/common/api_libs.opa
Expand Up @@ -33,8 +33,8 @@ API_libs_private = {{
/**
* Alias to easily manage the jlog of API modules
*/
apijlog(_text:string) =
//jlog(text)
apijlog(text:string) =
do jlog(text)
void

/* -------------- */
Expand Down
50 changes: 34 additions & 16 deletions stdlib/apis/oauth/oauth.opa
Expand Up @@ -57,6 +57,7 @@ type OAuth.parameters = {
authorize_uri : string
http_method : OAuth.method
inlined_auth : bool
custom_headers : option(string)
}

type OAuth.token = {
Expand Down Expand Up @@ -148,7 +149,7 @@ type OAuth.token_res = { success : OAuth.token } / { error : string }
[("oauth_consumer_key", p.consumer_key), ("oauth_timestamp", "{timestamp}"),
("oauth_nonce", "{nonce}"), ("oauth_version", "1.0")]

get_res(more_auth, secret, uri, params, parse_fun) =
get_res_2(more_auth, secret, uri, params) =
auth_params = build_basic_params()
|> List.append(_, more_auth)
|> sign_request(secret, uri, params, _)
Expand All @@ -174,32 +175,31 @@ type OAuth.token_res = { success : OAuth.token } / { error : string }
res = match p.http_method with
| {GET} ->
uri = if params_text == "" then uri else "{uri}?{params_text}"
options = {WebClient.Get.default_options with auth=auth}
options = {WebClient.Get.default_options with auth=auth custom_headers=p.custom_headers}
do API_libs_private.apijlog("URI: \n{uri}")
match Uri.of_string(uri) with
| {none} -> ""
| {some=u} ->
match WebClient.Get.try_get_with_options(u, options) with
| {failure=_} -> ""
| {success=s} -> s.content
end
| {none} -> error("_______")
| {some=u} -> WebClient.Get.try_get_with_options(u, options)
end
| {POST} ->
do API_libs_private.apijlog("URI: \n{uri}")
options = {WebClient.Post.default_options with
custom_headers=p.custom_headers
auth=auth
content=(if params_text == "" then {none}
else {some=params_text})}
match Uri.of_string(uri) with
| {none} -> ""
| {some=u} ->
match WebClient.Post.try_post_with_options(u, options) with
| {failure=_} -> ""
| {success=s} -> s.content
end
| {none} -> error("_______")
| {some=u} -> WebClient.Post.try_post_with_options(u, options)
end
do API_libs_private.apijlog("Result: '''{res}'''")
parse_fun(res)
res

get_res(more_auth, secret, uri, params, parse_fun) =
match get_res_2(more_auth, secret, uri, params) with
| {failure=_} -> parse_fun("")
| {success=s} -> parse_fun(s.content)
end

get_request_token(callback) =
more_auth =
Expand All @@ -217,9 +217,17 @@ type OAuth.token_res = { success : OAuth.token } / { error : string }
get_res(more_auth, request_secret, p.access_token_uri, [], build_result)

get_protected_resource(uri, params, access_key, access_secret) =
more_auth = [("oauth_token", access_key)]
more_auth = match access_key
| "" -> []
| _ -> [("oauth_token", access_key)]
get_res(more_auth, access_secret, uri, params, identity)

get_protected_resource2(uri, params, access_key, access_secret) =
more_auth = match access_key
| "" -> []
| _ -> [("oauth_token", access_key)]
get_res_2(more_auth, access_secret, uri, params)

}}

/**
Expand Down Expand Up @@ -307,4 +315,14 @@ OAuth(parameters:OAuth.parameters) = {{
uri, params, access_token, access_secret
)

get_protected_resource_2(
uri : string,
params : list((string, string)),
access_token : string,
access_secret : string
) =
OAuth_private(p).get_protected_resource2(
uri, params, access_token, access_secret
) : outcome

}}
2 changes: 2 additions & 0 deletions stdlib/apis/twitter/twitter.opa
Expand Up @@ -475,6 +475,7 @@ type Twitter.rate_limit = {
access_token_uri = "https://api.twitter.com/oauth/access_token"
http_method = http_method
inlined_auth = false
custom_headers = none
} : OAuth.parameters)
Expand Down Expand Up @@ -614,6 +615,7 @@ Twitter(conf:Twitter.configuration) = {{
access_token_uri = "https://api.twitter.com/oauth/access_token"
http_method = {POST}
inlined_auth = false
custom_headers = none
} : OAuth.parameters
Expand Down
1 change: 1 addition & 0 deletions stdlib/core/web/client/web_client.opa
Expand Up @@ -113,6 +113,7 @@ type WebClient.Head.options =
{
auth: option(string)
custom_headers: option(string)

custom_agent: option(string)
follow_redirects: int /*The maximal number of redirects to follow. By default, 0. Usually a bad idea to set it higher than 5.*/
timeout_sec: option(float)
Expand Down
42 changes: 34 additions & 8 deletions stdlib/core/web/core/reply.opa
Expand Up @@ -118,6 +118,32 @@ type web_cache_control = {volatile} /** The resource changes at each request
type web_server_status = external
type WebInfo.private.native_http_header = external

type Resource.cookie_attributes =
{ comment:string }
/ { domain:string }
/ { max_age:int }
/ { path:string }
/ { secure:void }
/ { version:int }

type Resource.cookie_def = {
name : string ;
value : string ;
attributes : list(Resource.cookie_attributes) ;
}

type Resource.http_response_header =
{ set_cookie:Resource.cookie_def }
/ { age:int }
/ { location:string }
/ { retry_after: { date:string } / { delay:int } }
/ { server: list(string) }
/ { content_disposition : { attachment : string } }

type Resource.http_general_header =
{ lastm : web_cache_control }

type Resource.http_header = Resource.http_general_header / Resource.http_response_header

/**
* {1 Interface}
Expand Down Expand Up @@ -165,17 +191,17 @@ WebCoreExport =
| _ -> lst

@private cookie_def_to_string(cd) =
cd.name ^ "=" ^ cd.value
^ String.implode(cookie_attribute_to_string, ";", cd.attributes)
g = List.fold(x, acc -> Text.insert_right(acc, "; ") |> Text.insert_right(_, cookie_attribute_to_string(x)), cd.attributes, Text.cons(""))
"{cd.name}={cd.value}{g}"

@private cookie_attribute_to_string(ca) =
match ca with
| ~{comment} -> "Comment=" ^ comment
| ~{domain} -> "Domain=" ^ domain
| ~{max_age} -> "Max-Age={max_age}"
| ~{path} -> "Path=" ^ path
| {secure} -> "Secure"
| ~{version} -> "Version={version}"
| ~{comment} -> "comment=" ^ comment
| ~{domain} -> "domain=" ^ domain
| ~{max_age} -> "max-age={max_age}"
| ~{path} -> "path=" ^ path
| {secure} -> "secure"
| ~{version} -> "version={version}"

@private to_ll_headers(headers : list(Resource.http_header)) : list(WebInfo.private.native_http_header) =
List.foldl(add_ll_header, headers, [])
Expand Down
30 changes: 1 addition & 29 deletions stdlib/core/web/resource/resource.opa
Expand Up @@ -128,35 +128,7 @@ type platform_customization =
type resource_content = external
*/

/**
* {1 Interface}
*/
type Resource.cookie_attributes =
{ comment:string }
/ { domain:string }
/ { max_age:int }
/ { path:string }
/ { secure:void }
/ { version:int }

type Resource.cookie_def = {
name : string ;
value : string ;
attributes : list(Resource.cookie_attributes) ;
}

type Resource.http_response_header =
{ set_cookie:Resource.cookie_def }
/ { age:int }
/ { location:string }
/ { retry_after: { date:string } / { delay:int } }
/ { server: list(string) }
/ { content_disposition : { attachment : string } }

type Resource.http_general_header =
{ lastm : web_cache_control }

type Resource.http_header = Resource.http_general_header / Resource.http_response_header



Resource = {{
Expand Down
6 changes: 3 additions & 3 deletions stdlib/core/web/resource/resource_private.opa
Expand Up @@ -75,7 +75,7 @@ type resource_private_content =
*/
type resource_private = { rc_content : resource_private_content;
rc_status : web_response;
rc_headers : list(WebCoreExport.http_header);
rc_headers : list(Resource.http_header);
}

type resource_cache_customizers = {
Expand Down Expand Up @@ -113,7 +113,7 @@ type dynamic_private_content =
type dynamic_resource_private = { rc_name : string;
rc_content : dynamic_private_content;
rc_status : web_response;
rc_headers : list(WebCoreExport.http_header);
rc_headers : list(Resource.http_header);
}

/**
Expand Down Expand Up @@ -818,7 +818,7 @@ export_resource(external_css_files: list(string),
),
make_response_with_headers: (
WebInfo.private.native_request, web_response,
list(WebCoreExport.http_header), string, string
list(Resource.http_header), string, string
-> WebInfo.private.native_response
)
) =
Expand Down

0 comments on commit 374c1c4

Please sign in to comment.