-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Veracity integration and Yodie+Crees bugfixing #11
base: sheffield-service-integration
Are you sure you want to change the base?
Changes from 4 commits
9df8dab
34deefb
fbe59dc
819e7bd
80b92ad
acb9955
28a0671
0cc4264
9f443f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Jobs\RunVeracityService; | ||
use Illuminate\Http\Request; | ||
|
||
use Log; | ||
|
||
class VeracityController extends Controller | ||
{ | ||
|
||
public function annotate(Request $request) | ||
{ | ||
$post = $request->all(); | ||
dispatch(new RunVeracityService($post)); | ||
return; | ||
} | ||
|
||
public function all(Request $request) | ||
{ | ||
$post = $request->all(); | ||
dispatch(new RunVeracityService($post, 'all')); | ||
|
||
return; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7; | ||
use GrahamCampbell\Throttle\Facades\Throttle; | ||
use Log; | ||
|
||
class RunVeracityService extends RunProxyService | ||
{ | ||
/** | ||
* Service Type | ||
* eventRelated, eventType, infoType | ||
*/ | ||
protected $request_type; | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$remaining = config('options.veracity.request_per_time_block'); | ||
$time = config('options.veracity.quota_reset'); | ||
|
||
$minutes = $time / 60; | ||
|
||
$throttler = Throttle::get([ | ||
'ip' => 'ushahidi', | ||
'route' => 'veracity', | ||
], $remaining, $minutes); | ||
|
||
if (!$throttler->check()) { | ||
$this->release($time); | ||
return; | ||
} | ||
$this->runService($this->post); | ||
} | ||
|
||
public function requestProcessing($text) | ||
{ | ||
try { | ||
$client = new Client(); | ||
$url = config('options.veracity.api.url'); | ||
$response = $client->request('POST', $url, | ||
[ | ||
'headers' => [ | ||
'Accept' => 'application/json', | ||
'Content-type' => 'text/plain', | ||
], | ||
'auth' => [ | ||
config('options.veracity.api.username'), | ||
config('options.veracity.api.password') | ||
], | ||
'body' => json_encode([$text]) | ||
] | ||
); | ||
return $response; | ||
} catch (RequestException $e) { | ||
if ($e->hasResponse()) { | ||
Log::error(Psr7\str($e->getResponse())); | ||
} | ||
} | ||
} | ||
|
||
public function format_as_post($post, $response) | ||
{ | ||
$tags = []; | ||
$json = json_decode($response->getBody(), true); | ||
switch($json['rumour_label']) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eh so this is really gross but since we don't receive the names we want to use for categories, I had to map the |
||
case 'unverified': | ||
$label = 'Unverified'; | ||
break; | ||
case false: | ||
$label ='Not a rumor'; | ||
break; | ||
default: | ||
$label = 'Rumor'; | ||
break; | ||
} | ||
$tags = array_merge($tags, ['value' => $label, 'confidence_score' => $json['confidence'] * 100]); | ||
// At the moment it is important to only set fields that you intend to change | ||
// as Post updates are async it is possible to overwrite other user's data | ||
// by performing a full update with the complete object recevied | ||
return [ | ||
'id' => $post['id'], | ||
'tags' => $tags, | ||
'values' => [], | ||
'webhook_uuid' => $post['webhook_uuid'] | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops change to <full_url_of_veracity_api> for clarity