diff --git a/stdlib/apis/common/api_libs.opa b/stdlib/apis/common/api_libs.opa index a43e2c86..cb228b92 100644 --- a/stdlib/apis/common/api_libs.opa +++ b/stdlib/apis/common/api_libs.opa @@ -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 /* -------------- */ diff --git a/stdlib/apis/oauth/oauth.opa b/stdlib/apis/oauth/oauth.opa index 0e99d730..89d75a04 100644 --- a/stdlib/apis/oauth/oauth.opa +++ b/stdlib/apis/oauth/oauth.opa @@ -57,6 +57,7 @@ type OAuth.parameters = { authorize_uri : string http_method : OAuth.method inlined_auth : bool + custom_headers : option(string) } type OAuth.token = { @@ -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, _) @@ -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 = @@ -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) + }} /** @@ -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 + }} diff --git a/stdlib/apis/twitter/twitter.opa b/stdlib/apis/twitter/twitter.opa index bf24003f..63a4a6f0 100644 --- a/stdlib/apis/twitter/twitter.opa +++ b/stdlib/apis/twitter/twitter.opa @@ -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) @@ -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 diff --git a/stdlib/core/web/client/web_client.opa b/stdlib/core/web/client/web_client.opa index 91355349..a55de399 100644 --- a/stdlib/core/web/client/web_client.opa +++ b/stdlib/core/web/client/web_client.opa @@ -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) diff --git a/stdlib/core/web/core/reply.opa b/stdlib/core/web/core/reply.opa index aec90d81..46a79b11 100644 --- a/stdlib/core/web/core/reply.opa +++ b/stdlib/core/web/core/reply.opa @@ -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} @@ -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, []) diff --git a/stdlib/core/web/resource/resource.opa b/stdlib/core/web/resource/resource.opa index 25e98cbd..7d23d829 100644 --- a/stdlib/core/web/resource/resource.opa +++ b/stdlib/core/web/resource/resource.opa @@ -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 = {{ diff --git a/stdlib/core/web/resource/resource_private.opa b/stdlib/core/web/resource/resource_private.opa index c45fd823..a9c79574 100644 --- a/stdlib/core/web/resource/resource_private.opa +++ b/stdlib/core/web/resource/resource_private.opa @@ -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 = { @@ -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); } /** @@ -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 ) ) =