Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .release-notes/pagination-converter-credentials.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Take credentials only from apply in the pagination converters

`PaginatedListJSONConverter` and `PaginatedSearchJSONConverter` took credentials twice: once when you created the converter and once when you called `apply`. `apply` converted the items with its own credentials, but the page's `prev_page()` and `next_page()` fetched with the constructor's credentials. Passing different credentials in the two places gave a page whose items made their own requests with one account while `prev_page()` and `next_page()` used another.

The constructors no longer take credentials. `prev_page()` and `next_page()` now use the credentials passed to `apply`, as the item conversion already did. Code that creates either converter must drop the credentials argument. Code that only calls the library's operations, such as `GetRepositoryIssues` or `SearchIssues`, needs no change.

Before:

```pony
let converter = PaginatedListJSONConverter[Issue](creds, IssueJSONConverter)
```

After:

```pony
let converter = PaginatedListJSONConverter[Issue](IssueJSONConverter)
```

`PaginatedSearchJSONConverter` changes the same way.
2 changes: 2 additions & 0 deletions github_rest_api/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ actor \nodoc\ Main is TestList
test(_TestSearchConverterExtractsLinks)
test(_TestSearchConverterNoLinks)
test(_TestListConverterExtractsLinks)
test(_TestListConverterPageUsesApplyCredentials)
test(_TestSearchConverterPageUsesApplyCredentials)
test(_TestListConverterNoLinks)
test(_TestSearchNextPageFollowsLink)
test(_TestSearchPrevPageFollowsLink)
Expand Down
7 changes: 3 additions & 4 deletions github_rest_api/_test_mock_http_server.pony
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ primitive \nodoc\ _TestRequestLineResponder
line
} val

