<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jaka Rizmal on Medium]]></title>
        <description><![CDATA[Stories by Jaka Rizmal on Medium]]></description>
        <link>https://medium.com/@jrizmal?source=rss-16bdca344bc5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*PUVuoR7FRz5OApT6.jpg</url>
            <title>Stories by Jaka Rizmal on Medium</title>
            <link>https://medium.com/@jrizmal?source=rss-16bdca344bc5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 10:24:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jrizmal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Setting up a WordPress development environment on an M1 Mac]]></title>
            <link>https://jrizmal.medium.com/setting-up-a-wordpress-development-environment-on-an-m1-mac-b0f4728982ae?source=rss-16bdca344bc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0f4728982ae</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[macbook]]></category>
            <category><![CDATA[wordpress]]></category>
            <dc:creator><![CDATA[Jaka Rizmal]]></dc:creator>
            <pubDate>Tue, 29 Dec 2020 19:57:58 GMT</pubDate>
            <atom:updated>2020-12-30T06:50:08.993Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to get WordPress up and running on a Mac with an M1 chip</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SSrDES_NaKGL6tGc" /><figcaption>Photo by <a href="https://unsplash.com/@tma?utm_source=medium&amp;utm_medium=referral">Tianyi Ma</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I develop WordPress plugins as a side job, and I just got the new MacBook Air M1 (my first Mac ever).</p><h3>XAMPP didn’t work for me</h3><p>I used to use XAMPP on Windows, so I tried installing it on this Mac.<br>I found the Mac version, installed it, clicked start, and it came up with an error. Bummer.<br>I tried googling for a solution, but haven’t found anything useful. It will probably be fixed in some time, but I didn’t bother waiting.</p><p>So I tried an another approach.</p><h3>Installing the tools manually</h3><p>I went on installing all the tools manually. I installed <strong>PHP, MySql </strong>and<strong> Composer. <br></strong>The whole procedure was quite simple.</p><h4>Homebrew</h4><p>To install PHP and MySql, I used Homebrew, a kind of a package manager. It’s basically a tool that installs programs on your Mac, in my case, PHP and MySql.</p><p>To get Homebrew, you need to first download and run an installer. That is what this command does:</p><pre>/bin/bash -c &quot;$(curl -fsSL <a href="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh">https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh</a>)&quot;</pre><p>Continue with the installation, and it’s done. Quite simple really.</p><h4>PHP</h4><p>Once we’ve installed Homebrew, we can use it to get PHP. This is the command that does it:</p><pre>brew install php</pre><h4>MySql</h4><p>Installing MySql is pretty similar. First, we install it and then we start the service.</p><pre>brew install mysql<br>brew services start mysql</pre><h4>Composer</h4><p>I use Composer when creating WordPress plugins, so I’ll show you how to install it. This step is not necessary if you don’t need Composer.</p><p>To install it, use these commands. The first one downloads the setup file, and the second one runs it.</p><pre>php -r &quot;copy(&#39;https://getcomposer.org/installer&#39;, &#39;composer-setup.php&#39;);&quot;</pre><pre>php composer-setup.php</pre><p>The setup generated a file called <strong><em>composer.phar</em></strong>, we move it to the <strong><em>bin</em></strong> folder and add it to PATH, so it can be used globally. You can also paste that last line to your .zshrc, so it will always work.</p><pre>mv composer.phar /usr/local/bin/composer</pre><pre>export PATH=${PATH}:~/.composer/vendor/bin</pre><h3>WordPress</h3><p>We have everything prepared to run WordPress now. Go to <a href="https://wordpress.org/download/">https://wordpress.org/download/</a> and download the package. Then extract it somewhere and open the terminal.</p><p>Navigate to the WordPress folder in terminal, and run this command. The command starts a HTTP server from the current folder.</p><pre>php -S localhost:8000</pre><p>You can now visit <a href="http://localhost:8000,"><strong><em>http://localhost:8000</em></strong></a><strong><em> </em></strong>and<strong><em> </em></strong>you’ll be greeted with the WordPress setup.</p><p>First, you choose your language and then you get to the database connection screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UJKLrJRd8Smm2Q6na5q8wQ.png" /></figure><p>Username is <strong><em>root</em></strong>, leave password empty and the host is <strong><em>localhost.</em></strong></p><p>Now, we need to create a database. Open the terminal and type:</p><pre>mysql -u root</pre><p>You’ve entered the database as <strong>root</strong> user. Now, simply create the database with this SQL statement:</p><pre>CREATE DATABASE &lt;your_database_name&gt;;</pre><p>In my case, that was:</p><pre>CREATE DATABASE test_wp;</pre><p>That was it for the tricky parts. You can now proceed with the installation and then you have a working WordPress instance on your Mac.</p><h3>Side Notes</h3><p>The performance is pretty good on the new Macs. I didn’t measure it but it feels much faster than my Intel i7 Windows laptop. I was quite surprised about that.</p><p>This tutorial helped me out a lot: <a href="https://austencam.com/posts/setting-up-an-m1-mac-for-laravel-development-with-homebrew-php-mysql-valet-and-redis">https://austencam.com/posts/setting-up-an-m1-mac-for-laravel-development-with-homebrew-php-mysql-valet-and-redis</a><br>It’s meant for Laravel development, but some parts are the same for WordPress</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0f4728982ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to authenticate Firebase users in Django rest framework]]></title>
            <link>https://jrizmal.medium.com/how-to-authenticate-firebase-users-in-django-rest-framework-c2d90f5a0a11?source=rss-16bdca344bc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2d90f5a0a11</guid>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Jaka Rizmal]]></dc:creator>
            <pubDate>Mon, 03 Aug 2020 09:15:02 GMT</pubDate>
            <atom:updated>2020-08-03T09:15:02.801Z</atom:updated>
            <content:encoded><![CDATA[<h4>Some times, the default authentication schemes are just not enough. In this article we’re using Firebase auth to authenticate requests. I’ll show you how to create a custom authentication backend to do that.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y_JIGOtPvelvxrmi" /><figcaption>Photo by <a href="https://unsplash.com/@jaye_haych?utm_source=medium&amp;utm_medium=referral">Jaye Haych</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I recently worked on a project that had a “not so standard” auth architecture. Rather than hacking Django’s existing auth backends I’ve decided to write a custom one and make things cleaner.</p><p>The project used Firebase for authenticating users and then stored the user profile in its’ own database.</p><h3>The flow</h3><p>Before I show you the code, I’ll quickly explain the authentication process.</p><h4>Client</h4><p>Every client request to our backend must include the Firebase auth <strong>idToken</strong>. We obtain that token on the client and pack it in the <strong>request headers</strong>. That’s everything special about the client. It could be an app, website or anything really.</p><h4>Server</h4><p>For every request, we need to verify that <strong>idToken</strong>. If token is successfully verified, we then fetch the corresponding user profile from the database. If verification fails, we forbid the access to that resource.</p><p>Seems simple? It actually is.</p><p>Django has a thing that makes it really simple to achieve all this. It’s called an <strong>authentication backend</strong>.</p><p>We’ll also be using <strong>firebase_admin, </strong>a package containing the method used to verify the user on Firebase auth backend. The details on how to include it are listed here: <a href="https://firebase.google.com/docs/admin/setup#python">https://firebase.google.com/docs/admin/setup#python</a> But here’s how I set it up.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c44dc3c41077cdc2d55e3ce38eb65d8f/href">https://medium.com/media/c44dc3c41077cdc2d55e3ce38eb65d8f/href</a></iframe><p>You just put that somewhere in <strong>settings.py.</strong></p><h3>Authentication backend</h3><p>An auth backend is a class that handles verifying the request. In our case it looks like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7969c714599da064484668b6d43f31aa/href">https://medium.com/media/7969c714599da064484668b6d43f31aa/href</a></iframe><p>I’ve created a class that extends <strong>BaseAuthentication</strong>. Our class must provide an <strong>authenticate()</strong> function. If successfull it should return a user and if not, it should return <strong>None</strong>. In this function all the magic happens. Here’s how it works.</p><p>First we extract the <strong>Authorization </strong>header from the request. This is the header that should contain the Firebase user’s <strong>idToken</strong>. If there’s no token, we return None, which means the user could not be authenticated.</p><p>Then, if an <strong>idToken </strong>was provided, we need to verify it with Firebase. As you can see we are using <strong>firebase_admin </strong>package, which has everything we need to do that. The verification could fail if token is expired etc. so we wrap it in a try/catch block. The function throws different types of exceptions according to an error, so you could except on different ones to get a better look into it.</p><p>In a previous step, we also extracted the <strong>uid </strong>corresponding to that <strong>idToken</strong>. In my case, that uid is identifying the <strong>User </strong>object in our database, which holds the profile info. We do a quick query and then return that user. If that fails (user doesn’t exist in our database or something else), we also return None which means auth was not successfull.</p><h4>Enabling our custom auth backend</h4><p>Now we need to tell Django to actually use this backend. We do that in settings.py.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e723d291328b8a2f3bdd392acd8149f2/href">https://medium.com/media/e723d291328b8a2f3bdd392acd8149f2/href</a></iframe><p>What does that string actually mean? <strong>“myauth”</strong> is the django app name which contains the file with the backend. <strong>“authentication”</strong> is the actual filename, in my case authentication.py. And last, <strong>“FirebaseAuthentication”</strong> is the class name. You can modify these in any way you want.</p><p>You could also have <strong>multiple auth backends</strong> listed here. Django tries to authenticate with all of them from the start and stops when it authenticates the first time.</p><h3>That’s it</h3><p>In this article we used Firebase, but the structure itself is the same for every different auth method you want to implement, you only need to provide your own auth logic. I hope this article helped you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nXiRnSggmLrhXiRF" /><figcaption>Photo by <a href="https://unsplash.com/@joshuaearle?utm_source=medium&amp;utm_medium=referral">Joshua Earle</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2d90f5a0a11" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stripe payment system in Flutter]]></title>
            <link>https://jrizmal.medium.com/stripe-payment-system-in-flutter-276b53940927?source=rss-16bdca344bc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/276b53940927</guid>
            <category><![CDATA[credit-card-processing]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[payments]]></category>
            <category><![CDATA[stripe]]></category>
            <dc:creator><![CDATA[Jaka Rizmal]]></dc:creator>
            <pubDate>Fri, 31 Jul 2020 17:11:29 GMT</pubDate>
            <atom:updated>2020-08-01T06:50:05.078Z</atom:updated>
            <content:encoded><![CDATA[<h4>In this article, I’ll show you how to implement card payment with Stripe in Flutter. We’ll be using the “stripe_payment” package.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EyjjV4wWXS_kgBCJ" /><figcaption>Photo by <a href="https://unsplash.com/@averye457?utm_source=medium&amp;utm_medium=referral">Avery Evans</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>What you need</h3><p>Here are some things you’ll need first.</p><h4>A Stripe account</h4><p>You’ll need to create a Stripe account. It’s super simple. We’ll be using their testing environment for now.</p><h4>A server</h4><p>You’ll need to create some kind of a web server. In this article I’ve used Node.js. We’ll only be creating a single endpoint. You could also use a cloud function or something similar.</p><p>You also need a corresponding Stripe library.</p><h3>Getting started</h3><p>We’ll be roughly following Stripe’s payment intent flow as seen on the image below. As you can see, there are 4 entities involved in this flow.</p><p><strong>The customer</strong> is the user of your app,<br><strong>the client</strong> is your Flutter app,<br><strong>the server</strong> is your backend<br>and <strong>Stripe</strong> is Stripe (their backend).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JjHpFkFlDcguJ3vgroKRBg.png" /><figcaption><a href="https://stripe.com/docs/payments/accept-a-payment">https://stripe.com/docs/payments/accept-a-payment</a></figcaption></figure><p>And this is how that flow looks in Flutter code. Don’t worry, I’ll explain it later on.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b7fd17d261e9a9c84b582a9f8549d4a/href">https://medium.com/media/3b7fd17d261e9a9c84b582a9f8549d4a/href</a></iframe><h4>Flutter Stripe package</h4><p>We need to add the “stripe_payment” package to the pubspec.yaml file. Before we start using the library, we need to initialize it, as seen below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dac5e5b68deb4686016f2ba42223cc7/href">https://medium.com/media/7dac5e5b68deb4686016f2ba42223cc7/href</a></iframe><p>We need to provide the publishable key, which is found on the Stripe dashboard.</p><h3>Payment intent</h3><p>First, we need to create a payment intent. In the above code you can see that I’ve created a function called <strong>“_createIntent” </strong>which is used to do just that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/147c1c9dd6f9ee2e782d78dca35089b0/href">https://medium.com/media/147c1c9dd6f9ee2e782d78dca35089b0/href</a></iframe><p>The intent is created on our backend server. I’ve created an endpoint used to do just that. In Flutter, we call this endpoint and save the response. I am using Dart’s <strong>http </strong>package to issue a <strong>POST </strong>request to that endpoint. The response is then converted to a Map with <strong>“json.decode”</strong> and then, a <strong>“PaymentIntent” </strong>object is created using the <strong>“client_secret”</strong> from the response. “client_secret” is actually the only information we need from the server, so you could optimize the process by only sending back this field.</p><p>Here’s how that endpoint looks like. I am using Node.js as it’s quite quick to set up but Stripe library supports many languages so use which ever one you like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcfde0f4a3ba9324367479db02fb956d/href">https://medium.com/media/dcfde0f4a3ba9324367479db02fb956d/href</a></iframe><p>You’ll usually send some extra information in the request, so you can identify why the payment is coming (order_id, user_id or something similar). You can also send the charge amount, but I would not recommend that, as clients should not really be trusted. Instead, <strong>calculate the price in this endpoint. </strong>In this example I’ve used a fixed amount of $14.95 (the prices are in cents).</p><h3><strong>Collecting card details</strong></h3><p>Now we need to ask user for payment info. Luckily the process is very simple as the library provides the <strong>card input form.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8070b9fae1194d6bb1061e8fe68a009/href">https://medium.com/media/e8070b9fae1194d6bb1061e8fe68a009/href</a></iframe><p>This code first shows the card input form. The user then inputs the card details and clicks save. That info is then sent to Stripe and a <strong>“PaymentMethod”</strong> object is created.</p><p>We must then associate this payment method with previously created intent. You can see that in the 7th line. The only thing we must do now, is to confirm the payment.</p><h3>Confirm payment</h3><p>We now have all the data we need to confirm the payment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5bb98c56f096435e2e37d30bdff50ea/href">https://medium.com/media/f5bb98c56f096435e2e37d30bdff50ea/href</a></iframe><p>When the payment is confirmed, we get back a <strong>“PaymentIntentResult”</strong> object. We need to check its’ <strong>“status”</strong> property to see if everything went ok. If the status is <strong>“succeeded”</strong> we know that the payment went through. Feel free to check for other statuses as well.</p><h3>Error catching</h3><p>This flow may throw multiple exceptions so I suggest wrapping it in a <strong>try/catch </strong>block. In my code I also handled <strong>“PlatformException”</strong> separately. This exception is thrown for example if the user exits the card input form. Feel free to experiment with this as it can bring valuable information for user experience.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c5843c23ae17eed9c4f917db9ac16fd/href">https://medium.com/media/6c5843c23ae17eed9c4f917db9ac16fd/href</a></iframe><h3>Demo</h3><p>I’ve created a simple page that shows this in real world. Here’s the UI code (it’s a stateful widget).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce082c74fb385421f8ff571d4988dc74/href">https://medium.com/media/ce082c74fb385421f8ff571d4988dc74/href</a></iframe><p>When the button is pressed, the flow gets triggered which then changes the background color. But because a picture is worth a thousand words (a gif even more I believe), here’s a preview.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*dtS5A59DuUbDpAAETJC-Og.gif" /></figure><p>I’m using the “test card” number. This card will always be accepted. You can put in any expiration date and CVC you want.</p><h4>That’s it</h4><p>You should now be able to accept payments in your app. Feel free to write a response :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=276b53940927" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying a Python web scraper on Google Cloud Platform]]></title>
            <link>https://jrizmal.medium.com/deploying-a-python-web-scraper-on-google-cloud-platform-12fe24e57bb0?source=rss-16bdca344bc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/12fe24e57bb0</guid>
            <category><![CDATA[cron]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[scheduler]]></category>
            <dc:creator><![CDATA[Jaka Rizmal]]></dc:creator>
            <pubDate>Mon, 20 Jul 2020 16:46:50 GMT</pubDate>
            <atom:updated>2020-07-22T11:18:14.211Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>In this article, I’ll show you how I built my first web scraper, deployed it in a Cloud Function and make it so it repeats every 10 minutes.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6eEZVRW7Yjc5Vyur" /><figcaption>Photo by <a href="https://unsplash.com/@dianamia?utm_source=medium&amp;utm_medium=referral">C Dustin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This article consists of <strong>three main parts.</strong> <br>In first part, I’ll quickly show you how I built a scraper itself, but I wont go into detail, as there are a lot of resources on this topic already.<br>In the second part, I’ll show you how to set up a Cloud Function in Google Cloud Platform.<br>And in the third part, I’ll show you how to make it repeat itself every 10 minutes.</p><h3>Web scraper</h3><p>I chose Python and a library called BeautifulSoup4. Based on my quick research, the library is very popular, which usually means lees headache later on. I was surprised at how nice it was to learn (I had zero experience).</p><p>As I said before, I won’t go into detail, as there are lots of very good tutorials online. The official docs are also great.</p><h4>Basics</h4><p>I used BeautifulSoup4 and “requests” library. With requests, I first accesed the website that I’m scraping, and then fed the raw response to BeautifulSoup4, which then enabled me to dig through the page in Python.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99b7ba4fbe91b3c52c2fd5d4f8a73c73/href">https://medium.com/media/99b7ba4fbe91b3c52c2fd5d4f8a73c73/href</a></iframe><p>In line 1, I send a GET request to the URL provided, then in line 2, I get the whole page document as a string. The “windows-1250” part is the page encoding. This is usually “UTF-8” but I scraped a page that used this encoding. In last step, we feed the string to a bs4 constructor and thus we have created the “soup” object.</p><p>Now we’re ready for digging.</p><h4>Scraping</h4><p>In my case, the page I was scraping displayed results in rows. all the results were structured the same so it was very easy to pull the data out.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea573df9aa13edfca82fa3c1af3878e2/href">https://medium.com/media/ea573df9aa13edfca82fa3c1af3878e2/href</a></iframe><p>First, I got all the result rows. I was looking for “div”s with a class “result-row”. Then for every row, I looked for a “span” with a class “result-title” and saved its’ value to a list.</p><h4>Saving data</h4><p>At the end, I saved all this data to a Firestore database.</p><p>Good, now I have the titles in my database. But there is a problem. New titles are always coming in at a steady rate and I want to have my database to be as up to date as possible.</p><h3>Automating it</h3><p>If I had time, I would run this script every hour or two, and that would be it. But I dont have any time, plus, I want to learn something new.</p><p>At first, I thought about setting up a CRON job on my computer, that runs the Python script every couple of minutes, but I quickly ruled that out as I don’t want to have random scripts running on my PC and it wouldn’t work at night, when my PC is turned off.</p><p>I decided that I would run the script somewhere on the cloud, where it could live and repeat itself infinitely.</p><h4>Google Cloud Platform</h4><p>I chose GCP, because I’ve used it before to host websites and databases. But I’ve never done anything like this before. After some research I had a good idea how the architecture would need to look.</p><p><strong>Cloud Function<br></strong>The whole script I wrote for my PC would be put into a cloud function. A cloud function is basically a piece of code that lives somewhere on Google servers, and waits for a trigger, does it’s thing and quits. I was really surprised how easy it was to set it up. I wrapped my code into a function and uploaded it, that was it.</p><h4>Creating a function</h4><p>At first, you need to go to Google Cloud Console and choose “Cloud Functions” in the main menu. When you click “create a function”, a form appears.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*laP8esophAWAxTPPw2jRfw.png" /><figcaption>Form step one</figcaption></figure><p>At step one, you name a function and then set it’s memory. I’ve chosen 256MiB as my function was fairly simple, but I would recommend trying it out for yourself. But then again, running these functions is very cheap, so rather go a bit higher.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*nfaL79Kyk_Kvyiw34Lc8sg.png" /><figcaption>Form step two</figcaption></figure><p>The second step is to set the trigger, so that a function knows when to run. The function could listen to a HTTP request or something else, but I’ve chosen a Cloud Pub/Sub mechanism as it seemed the easiest. I’ll now quickly explain how that works.</p><p><strong>Cloud Pub/Sub</strong></p><p>This is a mechanism that is used for communicating or triggering between different components in GCP. At first you need to create a Pub/Sub topic, which acts something like an event that some components can trigger and other components can be subscribed to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*fSceOW7vji9_ESwJbZXbbw.png" /><figcaption>Pub/Sub topic</figcaption></figure><p>Just search “Pub/Sub” in search bar and click create topic, name it and save it. I’ve created a topic named “update-db” which I’ll use for triggering the function.</p><h4>Let’s continue creating the function</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*nfaL79Kyk_Kvyiw34Lc8sg.png" /><figcaption>Function trigger</figcaption></figure><p>Now that we’ve created the Pub/Sub topic, we select it in the dropdown and continue.</p><h4>Uploading the source code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*oiC1AiwrSuxmSxzwmr5aZg.png" /><figcaption>Uploading source code</figcaption></figure><p>As you can see, you could write the code in the inline editor in the browser itself, but I’d suggest to <strong>ZIP </strong>it first and then upload.</p><p>You also need to choose a runtime. These functions can be written in lots of different languages, but mine is written in <strong>Python 3.8</strong>, so I chose that option.</p><p>As you can see, there is a <strong>“Function to execute”</strong> field at the bottom. You need to wrap your script in a function named the same, so Google knows what to run.</p><p><strong>One more thing<br></strong>You need to add a “requirements.txt” file to the ZIP archive, where you list all the “pip” dependencies your function uses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*upoTHtNYEBGWPOK_y2CNrA.png" /><figcaption>requirements.txt</figcaption></figure><p>That should be it for the function part.</p><h3>Triggering the function every 10 minutes</h3><p>Remember the Pub/Sub topic we created before and subscribed to it in the “Trigger” section of the function? Now we need to actually trigger it somehow. Preferrably once every 10 minutes.</p><h4>Cloud Scheduler</h4><p>For that purpose I created a Cloud Scheduler. Find it in the main menu and click “Create a job”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*GtcW5ldFqshARBSuV_oPdA.png" /><figcaption>Scheduler job creation form</figcaption></figure><p>You can name it anything you want, then add a description.</p><p>For the <strong>frequency</strong>, you must provide a string in a <strong>“unix-cron”</strong> format. It’s an interesting topic as itself, but at this moment I’ll just show you my case. In my case, the string is “*/10 * * * *”.</p><p>There’s also this great website which helps you with that. <a href="https://crontab.guru/every-10-minutes">https://crontab.guru/every-10-minutes</a></p><p>Then, choose your <strong>timezone</strong>, you can just write out your country and it will help you.</p><p>Then write in the <strong>Pub/Sub topic</strong> name that you want to trigger. You need to copy the topic name that you subscribed to in the function. The payload doesn’t serve us any purpose in this case. It’s used to transfer some data with the triggering of a topic. Put anything you want in here.</p><h4><strong>That’s it!</strong></h4><p>By now, the Cloud Scheduler should be triggering the Pub/Sub topic, which is sending a pulse to our Cloud Function. It’s nicely updating our database every 10 minutes.</p><p>The techniques used in this article could be used in many applications. I’m happy with the result and I’ve learned something new which is awesome.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uyPSUYJtoELmyp4n" /><figcaption>Photo by <a href="https://unsplash.com/@lukechesser?utm_source=medium&amp;utm_medium=referral">Luke Chesser</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Thank you for reading my <strong>first Medium article!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12fe24e57bb0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>