-
Notifications
You must be signed in to change notification settings - Fork 58
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
Open
moigagoo
wants to merge
29
commits into
master
Choose a base branch
from
feature/httpclient_tutorial
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 19 commits
Commits
Show all changes
29 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 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: | ||
| 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.