-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-to-elastic.php
165 lines (150 loc) · 5.74 KB
/
send-to-elastic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
class SendToElastic
{
/** Elastic Search Host */
private $elsHost;
/** Elastic Search Index */
private $elsIndex;
/** Elastic Search Type */
private $elsType;
/** BasicAuth github */
private $githubBasicAuth;
/** Github repository */
private $repository;
/** Github repository path */
private $repoPath;
/** Object with github response */
private $data;
/** Id to send in elastic request */
private $id;
/**
* Constructor
*
* @param array $options
*/
public function __construct(array $options)
{
if (empty($options)) {
throw new Exception('Error Processing Request');
}
$this->repository = $options['repository'];
$this->repoPath = $options['repoPath'];
$this->elsType = $options['elsType'];
$this->elsIndex = $options['elsIndex'];
$this->elsHost = isset($options['elsHost']) ? $options['elsHost'] : 'localhost:9200';
$this->githubBasicAuth = $options['githubUser'] . ':' . $options['githubPass'];
}
/**
* Make curl request, if $data is null the request is a GET
* if has value will be a POST
*
* @param [string] $url
* @param [any] $data
* @param [array] $headers
* @return void
*/
public function request($url, $method='GET', $data = null, $headers = null)
{
$ch = curl_init($url);
var_dump($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, dirname(__FILE__) . DIRECTORY_SEPARATOR);
if ($method == 'GET') {
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->githubBasicAuth);
}
if ($method == 'POST' || $method == 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if($method == 'DELETE'){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
if (curl_error($ch)) {
trigger_error('cURL error:' . curl_error($ch));
}
curl_close($ch);
return $response;
}
/**
* get json files on repository
*
* @return void
*/
public function getJson()
{
$url = 'https://api.github.com/repos/' .
$this->repository . '/contents/' . $this->repoPath . '?ref=master'; // make url with repository and path name
$resp = (object)json_decode($this->request($url)); // request github api
foreach ($resp as $repo) {
switch ($repo->type) { // verify type of return
case 'dir': // if is a dir
$dir = json_decode($this->request($repo->url)); // request the content on dir
foreach ($dir as $value) { // runs the repository looking for files
if ($this->isJson($value->name)) { // if file is a json file
$json = json_decode($this->request($value->url)); // request the json content
$this->data = base64_decode($json->content); // decode the content
$this->make(); // call the make(); function to send json to elastic search api
}
}
break;
case 'file': // if is a file
if ($this->isJson($repo->name)) { // verify if it's is a valid json file
$json = json_decode($this->request($repo->url)); // request the json content
$this->data = base64_decode($json->content); // decode the content
$this->make(); // call the make(); function to send json to elastic search api
}
break;
default:
break;
}
}
}
/**
* verify if file is a json by name
*
* @param [string] $j
* @return boolean
*/
public function isJson($j)
{
return (substr($j, 1) != '.' && substr($j, -5) === '.json') ? true : false;
}
/**
* Send json file to elastic search endpoint
*delete existing document before insert a new.delete existing document before insert a new.
* @return void
*/
public function make()
{
$ret = json_decode($this->data); // decode $this->data object
$this->id = str_replace('/', '_', $ret->repo . $ret->path); // concat $reto->repo with $ret->path and repleace '/' to '_' to use like a unique id
$this->delete($this->id); // delete existing document before insert a new.
$url = $this->elsHost . '/' . $this->elsIndex . '/' . $this->elsType . '/' . $this->id; // make url to ES api request
$this->request($url, 'PUT', $this->data); // make post request on $url path with $this->data content
}
/**
* delete els document
*/
public function delete($id){
$url = $this->elsHost . '/' . $this->elsIndex . '/' . $this->elsType . '/' . $id; //
$this->request($url, 'DELETE'); //
}
}
/*
$o = array(
'githubUser' => '', // Github username
'githubPass' => '', // Github passwd
'repository' => '', // Repository name
'repoPath' => '', // Repository Path
'elsType' => '', //
'elsIndex' => '', //
'elsHost' => '' // Elastic search host - default value set localhost:9200
);
$a = new SendToElastic($o); // new instance of classe
$a->getJson(); // call the function
*/