|
| 1 | +import React, { useEffect, useState } from "react" |
| 2 | + |
| 3 | +import { Buffer } from "buffer" |
| 4 | + |
| 5 | +import CodeBlock from "@theme/CodeBlock" |
| 6 | + |
| 7 | +export type ModuleTutorialCodeBlockProps = { |
| 8 | + github_username: string |
| 9 | + mixpanel_project_id: string |
| 10 | +} |
| 11 | + |
| 12 | +export const ModuleTutorialCodeBlock: React.FC< |
| 13 | + ModuleTutorialCodeBlockProps |
| 14 | +> = ({ github_username, mixpanel_project_id }) => { |
| 15 | + const mixpanelProjectId = "5736098d8918525aa0a75f1d6dda8321" |
| 16 | + const buffer = new Buffer(mixpanelProjectId) |
| 17 | + const base64data = buffer.toString("base64") |
| 18 | + |
| 19 | + const [githubUsername, setGithubUsername] = useState("unknown") |
| 20 | + |
| 21 | + const getGitHubUsernameFromQueryString = () => { |
| 22 | + // Grab query string params out of URL |
| 23 | + const urlParams = new URLSearchParams(window.location.search) |
| 24 | + let githubUsername = urlParams.get("github_username") |
| 25 | + if (githubUsername == null) { |
| 26 | + githubUsername = "unknown" |
| 27 | + } |
| 28 | + return githubUsername |
| 29 | + } |
| 30 | + |
| 31 | + const codeBlockSnippet = `import uuid |
| 32 | +import base64 |
| 33 | +import json |
| 34 | +from urllib.request import urlopen, Request |
| 35 | +
|
| 36 | +
|
| 37 | +def lambda_handler(event, context): |
| 38 | + url = "https://api.mixpanel.com/track" |
| 39 | +
|
| 40 | + github_username = "%github_username%" |
| 41 | +
|
| 42 | + # This code sets up our mixpanel project ID and sends an event into Mixpanel |
| 43 | + # that includes your GitHub username to signify that you completed the tutorial |
| 44 | + # We don't track anything else about you other than your GitHub username, and we |
| 45 | + # only use this data internally to understand who has completed our tutorial |
| 46 | + mixpanelClientId = "%mixpanel_project_id%" |
| 47 | + tok = base64.b64decode(mixpanelClientId).decode('utf-8') |
| 48 | +
|
| 49 | + payload = [ |
| 50 | + { |
| 51 | + "event": "ModuleTutorialDeploymentComplete", |
| 52 | + "properties": { |
| 53 | + "token": tok, |
| 54 | + "distinct_id": github_username, |
| 55 | + "github_username": github_username, |
| 56 | + "$insert_id": uuid.uuid4().hex |
| 57 | + } |
| 58 | + }] |
| 59 | +
|
| 60 | + headers = { |
| 61 | + "accept": "text/plain", |
| 62 | + "content-type": "application/json" |
| 63 | + } |
| 64 | +
|
| 65 | + httprequest = Request(url, headers=headers, |
| 66 | + data=json.dumps(payload).encode('utf-8')) |
| 67 | +
|
| 68 | + response_object = {} |
| 69 | +
|
| 70 | + with urlopen(httprequest) as response: |
| 71 | + text = "" |
| 72 | + mixpanel_response_text = response.read().decode() |
| 73 | + if mixpanel_response_text == "1": |
| 74 | + text = "Success" |
| 75 | + else: |
| 76 | + text = "Unknown error" |
| 77 | +
|
| 78 | + response_object["operation_status"] = text |
| 79 | + response_object["statusCode"] = response.status |
| 80 | + response_object["body"] = f"Hello, {github_username}, from Gruntwork!" |
| 81 | +
|
| 82 | + return response_object |
| 83 | + ` |
| 84 | + const [codeSnippet, setCodeSnippet] = useState("") |
| 85 | + |
| 86 | + const produceCodeSnippet = (githubUsername: string): string => { |
| 87 | + return codeBlockSnippet |
| 88 | + .replace("%github_username%", githubUsername) |
| 89 | + .replace("%mixpanel_project_id%", base64data) |
| 90 | + } |
| 91 | + |
| 92 | + useEffect(() => { |
| 93 | + setGithubUsername(getGitHubUsernameFromQueryString()) |
| 94 | + }, []) |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + setCodeSnippet(produceCodeSnippet(githubUsername)) |
| 98 | + }, [githubUsername]) |
| 99 | + |
| 100 | + return <CodeBlock language="python">{codeSnippet}</CodeBlock> |
| 101 | +} |
| 102 | + |
| 103 | +export default ModuleTutorialCodeBlock |
0 commit comments