primitive \nodoc\ _TestCheckRequestedLine[A: Any val]
primitive \nodoc\ _TestCheck418Body[A: Any val]
"""
Returns true when `result` is the error that `_TestRequestLineResponder`
sends for a request whose line is `expected`. Otherwise fails and completes
the test, and returns false.
Returns true when `result` is a `RequestError` with status 418 and body
`expected`. Otherwise fails and completes the test, and returns false.
"""
fun apply(
h: TestHelper,
Expand Down
2 changes: 1 addition & 1 deletion github_rest_api/_test_operation_request_lines.pony
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class \nodoc\ iso _TestOperationRequestLine[A: Any val] is UnitTest
{()(h, creds, base_url, expected_line, call) =>
call(base_url, creds).next[None](
{(result: (A | req.RequestError)) =>
if _TestCheckRequestedLine[A](h, result, expected_line) then
if _TestCheck418Body[A](h, result, expected_line) then
h.complete(true)
end
})
Expand Down
21 changes: 7 additions & 14 deletions github_rest_api/_test_result_receivers.pony
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ class \nodoc\ _TestPaginatedResultReceiverSuccess is UnitTest
req.Credentials(
TCPConnectAuth(h.env.root))
let converter =
PaginatedListJSONConverter[String](
creds, _TestStringConverter)
PaginatedListJSONConverter[String](_TestStringConverter)
let receiver =
PaginatedResultReceiver[String](
creds, p, converter)
Expand Down Expand Up @@ -301,8 +300,7 @@ class \nodoc\ _TestPaginatedResultReceiverConverterError is UnitTest
req.Credentials(
TCPConnectAuth(h.env.root))
let converter =
PaginatedListJSONConverter[String](
creds, _TestStringConverter)
PaginatedListJSONConverter[String](_TestStringConverter)
let receiver =
PaginatedResultReceiver[String](
creds, p, converter)
Expand Down Expand Up @@ -346,8 +344,7 @@ class \nodoc\ _TestPaginatedResultReceiverNotAnArray is UnitTest
req.Credentials(
TCPConnectAuth(h.env.root))
let converter =
PaginatedListJSONConverter[String](
creds, _TestStringConverter)
PaginatedListJSONConverter[String](_TestStringConverter)
let receiver =
PaginatedResultReceiver[String](
creds, p, converter)
Expand Down Expand Up @@ -387,8 +384,7 @@ class \nodoc\ _TestPaginatedResultReceiverFailure is UnitTest
req.Credentials(
TCPConnectAuth(h.env.root))
let converter =
PaginatedListJSONConverter[String](
creds, _TestStringConverter)
PaginatedListJSONConverter[String](_TestStringConverter)
let receiver =
PaginatedResultReceiver[String](
creds, p, converter)
Expand Down Expand Up @@ -432,8 +428,7 @@ class \nodoc\ _TestSearchResultReceiverSuccess is UnitTest
req.Credentials(
TCPConnectAuth(h.env.root))
let converter =
PaginatedSearchJSONConverter[String](
creds, _TestStringConverter)
PaginatedSearchJSONConverter[String](_TestStringConverter)
let receiver =
SearchResultReceiver[String](
creds, p, converter)
Expand Down Expand Up @@ -480,8 +475,7 @@ class \nodoc\ _TestSearchResultReceiverConverterError is UnitTest
req.Credentials(
TCPConnectAuth(h.env.root))
let converter =
PaginatedSearchJSONConverter[String](
creds, _TestStringConverter)
PaginatedSearchJSONConverter[String](_TestStringConverter)
let receiver =
SearchResultReceiver[String](
creds, p, converter)
Expand Down Expand Up @@ -521,8 +515,7 @@ class \nodoc\ _TestSearchResultReceiverFailure is UnitTest
req.Credentials(
TCPConnectAuth(h.env.root))
let converter =
PaginatedSearchJSONConverter[String](
creds, _TestStringConverter)
PaginatedSearchJSONConverter[String](_TestStringConverter)
let receiver =
SearchResultReceiver[String](
creds, p, converter)
Expand Down
165 changes: 147 additions & 18 deletions github_rest_api/_test_search_and_pagination.pony
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class \nodoc\ _TestSearchConverterExtractsLinks is UnitTest
let creds =
req.Credentials(TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx)
let converter =
PaginatedSearchJSONConverter[String](creds, _TestStringConverter)
PaginatedSearchJSONConverter[String](_TestStringConverter)
let items_arr = JSONArray
.push(JSONObject.update("value", "a"))
.push(JSONObject.update("value", "b"))
Expand Down Expand Up @@ -55,7 +55,7 @@ class \nodoc\ _TestSearchConverterExtractsLinks is UnitTest
| let next: Promise[(SearchResults[String] | req.RequestError)] =>
next.next[None](
{(result: (SearchResults[String] | req.RequestError))(h, sr) =>
if _TestCheckRequestedLine[SearchResults[String]](
if _TestCheck418Body[SearchResults[String]](
h, result, "GET /next?page=3 HTTP/1.1")
then
match \exhaustive\ sr.prev_page()
Expand All @@ -66,7 +66,7 @@ class \nodoc\ _TestSearchConverterExtractsLinks is UnitTest
{(prev_result:
(SearchResults[String] | req.RequestError))
=>
if _TestCheckRequestedLine[SearchResults[String]](
if _TestCheck418Body[SearchResults[String]](
h, prev_result, "GET /prev?page=1 HTTP/1.1")
then
h.complete(true)
Expand All @@ -92,8 +92,7 @@ class \nodoc\ _TestSearchConverterNoLinks is UnitTest
h.long_test(2_000_000_000)
let creds = req.Credentials(TCPConnectAuth(h.env.root))
let converter =
PaginatedSearchJSONConverter[String](
creds, _TestStringConverter)
PaginatedSearchJSONConverter[String](_TestStringConverter)
let items_arr = JSONArray
.push(JSONObject.update("value", "x"))
let envelope = JSONObject
Expand Down Expand Up @@ -126,7 +125,7 @@ class \nodoc\ _TestListConverterExtractsLinks is UnitTest
let creds =
req.Credentials(TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx)
let converter =
PaginatedListJSONConverter[String](creds, _TestStringConverter)
PaginatedListJSONConverter[String](_TestStringConverter)
let arr = JSONArray
.push(JSONObject.update("value", "p"))
.push(JSONObject.update("value", "q"))
Expand Down Expand Up @@ -161,7 +160,7 @@ class \nodoc\ _TestListConverterExtractsLinks is UnitTest
| let next: Promise[(PaginatedList[String] | req.RequestError)] =>
next.next[None](
{(result: (PaginatedList[String] | req.RequestError))(h, pl) =>
if _TestCheckRequestedLine[PaginatedList[String]](
if _TestCheck418Body[PaginatedList[String]](
h, result, "GET /next?page=3 HTTP/1.1")
then
match \exhaustive\ pl.prev_page()
Expand All @@ -172,7 +171,7 @@ class \nodoc\ _TestListConverterExtractsLinks is UnitTest
{(prev_result:
(PaginatedList[String] | req.RequestError))
=>
if _TestCheckRequestedLine[PaginatedList[String]](
if _TestCheck418Body[PaginatedList[String]](
h, prev_result, "GET /prev?page=1 HTTP/1.1")
then
h.complete(true)
Expand All @@ -198,8 +197,7 @@ class \nodoc\ _TestListConverterNoLinks is UnitTest
h.long_test(2_000_000_000)
let creds = req.Credentials(TCPConnectAuth(h.env.root))
let converter =
PaginatedListJSONConverter[String](
creds, _TestStringConverter)
PaginatedListJSONConverter[String](_TestStringConverter)
let arr = JSONArray
.push(JSONObject.update("value", "z"))
try
Expand Down Expand Up @@ -232,8 +230,7 @@ class \nodoc\ _TestSearchNextPageFollowsLink is UnitTest
TCPConnectAuth(h.env.root)
where ssl_ctx' = sslctx)
let converter =
PaginatedSearchJSONConverter[String](
creds, _TestStringConverter)
PaginatedSearchJSONConverter[String](_TestStringConverter)

let p =
Promise[(SearchResults[String] | req.RequestError)]
Expand Down Expand Up @@ -354,8 +351,7 @@ class \nodoc\ _TestSearchPrevPageFollowsLink is UnitTest
TCPConnectAuth(h.env.root)
where ssl_ctx' = sslctx)
let converter =
PaginatedSearchJSONConverter[String](
creds, _TestStringConverter)
PaginatedSearchJSONConverter[String](_TestStringConverter)

let p =
Promise[(SearchResults[String] | req.RequestError)]
Expand Down Expand Up @@ -475,8 +471,7 @@ class \nodoc\ _TestListNextPageFollowsLink is UnitTest
TCPConnectAuth(h.env.root)
where ssl_ctx' = sslctx)
let converter =
PaginatedListJSONConverter[String](
creds, _TestStringConverter)
PaginatedListJSONConverter[String](_TestStringConverter)

let p =
Promise[(PaginatedList[String] | req.RequestError)]
Expand Down Expand Up @@ -594,8 +589,7 @@ class \nodoc\ _TestListPrevPageFollowsLink is UnitTest
TCPConnectAuth(h.env.root)
where ssl_ctx' = sslctx)
let converter =
PaginatedListJSONConverter[String](
creds, _TestStringConverter)
PaginatedListJSONConverter[String](_TestStringConverter)

let p =
Promise[(PaginatedList[String] | req.RequestError)]
Expand Down Expand Up @@ -698,3 +692,138 @@ class \nodoc\ _TestListPrevPageFollowsLink is UnitTest
creds, page2_url, receiver)
} val)
h.dispose_when_done(listener)

primitive \nodoc\ _TestBearerResponder
"""
Answers every request with status 418. The body is `bearer` when the
request carries `authorization: Bearer <token>`, and `none` otherwise.
"""
fun apply(token: String): _Responder =>
let header: String val = "\r\nauthorization: Bearer " + token + "\r\n"
{(request: String)(header): String =>
let body =
if request.contains(header) then
"bearer"
else
"none"
end
"HTTP/1.1 418 I'm a teapot\r\n" +
"Content-Length: " + body.size().string() + "\r\n" +
"\r\n" +
body
} val

class \nodoc\ _TestListConverterPageUsesApplyCredentials is UnitTest
"""
The page that `apply` builds fetches its next page with the credentials
passed to `apply`.
"""
fun name(): String =>
"search-pagination/list-converter/page-uses-apply-credentials"

fun ref apply(h: TestHelper) ? =>
let sslctx = _TestSSLContext(h)?
let port: String = "48237"
let token = "ghp_page_token"
let creds =
req.Credentials(
TCPConnectAuth(h.env.root),
_TestToken(token)?
where ssl_ctx' = sslctx)
let converter =
PaginatedListJSONConverter[String](_TestStringConverter)
let arr = JSONArray.push(JSONObject.update("value", "p"))
let link: String val =
"<" + _TestURL(_TestHost(), port, "/next") + ">; rel=\"next\""

h.long_test(10_000_000_000)
let listener =
_MockHTTPListener(
h,
port,
sslctx,
_TestBearerResponder(token),
{()(h, creds, converter, arr, link) =>
let pl =
try
converter(JSONNav(arr), link, creds)?
else
h.fail("Converter raised an error")
h.complete(false)
return
end
match \exhaustive\ pl.next_page()
| let next: Promise[(PaginatedList[String] | req.RequestError)] =>
next.next[None](
{(result: (PaginatedList[String] | req.RequestError)) =>
if _TestCheck418Body[PaginatedList[String]](
h, result, "bearer")
then
h.complete(true)
end
})
| None =>
h.fail("next_page should not be None")
h.complete(false)
end
} val)
h.dispose_when_done(listener)

class \nodoc\ _TestSearchConverterPageUsesApplyCredentials is UnitTest
"""
The page that `apply` builds fetches its next page with the credentials
passed to `apply`.
"""
fun name(): String =>
"search-pagination/search-converter/page-uses-apply-credentials"

fun ref apply(h: TestHelper) ? =>
let sslctx = _TestSSLContext(h)?
let port: String = "48238"
let token = "ghp_page_token"
let creds =
req.Credentials(
TCPConnectAuth(h.env.root),
_TestToken(token)?
where ssl_ctx' = sslctx)
let converter =
PaginatedSearchJSONConverter[String](_TestStringConverter)
let envelope = JSONObject
.update("total_count", I64(1))
.update("incomplete_results", false)
.update("items", JSONArray.push(JSONObject.update("value", "a")))
let link: String val =
"<" + _TestURL(_TestHost(), port, "/next") + ">; rel=\"next\""

h.long_test(10_000_000_000)
let listener =
_MockHTTPListener(
h,
port,
sslctx,
_TestBearerResponder(token),
{()(h, creds, converter, envelope, link) =>
let sr =
try
converter(JSONNav(envelope), link, creds)?
else
h.fail("Converter raised an error")
h.complete(false)
return
end
match \exhaustive\ sr.next_page()
| let next: Promise[(SearchResults[String] | req.RequestError)] =>
next.next[None](
{(result: (SearchResults[String] | req.RequestError)) =>
if _TestCheck418Body[SearchResults[String]](
h, result, "bearer")
then
h.complete(true)
end
})
| None =>
h.fail("next_page should not be None")
h.complete(false)
end
} val)
h.dispose_when_done(listener)
4 changes: 2 additions & 2 deletions github_rest_api/gist.pony
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ primitive GetGistCommits
"""
let gc = GistCommitJSONConverter
let plc =
PaginatedListJSONConverter[GistCommit](creds, gc)
PaginatedListJSONConverter[GistCommit](gc)
let p =
Promise[(PaginatedList[GistCommit] |
req.RequestError)]
Expand Down Expand Up @@ -720,7 +720,7 @@ primitive _GetPaginatedGists
=>
let gc = GistJSONConverter
let plc =
PaginatedListJSONConverter[Gist](creds, gc)
PaginatedListJSONConverter[Gist](gc)
let p =
Promise[(PaginatedList[Gist] | req.RequestError)]
let r =
Expand Down
Loading
Loading