<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Or Carmi </title>
    <description>The latest articles on DEV Community by Or Carmi  (@liiight).</description>
    <link>https://dev.to/liiight</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F37573%2F4b0b92d9-e5b0-4d16-80a8-e022ae3ededc.jpeg</url>
      <title>DEV Community: Or Carmi </title>
      <link>https://dev.to/liiight</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/liiight"/>
    <language>en</language>
    <item>
      <title>An easy and unified way to send notifications</title>
      <dc:creator>Or Carmi </dc:creator>
      <pubDate>Mon, 26 Mar 2018 09:06:34 +0000</pubDate>
      <link>https://dev.to/liiight/an-easy-and-unified-way-to-send-notifications-1eaj</link>
      <guid>https://dev.to/liiight/an-easy-and-unified-way-to-send-notifications-1eaj</guid>
      <description>&lt;p&gt;While I was working on a certain project (the amazing &lt;a href="https://www.flexget.com"&gt;flexget&lt;/a&gt;) I needed to create a class of notifier plugins that will enable users to send push notifications to their notification provider of choice. It quickly became apparent that this was not a small undertaking, as it meant diving into each provider’s API and trying to implement it. &lt;/p&gt;

&lt;p&gt;Along the way you’d encounter confusing docs, missing docs, irregular response patterns, different response formats, different rules of interaction and so forth. Some providers supply their own SDKs, but that means adding more dependencies into an already dependency rich project, not to mention that not all of those dependencies were well maintained, or even maintained at all. There has to be a better way right?&lt;/p&gt;

&lt;p&gt;Well it turned out there wasn’t, or at least I couldn’t find one, so I decided to create &lt;a href="https://www.github.com/liiight/notifiers"&gt;notifiers&lt;/a&gt;, a generic way to interact with many notification providers.&lt;/p&gt;

&lt;p&gt;By creating a unified interface I was able to supply a simple way to interact with any implemented notification provider, regardless of the issues I mentioned earlier. &lt;/p&gt;

&lt;h2&gt;
  
  
  How it’s done
&lt;/h2&gt;

&lt;p&gt;Notification providers APIs (and any other API for that matter) have two things that they share. They accept arguments, some of which are required, and return a response.&lt;/p&gt;

&lt;p&gt;To generically implement the first part, I decided to represent each provider API via a JSON schema, which is very popular and not python specific. &lt;br&gt;
For example, &lt;a href="https://simplepush.io/"&gt;Simplepush&lt;/a&gt; API's correlating JSON schema for notification looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"properties"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"your user key"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"your message"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"message title"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"event"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Event ID"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"additionalProperties"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"required"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Obviously other notification provider schema’s can and do look much more complex than this one, but the point is clear. It is very possible to represent an API schema like so.&lt;/p&gt;

&lt;p&gt;The next part is sending the notification and generically handling the response and/or error(s).  This is done almost exclusively via HTTP requests, which of course means using the &lt;a href="http://docs.python-requests.org/en/master/"&gt;Requests&lt;/a&gt;) module. &lt;/p&gt;

&lt;p&gt;This a one of the parts that requires the most thorough research, as each provider chooses a different way to implement their API. Some providers want a &lt;em&gt;GET&lt;/em&gt; request, most prefer a &lt;em&gt;POST&lt;/em&gt; request, some use URL-Encoded content type, one prefer JSON body, some want the authorisation to be carried via the headers, some via URL parameters and etc. &lt;/p&gt;

&lt;p&gt;Once the requests is sent, the response is parsed into an internal Response object and returned to the user. And that’s it, the easiest way to send notifications!&lt;/p&gt;

&lt;p&gt;Source code: &lt;a href="https://github.com/liiight/notifiers"&gt;https://github.com/liiight/notifiers&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  notifiers/tech
&lt;/h1&gt;

</description>
      <category>python</category>
      <category>showdev</category>
      <category>notifications</category>
    </item>
  </channel>
</rss>
