Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[feature] compiler, database, dropbox: app conf and user tokens handle

  • Loading branch information...
commit eba818a7d84eece71f701aae171016320b1606ee 1 parent ac96dfc
@cedricss cedricss authored
Showing with 71 additions and 38 deletions.
  1. +3 −18 opa/pass_DropBoxCodeGeneration.ml
  2. +68 −20 stdlib/database/dropbox/db.opa
View
21 opa/pass_DropBoxCodeGeneration.ml
@@ -73,25 +73,10 @@ module Generator = struct
let ty_database = Q.TypeVar (QmlTypeVars.TypeVar.next ())
- (* todo: dropbox backend open with appkey/appsecret/root instead *)
- let open_database gamma annotmap name host port =
+ let open_database gamma annotmap name =
let annotmap, name = C.string annotmap name in
- let annotmap, host =
- match host with
- | None -> C.none annotmap gamma
- | Some host ->
- let annotmap, host = C.string annotmap host in
- C.some annotmap gamma host
- in
- let annotmap, port =
- match port with
- | None -> C.none annotmap gamma
- | Some port ->
- let annotmap, port = C.int annotmap port in
- C.some annotmap gamma port
- in
let annotmap, open_ = OpaMapToIdent.typed_val ~label Api.Db.open_ annotmap gamma in
- let annotmap, open_ = C.apply gamma annotmap open_ [name; host; port] in
+ let annotmap, open_ = C.apply gamma annotmap open_ [name] in
(annotmap, open_)
let node_to_dbexpr _gamma annotmap node =
@@ -354,7 +339,7 @@ let init_database gamma annotmap schema =
| `dropbox ->
let ident = database.DbSchema.ident in
let name = database.DbSchema.name in
- let (annotmap, open_) = Generator.open_database gamma annotmap name None None in
+ let (annotmap, open_) = Generator.open_database gamma annotmap name in
(annotmap, (Q.NewVal (label, [ident, open_]))::newvals)
| _ -> (annotmap, newvals)
)
View
88 stdlib/database/dropbox/db.opa
@@ -27,6 +27,7 @@ import stdlib.system
import stdlib.database.common
import stdlib.apis.dropbox
import stdlib.core.rpc.core
+
/**
* {1 About this module}
*
@@ -40,11 +41,16 @@ import stdlib.core.rpc.core
* {1 Types defined in this module}
*/
+type DbDropbox.User.status =
+ {no_credentials}
+ / {request_secret : string; request_token : string}
+ / {authenticated : Dropbox.creds}
+
@opacapi @abstract type DbDropbox.t = {
root : string;
appkey : string;
appsecret : string;
- cols : list(string)
+ context : UserContext.t(DbDropbox.User.status)
}
@opacapi @abstract type DbDropbox.engine = void
@@ -96,22 +102,46 @@ DbDropbox = {{
path = DbCommon.path_to_id(path)
"/{path}.json"
- build_conf(db) = {app_key = db.appkey app_secret = db.appsecret}
- Context = {{
+ @private _User(db : DbDropbox.t) = {{
+
+ get_status() = UserContext.execute(s -> s, db.context)
+ is_authenticated() = match get_status() with { authenticated = _ } -> true | _ -> false
+ set_status(r) = UserContext.change(_ -> r, db.context)
+ set_request(token, secret) = set_status({request_secret = secret; request_token = token})
+ set_authenticated(token, secret) = set_status({authenticated = { token = token; secret = secret}})
+
+ }}
+
+ @private _Auth(db : DbDropbox.t) = {{
+
+ conf = {app_key = db.appkey app_secret = db.appsecret}
+ Auth = Dropbox(conf).Auth(_User(db))
+ get_login_url = Auth.get_login_url
+ get_access = Auth.get_access
+ are_valid_creds = Auth.are_valid_creds
- dropbox_context = UserContext.make({no_credentials})
- get() = UserContext.execute(s -> s, dropbox_context)
- set(r) = UserContext.change(_ -> r, dropbox_context)
- set_request(token, secret) = set({request_secret = secret; request_token = token})
- set_authenticated(token, secret) = set({authenticated = { token = token; secret = secret}})
+ }}
+
+ User(db : DbDropbox.t) = {{
+
+ get_status = _User(db).get_status
+ is_authenticated = _User(db).is_authenticated
+ set_status = _User(db).set_status
+ set_request = _User(db).set_request
+ set_authenticated = _User(db).set_authenticated
+ get_login_url = _Auth(db).get_login_url
+ get_access = _Auth(db).get_access
+ are_valid_creds = _Auth(db).are_valid_creds
}}
- @package gen_read(db:DbDropbox.t, path):option('data) =
- match Context.get() with
+ D(db) = Dropbox({app_key = db.appkey app_secret = db.appsecret})
+
+ @package @server gen_read(db:DbDropbox.t, path):option('data) =
+ match User(db).get_status() with
| {authenticated = creds} -> (
- match Dropbox(build_conf(db)).Files(db.root, build_path(path)).get(none, creds) with
+ match D(db).Files(db.root, build_path(path)).get(none, creds) with
| { success = file } -> (
value : DbDropbox.value = file.content
match Json.deserialize(value) with
@@ -127,10 +157,10 @@ DbDropbox = {{
| _ -> do error("Impossible to read {path}. The user is not authenticated, but in request mode.") none
@package @server gen_write(db:DbDropbox.t, path, data:string) =
- match Context.get() with
+ match User(db).get_status() with
| {authenticated = creds} -> (
json_value : DbDropbox.value = data
- match Dropbox(build_conf(db)).Files(db.root, build_path(path)).put("application/json", json_value, true, none, creds) with
+ match D(db).Files(db.root, build_path(path)).put("application/json", json_value, true, none, creds) with
| { success = _ } -> true
| { failure = failure } -> do error("Impossible to write to {path}: {failure}") false )
| {no_credentials} -> do error("Impossible to write to {path}. The user is not authenticated.") false
@@ -236,17 +266,35 @@ DbDropbox = {{
jlog(msg) = Log.notice("DbGen/Dropbox", msg)
- // todo
- open(appkey, appsecret, root) = ~{appkey appsecret root; cols=[]}
-
+ open(name) =
+ (suffix, msg) = match name with
+ | "_no_name" -> ("", "")
+ | _ -> (":{name}", "\"{name}\"")
+ args = CommandLine.filter({
+ title = "Options for Dropbox database {msg}"
+ init = none
+ anonymous = []
+ parsers = [
+ {CommandLine.default_parser with
+ names = ["--db-remote{suffix}"]
+ description = "Use a user dropbox account as a database. Specify your dropbox apps parameters."
+ param_doc = "appkey:appsecret"
+ on_param(_) = parser appkey=((![:] .)+)[:] appsecret=(.+)
+ -> {no_params = { some = {appkey=Text.to_string(appkey)
+ appsecret=Text.to_string(appsecret)
+ root="sandbox"
+ context=UserContext.make({no_credentials})
+ }}}
+ }]
+ })
+ match args with
+ | {some = conf} -> conf
+ | {none} -> error("You have to specify your dropbox app parameters: --db-remote{suffix} appkey:appsecret")
}}
open = Init.open
- @package drop(db:DbDropbox.t) =
- List.iter(
- _col -> error("Drop not yep implemented")
- , db.cols)
+ @package drop(_db:DbDropbox.t) = error("Drop not yep implemented")
}}

0 comments on commit eba818a

Please sign in to comment.
Something went wrong with that request. Please try again.