Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Aurélien Vermylen
jio
Commits
aefd3402
Commit
aefd3402
authored
Sep 25, 2017
by
Aurélien Vermylen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Stash to fix mappingstorage tests.
parent
6caa5ab4
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
167 additions
and
488 deletions
+167
-488
src/jio.storage/automaticapistorage.js
src/jio.storage/automaticapistorage.js
+92
-51
test/jio.storage/automaticapistorage.tests.js
test/jio.storage/automaticapistorage.tests.js
+75
-437
No files found.
src/jio.storage/automaticapistorage.js
View file @
aefd3402
...
...
@@ -21,20 +21,14 @@
'/user/{id}/', '/user/{user_id}/device/',
'/user/{user_id}/device/{device_id}/', '/vehicle/{id}/', '/vehicle/',
'/vehicle/{vehicle_id}/mil/'],*/
AUTOMATIC_VALID_ENDPOINT_REGEXES
=
[
/^
\/
trip
(\/
T_
\w
*|
)\/
$/
,
/^
\/
user
\/
me
(\/
device
(\/\w
*|
)
|
)\/
$/
,
/^
\/
vehicle
(\/
C_
\w
*
(\/
mil|
)
|
)\/
$/
],
AUTOMATIC_VALID_ENDPOINTID_REGEXES
=
[
/^
\/
trip
\/
T_
\w
*
\/
$/
,
/^
\/
user
\/
me
\/
device
\/\w
*
\/
$/
,
/^
\/
vehicle
\/
C_
\w
*
\/
$/
],
AUTOMATIC_VALID_ENDPOINT_REGEXES
=
[
/^
\/
(
U_
\w
*|all
)\/
trip
(\/
T_
\w
*|
)\/
$/
,
/^
\/
(
U_
\w
*|all
)\/
user
\/
me
(\/
device
(\/\w
*|
)
|
)\/
$/
,
/^
\/
(
U_
\w
*|all
)\/
vehicle
(\/
C_
\w
*
(\/
mil|
)
|
)\/
$/
],
AUTOMATIC_VALID_ENDPOINTID_REGEXES
=
[
/^
\/
(
U_
\w
*|all
)\/
trip
\/
T_
\w
*
\/
$/
,
/^
\/
(
U_
\w
*|all
)\/
user
\/
me
\/
device
\/\w
*
\/
$/
,
/^
\/
(
U_
\w
*|all
)\/
vehicle
\/
C_
\w
*
\/
$/
],
automatic_template
=
UriTemplate
.
parse
(
AUTOMATIC_BASE_URI
+
'
{endpoint}
'
);
function
_queryAutomaticAPI
(
endpoint
,
filters
,
jio
)
{
var
result
=
[],
type
,
promises
,
user_dict
=
{},
temp
;
// Check validity of endpoint.
function
checkEndpoint
(
endpoint
)
{
var
regex
;
...
...
@@ -46,32 +40,68 @@
}
return
false
;
}
// Check that ids match a valid Automatic API id
function
checkEndpointAsId
(
endpoint
)
{
var
regex
;
for
(
regex
in
AUTOMATIC_VALID_ENDPOINTID_REGEXES
)
{
if
(
AUTOMATIC_VALID_ENDPOINTID_REGEXES
.
hasOwnProperty
(
regex
)
&&
regex
.
test
(
endpoint
))
{
return
true
;
}
}
return
false
;
}
function
_queryAutomaticAPI
(
endpoint
,
filters
,
jio
)
{
var
result
=
[],
type
,
promises
,
user_dict
=
{},
temp
,
i
;
if
(
!
checkEndpoint
(
endpoint
))
{
throw
new
jIO
.
util
.
jIOError
(
'
Wrong Automatic API query. (usually
'
+
'
caused by wrong "id" or "type")
'
,
400
);
}
type
=
endpoint
.
split
(
'
/
'
)[
1
];
type
=
endpoint
.
split
(
'
/
'
)[
2
];
// Promise chain to handle multi-part response ("_metadata"->"next" parts).
function
treatNext
(
returned
)
{
var
path
,
data
,
user_id
;
user_id
,
next
;
data
=
returned
[
0
];
user_id
=
user_dict
[
returned
[
1
]];
if
(
data
.
error
===
'
err_unauthorized
'
)
{
throw
new
jIO
.
util
.
jIOError
(
'
Invalid token provided, Unauthorized.
'
,
401
);
}
if
(
!
checkEndpointAsId
(
endpoint
))
{
if
(
data
.
_metadata
===
undefined
)
{
throw
new
jIO
.
util
.
jIOError
(
'
Malformed Automatic API result.
'
,
500
);
}
path
=
URI
(
data
.
results
.
url
).
path
();
temp
=
{
'
path
'
:
path
,
next
=
data
.
_metadata
.
next
;
data
=
data
.
results
;
}
for
(
i
=
0
;
i
<
data
.
length
;
i
+=
1
)
{
path
=
URI
(
data
[
i
].
url
).
path
();
temp
=
{
'
path
'
:
path
,
'
reference
'
:
user_id
+
path
,
'
type
'
:
type
,
'
started_at
'
:
data
.
results
.
started_at
||
null
,
'
ended_at
'
:
data
.
results
.
ended_at
||
null
,
'
user
'
:
user_id
};
'
started_at
'
:
data
[
i
].
started_at
||
null
,
'
ended_at
'
:
data
[
i
].
ended_at
||
null
,
'
user
'
:
user_id
};
result
.
push
(
temp
);
jio
.
_cache
.
put
(
'
/
'
+
user_id
+
path
,
temp
);
jio
.
_cache
.
putAttachment
(
'
/
'
,
''
,
JSON
.
stringify
(
data
.
results
));
if
(
data
.
_metadata
.
next
===
undefined
)
{
return
result
;
jio
.
_cache
.
putAttachment
(
'
/
'
+
user_id
+
path
,
'
data
'
,
new
Blob
([
JSON
.
stringify
(
data
[
i
].
results
)],
{
type
:
'
application/text
'
}));
}
if
(
next
===
undefined
)
{
return
true
;
}
return
jIO
.
util
.
ajax
({
'
type
'
:
'
GET
'
,
...
...
@@ -99,18 +129,24 @@
'
xhrFields
'
:
{
withCredentials
:
true
}
}).
push
(
function
(
usr
)
{
user_dict
[
token
]
=
JSON
.
parse
(
usr
).
id
;
temp
=
endpoint
.
split
(
'
/
'
)[
1
];
if
(
temp
===
'
all
'
||
temp
===
user_dict
[
token
])
{
return
jIO
.
util
.
ajax
({
'
type
'
:
'
GET
'
,
'
url
'
:
automatic_template
.
search
(
filters
).
expand
(
endpoint
),
'
headers
'
:
{
'
Authorization
'
:
'
Bearer
'
+
token
},
'
xhrFields
'
:
{
withCredentials
:
true
}
});
}
throw
new
jIO
.
util
.
jIOError
(
'
Ignored.
'
,
200
);
}).
push
(
function
(
response
)
{
return
[
JSON
.
parse
(
response
.
target
.
responseText
),
token
];
}).
push
(
treatNext
);
});
});
return
RSVP
.
all
(
promises
);
return
RSVP
.
all
(
promises
).
push
(
function
()
{
return
result
;
});
}
/**
* The Automatic API Storage extension
...
...
@@ -126,7 +162,7 @@
for
(
i
=
0
;
i
<
spec
.
access_tokens
.
length
;
i
+=
1
)
{
if
(
typeof
spec
.
access_tokens
[
i
]
!==
'
string
'
||
!
spec
.
access_tokens
[
i
])
{
throw
new
jIO
.
util
.
jIOError
(
'
Access Tokens must be
'
+
'
an array of non-empty strings.
'
,
400
);
'
an array of non-empty strings.
'
,
400
);
}
}
...
...
@@ -135,15 +171,13 @@
}
AutomaticAPIStorage
.
prototype
.
get
=
function
(
id
)
{
function
checkEndpointAsId
(
endpoint
)
{
var
regex
;
for
(
regex
in
AUTOMATIC_VALID_ENDPOINTID_REGEXES
)
{
if
(
AUTOMATIC_VALID_ENDPOINTID_REGEXES
.
hasOwnProperty
(
regex
)
&&
regex
.
test
(
endpoint
))
{
return
true
;
if
(
id
.
indexOf
(
'
/
'
)
!==
0
)
{
throw
new
jIO
.
util
.
jIOError
(
'
id
'
+
id
+
'
is forbidden (not starting with /)
'
,
400
);
}
}
return
false
;
if
(
id
.
lastIndexOf
(
"
/
"
)
!==
(
id
.
length
-
1
))
{
throw
new
jIO
.
util
.
jIOError
(
'
id
'
+
id
+
'
is forbidden (not ending with /)
'
,
400
);
}
if
(
!
checkEndpointAsId
(
id
))
{
throw
new
jIO
.
util
.
jIOError
(
'
Invalid id.
'
,
400
);
...
...
@@ -151,7 +185,14 @@
return
this
.
_cache
.
get
(
id
).
push
(
function
(
res
)
{
return
res
;
},
function
()
{
_queryAutomaticAPI
(
id
,
{},
this
);
return
_queryAutomaticAPI
(
id
,
{},
this
).
push
(
function
(
res
)
{
if
(
typeof
res
===
'
object
'
&&
res
.
length
>
0
)
{
return
res
[
0
];
}
throw
new
jIO
.
util
.
jIOError
(
'
Cannot find document:
'
+
id
,
404
);
},
function
()
{
throw
new
jIO
.
util
.
jIOError
(
'
Cannot find document:
'
+
id
,
404
);
});
});
};
...
...
test/jio.storage/automaticapistorage.tests.js
View file @
aefd3402
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment