1111import javax .ws .rs .core .StreamingOutput ;
1212
1313import org .gitlab4j .api .GitLabApi .ApiVersion ;
14+ import org .gitlab4j .api .models .Project ;
1415
1516/**
1617 * This class is the base class for all the sub API classes. It provides implementations of
@@ -24,6 +25,42 @@ public AbstractApi(GitLabApi gitLabApi) {
2425 this .gitLabApi = gitLabApi ;
2526 }
2627
28+ /**
29+ * Returns the project ID or path from the provided Integer, String, or Project instance.
30+ *
31+ * @param obj the object to determine the ID or path from
32+ * @return the project ID or path from the provided Integer, String, or Project instance
33+ * @throws GitLabApiException if any exception occurs during execution
34+ */
35+ public Object getProjectIdOrPath (Object obj ) throws GitLabApiException {
36+
37+ if (obj == null ) {
38+ throw (new RuntimeException ("Cannot determine ID or path from null object" ));
39+ } else if (obj instanceof Integer ) {
40+ return (obj );
41+ } else if (obj instanceof String ) {
42+ return (urlEncode (((String ) obj ).trim ()));
43+ } else if (obj instanceof Project ) {
44+
45+ Integer id = ((Project ) obj ).getId ();
46+ if (id != null && id .intValue () > 0 ) {
47+ return (id );
48+ }
49+
50+ String path = ((Project ) obj ).getPath ();
51+ if (path != null && path .trim ().length () > 0 ) {
52+ return (urlEncode (path .trim ()));
53+ }
54+
55+ throw (new RuntimeException ("Cannot determine ID or path from provided Project instance" ));
56+
57+ } else {
58+
59+ throw (new RuntimeException ("Cannot determine ID or path from provided " + obj .getClass ().getSimpleName () +
60+ " instance, must be Integer, String, or Project instance" ));
61+ }
62+ }
63+
2764 protected ApiVersion getApiVersion () {
2865 return (gitLabApi .getApiVersion ());
2966 }
0 commit comments