• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 17581574457

09 Sep 2025 11:47AM UTC coverage: 13.561% (-0.04%) from 13.601%
17581574457

push

github

tkuhn
Add QueryRef convenience class

406 of 3856 branches covered (10.53%)

Branch coverage included in aggregate %.

1070 of 7028 relevant lines covered (15.22%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/main/java/com/knowledgepixels/nanodash/component/ApiResultComponent.java
1
package com.knowledgepixels.nanodash.component;
2

3
import java.util.HashMap;
4

5
import org.apache.wicket.Component;
6
import org.apache.wicket.markup.html.basic.Label;
7
import org.nanopub.extra.services.ApiResponse;
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10

11
import com.knowledgepixels.nanodash.ApiCache;
12
import com.knowledgepixels.nanodash.QueryRef;
13

14
/**
15
 * A component that retrieves and displays the result of an API call.
16
 * It uses AjaxLazyLoadPanel to load the content lazily and shows a loading indicator while waiting for the response.
17
 */
18
public abstract class ApiResultComponent extends ResultComponent {
19

20
    private final String queryName;
21
    private final HashMap<String, String> params;
22
    private ApiResponse response = null;
×
23
    private static final Logger logger = LoggerFactory.getLogger(ApiResultComponent.class);
×
24

25
    /**
26
     * Constructor for ApiResultComponent.
27
     *
28
     * @param id        the component id
29
     * @param queryName the name of the API query to be executed
30
     * @param params    a map of parameters to be passed to the API query
31
     */
32
    public ApiResultComponent(String id, String queryName, HashMap<String, String> params) {
33
        super(id);
×
34
        this.queryName = queryName;
×
35
        this.params = params;
×
36
    }
×
37

38
    public ApiResultComponent(String id, QueryRef queryRef) {
39
        super(id);
×
40
        this.queryName = queryRef.getName();
×
41
        this.params = queryRef.getParams();
×
42
    }
×
43

44
    /**
45
     * Constructor for ApiResultComponent with a single parameter.
46
     *
47
     * @param id         the component id
48
     * @param queryName  the name of the API query to be executed
49
     * @param paramKey   the key of the parameter to be passed to the API query
50
     * @param paramValue the value of the parameter to be passed to the API query
51
     */
52
    public ApiResultComponent(String id, String queryName, String paramKey, String paramValue) {
53
        this(id, queryName, getParams(paramKey, paramValue));
×
54
    }
×
55

56
    private static HashMap<String, String> getParams(String paramKey, String paramValue) {
57
        final HashMap<String, String> params = new HashMap<>();
×
58
        params.put(paramKey, paramValue);
×
59
        return params;
×
60
    }
61

62
    /**
63
     * {@inheritDoc}
64
     */
65
    @Override
66
    public Component getLazyLoadComponent(String markupId) {
67
        while (true) {
68
            if (!ApiCache.isRunning(queryName, params)) {
×
69
                try {
70
                    response = ApiCache.retrieveResponse(queryName, params);
×
71
                    if (response != null) break;
×
72
                } catch (Exception ex) {
×
73
                    return new Label(markupId, "<span class=\"negative\">API call failed.</span>").setEscapeModelStrings(false);
×
74
                }
×
75
            }
76
            try {
77
                Thread.sleep(100);
×
78
            } catch (InterruptedException ex) {
×
79
                logger.error("Interrupted while waiting for API response", ex);
×
80
            }
×
81
        }
82
        return getApiResultComponent(markupId, response);
×
83
    }
84

85
    /**
86
     * {@inheritDoc}
87
     */
88
    @Override
89
    protected boolean isContentReady() {
90
        return response != null || !ApiCache.isRunning(queryName, params);
×
91
    }
92

93
    /**
94
     * Abstract method to be implemented by subclasses to provide the component that displays the API result.
95
     *
96
     * @param markupId the markup ID for the component
97
     * @param response the API response to display
98
     * @return a Component that displays the API result
99
     */
100
    // TODO Use lambda instead of abstract method?
101
    public abstract Component getApiResultComponent(String markupId, ApiResponse response);
102

103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc