summary refs log tree commit diff stats
path: root/request/request.go
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-03-25 00:13:51 +0530
committerAndinus <andinus@nand.sh>2020-03-25 00:13:51 +0530
commit3ee3715ae7777dd6c1804974e2e293406fd1fe54 (patch)
treef035e76ff9ce48d03299381d03906eaff76140dc /request/request.go
parent28e35c0b0b996e17bd94a5b9e78fda90086ba85e (diff)
downloadcetus-3ee3715ae7777dd6c1804974e2e293406fd1fe54.tar.gz
Add apod support & fix errors
Diffstat (limited to 'request/request.go')
-rw-r--r--request/request.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/request/request.go b/request/request.go
new file mode 100644
index 0000000..c5a2dd1
--- /dev/null
+++ b/request/request.go
@@ -0,0 +1,75 @@
+// Request manages all outgoing requests for cetus projects.
+package request
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"time"
+)
+
+// GetRes takes api and params as input and returns the body and
+// error.
+func GetRes(api string, params map[string]string) (string, error) {
+	var body string
+
+	c := http.Client{
+		// TODO: timeout should be configurable by the user
+		Timeout: time.Second * 64,
+	}
+
+	req, err := http.NewRequest(http.MethodGet, api, nil)
+	if err != nil {
+		err = fmt.Errorf("%s\n%s",
+			"request.go: failed to create request",
+			err.Error())
+		return body, err
+	}
+
+	// User-Agent should be passed with every request to make work
+	// easier for the server handler. Include contact information
+	// along with the project name so they could reach you if
+	// required.
+	req.Header.Set("User-Agent",
+		"Andinus / Cetus - https://andinus.nand.sh/projects/cetus")
+
+	// Params is a simple map[string]string which contains
+	// parameters that needs to be passed along with the request.
+	// There is no check involved here & it should be done before
+	// passing params to this function.
+	q := req.URL.Query()
+	for k, v := range params {
+		q.Add(k, v)
+	}
+	req.URL.RawQuery = q.Encode()
+
+	res, err := c.Do(req)
+	if err != nil {
+		err = fmt.Errorf("%s\n%s",
+			"request.go: failed to get response",
+			err.Error())
+		return body, err
+	}
+	defer res.Body.Close()
+
+	if res.StatusCode != 200 {
+		err = fmt.Errorf("Unexpected response status code received: %d %s",
+			res.StatusCode,
+			http.StatusText(res.StatusCode))
+		return body, err
+	}
+
+	// This will read everything to memory and is okay to use here
+	// because the json response received will be small unlike in
+	// download.go (package background) where it is an image.
+	out, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		err = fmt.Errorf("%s\n%s",
+			"request.go: failed to read body to out (var)",
+			err.Error())
+		return body, err
+	}
+
+	body = string(out)
+	return body, err
+}
href='#n238'>238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420