Skip to content

Instantly share code, notes, and snippets.

View khamidou's full-sized avatar
🎧

Karim Hamidou khamidou

🎧
View GitHub Profile
@khamidou
khamidou / genjwt.py
Created May 24, 2018 16:12
How to generate Github API JWT tokens in Python
#!/usr/bin/env python3
#
# This is an example script that generates JWT tokens for the Github API.
# Usage: genjwt.py your_github_private_key.pem
#
# After getting a token, you can make curl requests to the API like this:
# curl -i -H "Authorization: Bearer JWT_TOKEN" -H "Accept: application/vnd.github.machine-man-preview+json" "https://api.github.com/app"
import sys
import jwt
import time
@khamidou
khamidou / exponential_backoff.rb
Last active April 21, 2022 13:44 — forked from nathanl/exponential_backoff.rb
Exponential backoff with jitter in Ruby
# Exponential backoff in Ruby
begin
make_request
rescue RequestError => e
if retries <= max_retries
retries += 1
sleep 2 ** retries + rand(20)
retry
else
raise "Timeout: #{e.message}"
@khamidou
khamidou / gist:6b7e8702f8fc0e8dd251
Last active February 28, 2022 08:57
Ever got a mysterious "IOError: cannot send to <Channel id=1 closed>" from pytest with xdist and gevent? Scroll to the comments to see how to solve it.
vagrant@precise64:/vagrant/tests$ py.test -n 2 general/
================================================= test session starts ==================================================
platform linux2 -- Python 2.7.3 -- py-1.4.26 -- pytest-2.6.4
plugins: xdist, httpretty
gw0 [24] / gw1 okINTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/main.py", line 84, in wrap_session
INTERNALERROR> doit(config, session)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/main.py", line 122, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 413, in __call__
@khamidou
khamidou / verify.rb
Last active September 28, 2021 13:20
Verifying Nylas webhooks using Ruby
require 'openssl'
# verify_webhook: check that a webhook was sent by a Nylas server.
# params:
# - request: the request object you get from Rails,
# - nylas_app_secret: your app secret.
def verify_webhook(request, nylas_app_secret)
digest = OpenSSL::Digest.new('sha256')
data = request.body.read
digest = OpenSSL::HMAC.hexdigest(digest, nylas_app_secret, data)
<form method = "POST" action="resultat.html"> {% csrf_token %}
<select name="entry_to_delete">
{% for product_name, product_id in history %}
<option value={{ product_id }}>{{ product_name }}</option>
{% endfor %}
</select>
<input type = "submit" value = "Submit">
</form>
from django.db import models
from django import forms
class Niveau(forms.Form):
choix_niveau=(
('deb', 'debutant'),
('int', 'intermédiaire'),
('av', 'avancé'))
niveau=forms.ChoiceField(choices=choix_niveau)

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@khamidou
khamidou / verify_github.py
Created June 7, 2018 03:41
How to verify github webhooks
# Mostly cribbed from https://github.com/carlos-jenkins/python-github-webhooks/blob/759b67e3af8ed7334467b7d359cd00a10b0ac3c7/webhooks.py#L73
import hmac
def verify_github_webhook(secret, hub_signature_header, request_body):
sha_name, sha_value = hub_signature_header.split('=')
if sha_name != 'sha1':
raise ValueError('Unknown signature algorithm')
mac = hmac.new(secret.encode('utf-8'), msg=request_body, digestmod='sha1')
if not hmac.compare_digest(str(mac.hexdigest()), str(sha_value)):
@khamidou
khamidou / create-event.php
Created April 9, 2018 19:19
Creating a calendar event using the Nylas API with PHP and cURL
<?php
$ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
$CALENDAR_ID = "The id of a calendar you got from the Nylas /calendars API";
$ch = curl_init("https://api.nylas.com/events");
# Setup request to send json via POST.
$payload = json_encode(
array("title" => "Alan Turing Birthdate",
"description" => "Alan Turing Birthdate",
@khamidou
khamidou / list-events.php
Created April 9, 2018 19:18
Listing calendar events from the Nylas API using PHP and cURL
<?php
$ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
$ch = curl_init("https://api.nylas.com/events");
# Pass the access token as an HTTP basic auth username
curl_setopt($ch, CURLOPT_USERPWD, $ACCESS_TOKEN . ":");
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);