<commit_before><?php namespace webhook;

use Exception;
use IntegerField;
use Reference;
use Request;
use spitfire\Model;
use spitfire\storage\database\Schema;
use StringField;
use TextField;
use function db;

class CallModel extends Model
{
	
	public function definitions(Schema $schema) {
		$schema->hook     = new Reference('webhook\hook');
		$schema->target   = new StringField(60);
		$schema->called   = new IntegerField(true);
		$schema->response = new TextField();
		$schema->tried    = new IntegerField();
	}
	
	public static function run() {
		$next = db()->table('webhook\call')->get('called', null, 'IS')->fetch();
		$url  = $next->hook->url;
		
		$payload = $next->hook->mask2Array();
		$payload['id'] = $next->target;
		
		$request = new Request($url);
		
		try {
			$next->response = $request->send($payload);
			$next->called   = time();
			$next->store();
		} 
		catch (Exception$e) {
			$next->tried    = $next->tried + 1;
			$next->called   = $next->tried > 3? time() : null;
			$next->store();
		}
	}

}<commit_msg>If there's not webhook to call. Just skip it.<commit_after><?php namespace webhook;

use Exception;
use IntegerField;
use Reference;
use Request;
use spitfire\Model;
use spitfire\storage\database\Schema;
use StringField;
use TextField;
use function db;

class CallModel extends Model
{
	
	public function definitions(Schema $schema) {
		$schema->hook     = new Reference('webhook\hook');
		$schema->target   = new StringField(60);
		$schema->called   = new IntegerField(true);
		$schema->response = new TextField();
		$schema->tried    = new IntegerField();
	}
	
	public static function run() {
		$next = db()->table('webhook\call')->get('called', null, 'IS')->fetch();
		
		if (!$next) { return; } //Everything is fine
		
		$url  = $next->hook->url;
		
		$payload = $next->hook->mask2Array();
		$payload['id'] = $next->target;
		
		$request = new Request($url);
		
		try {
			$next->response = $request->send($payload);
			$next->called   = time();
			$next->store();
		} 
		catch (Exception$e) {
			$next->tried    = $next->tried + 1;
			$next->called   = $next->tried > 3? time() : null;
			$next->store();
		}
	}

}
{'fraction_non_alphanumeric': 0.17928471899674872, 'fraction_numerical': 0.004644681839294009, 'mean_word_length': 3.612419700214133, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6482146', 'n_tokens_mistral': 803, 'n_tokens_neox': 705, 'n_words': 172}