Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
15d6d3fb
Commit
15d6d3fb
authored
Apr 18, 2016
by
Sean Hammond
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3211 from hypothesis/retry-api-requests
Retry requests to /api endpoint
parents
0657dd53
680d9b69
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
30 deletions
+44
-30
store.js
h/static/scripts/store.js
+27
-22
store-test.js
h/static/scripts/test/store-test.js
+17
-8
No files found.
h/static/scripts/store.js
View file @
15d6d3fb
...
...
@@ -2,6 +2,8 @@
var
angular
=
require
(
'angular'
);
var
retryUtil
=
require
(
'./retry-util'
);
function
prependTransform
(
defaults
,
transform
)
{
// We can't guarantee that the default transformation is an array
var
result
=
angular
.
isArray
(
defaults
)
?
defaults
.
slice
(
0
)
:
[
defaults
];
...
...
@@ -57,10 +59,14 @@ function encodeUriQuery(val) {
// then proceed to parse as a delimiter in the query string. To avoid this
// problem we use a very conservative encoder, found above.
function
serializeParams
(
params
)
{
if
(
!
params
)
return
''
;
if
(
!
params
)
{
return
''
;
}
var
parts
=
[];
forEachSorted
(
params
,
function
(
value
,
key
)
{
if
(
value
===
null
||
typeof
value
===
'undefined'
)
return
;
if
(
value
===
null
||
typeof
value
===
'undefined'
)
{
return
;
}
if
(
angular
.
isArray
(
value
))
{
angular
.
forEach
(
value
,
function
(
v
,
k
)
{
parts
.
push
(
encodeUriQuery
(
key
)
+
'='
+
encodeUriQuery
(
serializeValue
(
v
)));
...
...
@@ -99,29 +105,28 @@ function store($http, $resource, settings) {
};
// We call the API root and it gives back the actions it provides.
instance
.
$resolved
=
false
;
instance
.
$promise
=
$http
.
get
(
settings
.
apiUrl
)
.
finally
(
function
()
{
instance
.
$resolved
=
true
;
})
.
then
(
function
(
response
)
{
var
links
=
response
.
data
.
links
;
// N.B. in both cases below we explicitly override the default `get`
// action because there is no way to provide defaultOptions to the default
// action.
instance
.
SearchResource
=
$resource
(
links
.
search
.
url
,
{},
{
get
:
angular
.
extend
({
url
:
links
.
search
.
url
},
defaultOptions
),
});
instance
.
AnnotationResource
=
$resource
(
links
.
annotation
.
read
.
url
,
{},
{
get
:
angular
.
extend
(
links
.
annotation
.
read
,
defaultOptions
),
create
:
angular
.
extend
(
links
.
annotation
.
create
,
defaultOptions
),
update
:
angular
.
extend
(
links
.
annotation
.
update
,
defaultOptions
),
delete
:
angular
.
extend
(
links
.
annotation
.
delete
,
defaultOptions
),
});
instance
.
$promise
=
retryUtil
.
retryPromiseOperation
(
function
()
{
return
$http
.
get
(
settings
.
apiUrl
);
}).
then
(
function
(
response
)
{
var
links
=
response
.
data
.
links
;
// N.B. in both cases below we explicitly override the default `get`
// action because there is no way to provide defaultOptions to the default
// action.
instance
.
SearchResource
=
$resource
(
links
.
search
.
url
,
{},
{
get
:
angular
.
extend
({
url
:
links
.
search
.
url
},
defaultOptions
),
});
return
instance
;
instance
.
AnnotationResource
=
$resource
(
links
.
annotation
.
read
.
url
,
{},
{
get
:
angular
.
extend
(
links
.
annotation
.
read
,
defaultOptions
),
create
:
angular
.
extend
(
links
.
annotation
.
create
,
defaultOptions
),
update
:
angular
.
extend
(
links
.
annotation
.
update
,
defaultOptions
),
delete
:
angular
.
extend
(
links
.
annotation
.
delete
,
defaultOptions
),
});
return
instance
;
});
return
instance
;
}
...
...
h/static/scripts/test/store-test.js
View file @
15d6d3fb
'use strict'
;
var
inject
=
angular
.
mock
.
inject
;
var
module
=
angular
.
mock
.
module
;
var
angular
=
require
(
'angular'
);
var
proxyquire
=
require
(
'proxyquire'
);
var
util
=
require
(
'./util'
);
describe
(
'store'
,
function
()
{
var
$httpBackend
=
null
;
...
...
@@ -10,12 +12,19 @@ describe('store', function () {
before
(
function
()
{
angular
.
module
(
'h'
,
[
'ngResource'
])
.
service
(
'store'
,
require
(
'../store'
));
.
service
(
'store'
,
proxyquire
(
'../store'
,
util
.
noCallThru
({
angular
:
angular
,
'./retry-util'
:
{
retryPromiseOperation
:
function
(
fn
)
{
return
fn
();
},
},
})));
});
beforeEach
(
module
(
'h'
));
beforeEach
(
angular
.
mock
.
module
(
'h'
));
beforeEach
(
module
(
function
(
$provide
)
{
beforeEach
(
angular
.
mock
.
module
(
function
(
$provide
)
{
sandbox
=
sinon
.
sandbox
.
create
();
$provide
.
value
(
'settings'
,
{
apiUrl
:
'http://example.com/api'
});
}));
...
...
@@ -26,7 +35,7 @@ describe('store', function () {
sandbox
.
restore
();
});
beforeEach
(
inject
(
function
(
$q
,
_$httpBackend_
,
_store_
)
{
beforeEach
(
angular
.
mock
.
inject
(
function
(
$q
,
_$httpBackend_
,
_store_
)
{
$httpBackend
=
_$httpBackend_
;
store
=
_store_
;
...
...
@@ -50,8 +59,8 @@ describe('store', function () {
}));
it
(
'reads the operations from the backend'
,
function
()
{
assert
.
isFunction
(
store
.
AnnotationResource
,
'expected store.AnnotationResource to be a function'
)
assert
.
isFunction
(
store
.
SearchResource
,
'expected store.SearchResource to be a function'
)
assert
.
isFunction
(
store
.
AnnotationResource
);
assert
.
isFunction
(
store
.
SearchResource
);
});
it
(
'saves a new annotation'
,
function
()
{
...
...
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