-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_helper.go
61 lines (52 loc) · 1.5 KB
/
api_helper.go
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
/*
* snapWONDERS OpenAPI Specification
* API version: 1.0
*
* Copyright (c) snapWONDERS.com, All rights reserved 2023
*
* Author: Kenneth Springer (https://kennethbspringer.au)
*
* All the snapWONDERS API services is available over the Clearnet / **Web** and Dark Web **Tor** and **I2P**
* Read details: https://snapwonders.com/snapwonders-openapi-specification
*
*/
package main
import (
"encoding/json"
"io"
"log"
"net/http"
)
const (
// The API URLs
URL_SNAPWONDERS_API = "https://api.snapwonders.com/v1/"
URL_UPLOAD_CREATE_MEDIA_URL = "upload/create-media-url"
URL_JOB_CREATE_ANALYSE = "job/analyse"
// Content types
HTTP_CONTENT_TYPE_JSON = "application/json"
// Job status
JOB_STATUS_WAITING = "WAITING"
JOB_STATUS_PROCESSING = "PROCESSING"
JOB_STATUS_COMPLETED = "COMPLETED"
)
// Dumps API error and exit
func dumpApiError(what string, res *http.Response) {
resStatus := res.Status
resBody, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("ERROR: Reading response body failed:[%v], status:[%s]", err, resStatus)
}
var jsonResBody map[string]interface{}
json.Unmarshal([]byte(resBody), &jsonResBody)
resMessage, ok := jsonResBody["message"]
if !ok {
resMessage = resBody
}
log.Fatalf("ERROR: %s:[%s], status:[%s]", what, resMessage, resStatus)
}
// Adds the headers
func addApiHeaders(req *http.Request, contentType string) {
req.Header.Add("Accept", "*/*")
req.Header.Add("Content-Type", contentType)
req.Header.Add("Api_key", SNAPWONDERS_API_KEY)
}