successfully connecting and parsing data from API
This commit is contained in:
+6
-3
@@ -77,27 +77,29 @@ request_json_array :: proc(
|
||||
) {
|
||||
resp, req_err := http.http_get(url)
|
||||
if req_err != nil {
|
||||
log.errorf("failed getting response from API: %w", req_err)
|
||||
defer free(req_err)
|
||||
return nil, json.Null(nil), api_error(req_err.code, strings.clone(req_err.message)), false
|
||||
}
|
||||
defer delete(resp.body)
|
||||
|
||||
if resp.status != 200 {
|
||||
log.errorf("response did not return with 200 => %d", resp.status)
|
||||
return nil,
|
||||
json.Null(nil),
|
||||
api_error(resp.status, strings.clone(strings.trim_space(resp.body))),
|
||||
false
|
||||
}
|
||||
|
||||
log.debugf("got %s => %s", url, resp.body)
|
||||
|
||||
v, perr := json.parse_string(resp.body)
|
||||
if perr != .None {
|
||||
log.errorf("parsing json response failed: %w", perr)
|
||||
return nil, json.Null(nil), api_error(int(perr), "invalid JSON in API response"), false
|
||||
}
|
||||
|
||||
a, is_arr := v.(json.Array)
|
||||
if !is_arr {
|
||||
log.error("API response was not in JSON array format")
|
||||
json.destroy_value(v)
|
||||
return nil, json.Null(nil), api_error(0, "API response was not a JSON array"), false
|
||||
}
|
||||
@@ -135,13 +137,14 @@ get_catalog_objects :: proc(
|
||||
) {
|
||||
sb := strings.builder_make()
|
||||
defer strings.builder_destroy(&sb)
|
||||
fmt.sbprintf(&sb, "%s/api/v1/objects?catalog=%s", endpoint(base_url, ""), catalog_name)
|
||||
fmt.sbprintf(&sb, "%s?catalog=%s", endpoint(base_url, "/api/v1/objects"), catalog_name)
|
||||
if region != "" {
|
||||
fmt.sbprintf(&sb, "®ion=%s", region)
|
||||
}
|
||||
fmt.sbprintf(&sb, "&limit=%d", 10_000)
|
||||
url := strings.to_string(sb)
|
||||
|
||||
|
||||
arr, val, err, ok := request_json_array(url)
|
||||
if !ok {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user