-
Notifications
You must be signed in to change notification settings - Fork 60
Docs: HTTP Client Tutorial #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
9c6b0de
Docs: Tutorials: HTTP Client: Chapter 1.
moigagoo c0c4e5c
Docs: Tutorials: HTTP Client: Chapter 2 (WIP).
moigagoo 2bd39c0
Docs: Tutorials: HTTP Client: Chapter 2.
moigagoo 7ecd27e
Docs: Uptimemon: Chapter 2: Replace status.im with mock.codes for rel…
moigagoo a429ab4
Docs: Tutorials: HTTP Client: Chapter 3.
moigagoo ff0899d
Docs: Tutorials: HTTP Client: Chapter 4 (WIP).
moigagoo 3106871
Docs: Tutorials: HTTP Client: Chapter 4 (WIP).
moigagoo 1591e5b
Docs: Tutorials: HTTP Client: Chapter 4.
moigagoo a8e520b
Docs: Tutorials: HTTP Client: Chapter 5 (WIP).
moigagoo a00a3a0
Docs: Tutorials: HTTP Client: Chapter 5.
moigagoo 1d1c98a
Docs: Tutorials: HTTP Client: Chapter 6.
moigagoo b3bada4
Docs: Tutorials: HTTP Client: Add links to the source code.
moigagoo 9003c05
Build API docs for modules in apps/http.
moigagoo 0b5a75d
Fix exception printing.
moigagoo 3ed384a
Docs: Tutorials: HTTP Client: Chapter 1: Add ref links.
moigagoo 9d7e231
Docs: Tutorials: HTTP Client: Add ref links to all chapters.
moigagoo 25dccf5
CI: Add shiftinclude.
moigagoo e801920
CI: Add shiftinclude.
moigagoo 2ab3b67
Docs: Chapter 1: Replace hardcoded code snippets with includes.
moigagoo f0a5e9f
Docs: Uptimemon: Replace line numbers with anchors.
moigagoo a2312cb
Docs: Uptimemon: Chapter 1: Improve exception handling.
moigagoo ce9e273
Docs: Uptimemon: Chapter 2: Replace copypasta with includes.
moigagoo 0d73475
Docs: Uptimemon: Chapter 3: Replace copypasta with includes.
moigagoo ddc599a
Docs: Uptimemon: Chapter 4: Replace copypasta with includes.
moigagoo 927495b
Docs: Uptimemon: Chapter 5: Replace copypasta with includes.
moigagoo 63b6d62
Docs: Uptimemon: Fix missing exceptions.
moigagoo 5930532
Docs: Uptimemon: Chapter 6: Replace copypasta with includes.
moigagoo 3dd9503
HTTP client tutorial: Rename directory to http_client.
moigagoo 1e85d89
HTTP client tutorial: Switch from single files to Nimble projects in …
moigagoo 3f9e253
Docs: Concepts: Clarify noCancel usage.
moigagoo 732e568
Docs: HTTP Client: Remove info about noCancel usage.
moigagoo 94e863b
Docs: HTTP Client: Remove redundant noCancel.
moigagoo d47faa6
Examples: Remove redundant noCancel.
moigagoo 79a315d
Docs: HTTP Client: Fix info about session purpose.
moigagoo 1c7574e
Docs: HTTP Client: Fix the section about serial vs. concurrent requests.
moigagoo 09dbf48
Examples: HTTP Client: Require Chronos >= 4.2.2.
moigagoo f680e31
Examples: HTTP Client: Update check[uris] to handle cancellation.
moigagoo 9d17fa5
Docs: HTTP Client: Add info about cancellation policy in check[uris].
moigagoo f3e69f4
Docs: HTTP Client: Add a general tip about HTTP protocol.
moigagoo 7efaeb7
Examples: HTTP Client: Use valueOr instead of isErr.
moigagoo d13a54c
Docs: HTTP Client: Remove a weasel word.
moigagoo a9bfc8f
Docs: HTTP Client: Fix text about error checking on request creation.
moigagoo 4b0addc
Docs: HTTP Client: Split address resolution from async requests.
moigagoo 4d32e20
Merge branch 'master' into feature/httpclient_tutorial
moigagoo cb269e3
Docs: HTTP Client: Optimize marker lookup to detect valid HTML in che…
moigagoo 25e889f
Docs: HTTP Client: Add finally block to close request.
moigagoo 3a3dff5
Ignore documentation samples technical files.
moigagoo 2596b78
Update .github/workflows/doc.yml
moigagoo c4700bc
Update .github/workflows/doc.yml
moigagoo 68f87c4
Update .github/workflows/doc.yml
moigagoo 5b8a783
Docs: HTTP Client: Chapter 6: Handle AsyncSemaphoreError in check.
moigagoo ee5b09c
Docs: HTTP Client: Chapter 6: Update text about AsyncSemaphoreError.
moigagoo 75fdd97
Docs: HTTP Client: Add chapter on session reuse.
moigagoo c68886a
Docs: HTTP Client: Chapter 6: Rewrite marker lookup to use strings in…
moigagoo f6c848e
Add readOnce flavor that reads directly to a string.
moigagoo 680c48e
Docs: HTTP Client: Timeout: Use wait instead of withTimeout.
moigagoo 68b53c4
Docs: HTTP Client: Add a page about handling erroneous URIs.
moigagoo 61625f6
Docs: HTTP client: Rewrite chapter about streaming, fix code samples.
moigagoo d7b202d
Update docs/src/tutorials/http_client/chapter1.md
moigagoo c854998
Docs: Examples: HTTP Client: Close bodyReader after use.
moigagoo e931b12
Docs: HTTP Client: Update stdout messages.
moigagoo 396bafc
Docs: Examples: HTTP Client: Close bodyReader *properly*; close reque…
moigagoo 7d0cd02
Docs: HTTP Client: Remove chapter about invalid URI handling and DNS …
moigagoo 5e6c456
Docs: HTTP Client: Add a note aboutt timer drift.
moigagoo cb65970
Docs: HTTP Client: Add more info about drift.
moigagoo 9a4d749
Remove string-aiming readOnce variant.
moigagoo 3946f0d
Docs: HTTP Client: Add request and response finalization and closing.
moigagoo 735bd7a
Remove compiled binary.
moigagoo cac1af9
Docs: Examples: HTTP Client: Work around a bug with except in let block.
moigagoo 5c620a3
Docs: HTTP Client: Refactor chapter about streaming.
moigagoo 206a507
Docs: HTTP Client: Fix typo.
moigagoo 627c2d2
Remove compiled binaries.
moigagoo 6263479
Docs: HTTP Client: Update code samples to use refactored findMarker.
moigagoo 23b2578
Docs: HTTP Client: Replace getCurrentExceptionMsg with e.msg.
moigagoo b61015b
Move examples out of docs.
moigagoo 1b406a7
Docs: HTTP Client: Replace e.msg with getCurrentExceptionMsg.
moigagoo 3bf3057
Merge branch 'master' into feature/httpclient_tutorial
moigagoo 83715b9
Docs: HTTP Client: Chapter 4: Clarify the usage of `except ... as ...…
moigagoo d5e4c60
Merge branch 'master' into feature/httpclient_tutorial
moigagoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import chronos/apps/http/httpclient | ||
|
|
||
| proc check(uri: string) {.async.} = | ||
| let session = HttpSessionRef.new() | ||
|
|
||
| try: | ||
| let response = await session.fetch(parseUri(uri)) | ||
|
|
||
| if response.status == 200: | ||
| echo "[OK] " & uri | ||
| else: | ||
| echo "[NOK] " & uri & ": " & $response.status | ||
| except CatchableError: | ||
|
moigagoo marked this conversation as resolved.
Outdated
|
||
| echo "[ERR] " & uri & ": " & getCurrentExceptionMsg() | ||
| finally: | ||
| await noCancel(session.closeWait()) | ||
|
|
||
| when isMainModule: | ||
| waitFor check("https://google.com") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import chronos/apps/http/httpclient | ||
|
|
||
| const uris = @[ | ||
| "https://duckduckgo.com/?q=chronos", "https://mock.codes/403", "http://123.456.78.90" | ||
| ] | ||
|
|
||
| proc check(session: HttpSessionRef, uri: string) {.async.} = | ||
| try: | ||
| let response = await session.fetch(parseUri(uri)) | ||
|
|
||
| if response.status == 200: | ||
| echo "[OK] " & uri | ||
| else: | ||
| echo "[NOK] " & uri & ": " & $response.status | ||
| except CatchableError: | ||
| echo "[ERR] " & uri & ": " & getCurrentExceptionMsg() | ||
|
|
||
| proc check(uris: seq[string]) {.async.} = | ||
| let session = HttpSessionRef.new() | ||
| var futures: seq[Future[void]] | ||
|
|
||
| for uri in uris: | ||
| futures.add(session.check(uri)) | ||
|
|
||
| await allFutures(futures) | ||
| await noCancel(session.closeWait()) | ||
|
|
||
| when isMainModule: | ||
| waitFor check(uris) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import chronos/apps/http/httpclient | ||
|
|
||
| const uris = @[ | ||
| "https://duckduckgo.com/?q=chronos", "https://mock.codes/403", "http://123.456.78.90", | ||
| "http://10.255.255.1", | ||
| ] | ||
|
|
||
| proc check(session: HttpSessionRef, uri: string) {.async.} = | ||
| try: | ||
| let responseFuture = session.fetch(parseUri(uri)) | ||
|
|
||
| if await responseFuture.withTimeout(5.seconds): | ||
| let response = responseFuture.read() | ||
|
|
||
| if response.status == 200: | ||
| echo "[OK] " & uri | ||
| else: | ||
| echo "[NOK] " & uri & ": " & $response.status | ||
| else: | ||
| raise newException(AsyncTimeoutError, "Connection timed out") | ||
| except CatchableError: | ||
| echo "[ERR] " & uri & ": " & getCurrentExceptionMsg() | ||
|
|
||
| proc check(uris: seq[string]) {.async.} = | ||
| let session = HttpSessionRef.new() | ||
| var futures: seq[Future[void]] | ||
|
|
||
| for uri in uris: | ||
| futures.add(session.check(uri)) | ||
|
|
||
| await allFutures(futures) | ||
| await noCancel(session.closeWait()) | ||
|
|
||
| when isMainModule: | ||
| waitFor check(uris) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import chronos/apps/http/httpclient | ||
|
|
||
| const uris = @[ | ||
| "https://duckduckgo.com/?q=chronos", "https://mock.codes/403", "http://123.456.78.90", | ||
| "http://10.255.255.1", "https://html.spec.whatwg.org/", "https://mock.codes/200", | ||
| ] | ||
|
|
||
| proc findMarker(response: HttpClientResponseRef): Future[bool] {.async.} = | ||
| let bodyReader = response.getBodyReader() | ||
|
|
||
| var | ||
| buffer = newSeq[byte](1024) | ||
| fetchedBytes: seq[byte] | ||
|
|
||
| while not result and len(fetchedBytes) <= 10 * 1024: | ||
| let bytesRead = await bodyReader.readOnce(addr(buffer[0]), len(buffer)) | ||
|
|
||
| if bytesRead == 0: | ||
| break | ||
|
|
||
| fetchedBytes &= buffer | ||
|
|
||
| result = "<html" in bytesToString(fetchedBytes) | ||
|
|
||
| proc check(session: HttpSessionRef, uri: string) {.async.} = | ||
| try: | ||
| let request = HttpClientRequestRef.new(session, uri) | ||
|
|
||
| if request.isErr: | ||
| raise newException(HttpRequestError, request.error) | ||
|
|
||
| let responseFuture = request.value.send() | ||
|
|
||
| if await responseFuture.withTimeout(5.seconds): | ||
| let response = responseFuture.read() | ||
|
|
||
| if response.status == 200: | ||
| let markerFound = await findMarker(response) | ||
|
|
||
| if markerFound: | ||
| echo "[OK] " & uri | ||
| else: | ||
| echo "[NOK] " & uri & ": Not valid HTML" | ||
| else: | ||
| echo "[NOK] " & uri & ": " & $response.status | ||
| else: | ||
| raise newException(AsyncTimeoutError, "Connection timed out") | ||
| except CatchableError: | ||
| echo "[ERR] " & uri & ": " & getCurrentExceptionMsg() | ||
|
|
||
| proc check(uris: seq[string]) {.async.} = | ||
| let session = HttpSessionRef.new() | ||
| var futures: seq[Future[void]] | ||
|
|
||
| for uri in uris: | ||
| futures.add(session.check(uri)) | ||
|
|
||
| await allFutures(futures) | ||
| await noCancel(session.closeWait()) | ||
|
|
||
| when isMainModule: | ||
| waitFor check(uris) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import chronos/apps/http/httpclient | ||
|
|
||
| const | ||
| ntfyTopic = "X3JIaLZSrFqBJXfJ" | ||
| uris = @[ | ||
| "https://duckduckgo.com/?q=chronos", "https://mock.codes/403", | ||
| "http://123.456.78.90", "http://10.255.255.1", "https://html.spec.whatwg.org/", | ||
| "https://mock.codes/200", | ||
| ] | ||
|
|
||
| proc sendAlert( | ||
| session: HttpSessionRef, message: string, priority = 3 | ||
| ) {.async.} = | ||
| let | ||
| headers = {"Title": "Chronos Uptime Monitor", "Priority": $priority} | ||
| body = message.stringToBytes() | ||
| request = HttpClientRequestRef.new( | ||
| session, | ||
| "https://ntfy.sh/" & ntfyTopic, | ||
| meth = MethodPost, | ||
| headers = headers, | ||
| body = body, | ||
| ) | ||
|
|
||
| if request.isOk: | ||
| try: | ||
| let response = await request.get.send() | ||
| await response.closeWait() | ||
| except CatchableError: | ||
| echo "[WRN] Failed to send alert: " & getCurrentExceptionMsg() | ||
|
|
||
| proc findMarker(response: HttpClientResponseRef): Future[bool] {.async.} = | ||
| let bodyReader = response.getBodyReader() | ||
|
|
||
| var | ||
| buffer = newSeq[byte](1024) | ||
| fetchedBytes: seq[byte] | ||
|
|
||
| while not result and len(fetchedBytes) <= 10 * 1024: | ||
| let bytesRead = await bodyReader.readOnce(addr(buffer[0]), len(buffer)) | ||
|
|
||
| if bytesRead == 0: | ||
| break | ||
|
|
||
| fetchedBytes &= buffer | ||
|
|
||
| result = "<html" in bytesToString(fetchedBytes) | ||
|
|
||
| proc check(session: HttpSessionRef, uri: string) {.async.} = | ||
| try: | ||
| let request = HttpClientRequestRef.new(session, uri) | ||
|
|
||
| if request.isErr: | ||
| raise newException(HttpRequestError, request.error) | ||
|
|
||
| let responseFuture = request.get.send() | ||
|
|
||
| if await responseFuture.withTimeout(5.seconds): | ||
| let response = responseFuture.read() | ||
|
|
||
| if response.status == 200: | ||
| let markerFound = await findMarker(response) | ||
|
|
||
| if markerFound: | ||
| echo "[OK] " & uri | ||
| else: | ||
| let message = "[NOK] " & uri & ": Not valid HTML" | ||
| echo message | ||
| await session.sendAlert(message) | ||
| else: | ||
| let message = "[NOK] " & uri & ": " & $response.status | ||
| echo message | ||
| await session.sendAlert(message) | ||
| else: | ||
| raise newException(AsyncTimeoutError, "Connection timed out") | ||
| except CatchableError: | ||
| let message = "[ERR] " & uri & ": " & getCurrentExceptionMsg() | ||
| echo message | ||
| await session.sendAlert(message, 4) | ||
|
|
||
| proc check(uris: seq[string]) {.async.} = | ||
| let session = HttpSessionRef.new() | ||
| var futures: seq[Future[void]] | ||
|
|
||
| for uri in uris: | ||
| futures.add(session.check(uri)) | ||
|
|
||
| await allFutures(futures) | ||
| await noCancel(session.closeWait()) | ||
|
|
||
| when isMainModule: | ||
| waitFor check(uris) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.