Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a9cc19f7 authored by Stefan Niedermann's avatar Stefan Niedermann Committed by Niedermann IT-Dienstleistungen
Browse files

#401 Provide convenience classes for OCS requests

parent 681652fb
Loading
Loading
Loading
Loading
+22 −9
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ package com.nextcloud.android.sso.api;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

@@ -89,6 +90,18 @@ public class NextcloudAPI {
        new Thread(NextcloudAPI.this.networkRequest::connectApiWithBackoff).start();
    }

    /**
     * <blockquote>
     * If you need to make multiple calls, keep the NextcloudAPI open as long as you
     * can. This way the services will stay active and the connection between the
     * files app and your app is already established when you make subsequent requests.
     * Otherwise you'll have to bind to the service again and again for each request.
     * <cite><a href="https://github.com/nextcloud/Android-SingleSignOn/issues/120#issuecomment-540069990">Source</a></cite>
     * </blockquote>
     *
     * <p>A good place <em>depending on your actual implementation</em> might be {@link Activity#onStop()}.</p>
     */
    @SuppressWarnings("JavadocReference")
    public void stop() {
        gson = null;
        networkRequest.stop();
@@ -164,11 +177,11 @@ public class NextcloudAPI {
    /**
     * The InputStreams needs to be closed after reading from it
     *
      * @deprecated Use {@link #performNetworkRequestV2(NextcloudRequest)}
     * @see <a href="https://github.com/nextcloud/Android-SingleSignOn/issues/133">Issue #133</a>
     * @param request {@link NextcloudRequest} request to be executed on server via Files app
     * @return InputStream answer from server as InputStream
     * @throws Exception or {@link SSOException}
     * @see <a href="https://github.com/nextcloud/Android-SingleSignOn/issues/133">Issue #133</a>
     * @deprecated Use {@link #performNetworkRequestV2(NextcloudRequest)}
     */
    @Deprecated
    public InputStream performNetworkRequest(NextcloudRequest request) throws Exception {
+58 −0
Original line number Diff line number Diff line
package com.nextcloud.android.sso.model.ocs;

import com.google.gson.annotations.SerializedName;

/**
 * <p>This is a basic implementation for the <code>capabilities</code> endpoint which maps version and theming properties.<br>
 * You can use it directly in combination with {@link OcsResponse} or extend it.</p>
 * <p>Example usage with Retrofit:</p>
 * <pre>
 * {@code
 * @GET("/ocs/v2.php/cloud/capabilities?format=json")
 * Call<OcsResponse<OcsCapabilities>> getCapabilities();
 * }
 * </pre>
 *
 * @see <a href="https://docs.nextcloud.com/server/latest/developer_manual//client_apis/OCS/ocs-api-overview.html#capabilities-api">Capabilities API</a>
 */
@SuppressWarnings("unused, SpellCheckingInspection")
public class OcsCapabilitiesResponse {
    public OcsVersion version;
    public OcsCapabilities capabilities;

    public static class OcsVersion {
        public int major;
        public int minor;
        public int macro;
        public String string;
        public String edition;
        public boolean extendedSupport;
    }

    public static class OcsCapabilities {
        public OcsTheming theming;

        public static class OcsTheming {
            public String name;
            public String url;
            public String slogan;
            public String color;
            @SerializedName("color-text")
            public String colorText;
            @SerializedName("color-element")
            public String cnextcloudapiolorElement;
            @SerializedName("color-element-bright")
            public String colorElementBright;
            @SerializedName("color-element-dark")
            public String colorElementDark;
            public String logo;
            public String background;
            @SerializedName("background-plain")
            public boolean backgroundPlain;
            @SerializedName("background-default")
            public boolean backgroundDefault;
            public String logoheader;
            public String favicon;
        }
    }
}
+34 −0
Original line number Diff line number Diff line
package com.nextcloud.android.sso.model.ocs;

import com.google.gson.annotations.SerializedName;

/**
 * <p>A generic wrapper for <a href="https://www.open-collaboration-services.org/">OpenCollaborationServices</a> responses.</p>
 * <p>This is a convenience class for API endpoints located at <code>/ocs/…</code> which all have an identical wrapping structure.<br>
 * It is usually <strong>not</strong> used in APIs of 3rd party server apps like <a href="https://deck.readthedocs.io/en/latest/API/">Deck</a> or <a href="https://github.com/nextcloud/notes/blob/master/docs/api/README.md">Notes</a></p>
 * <p>Example usage with Retrofit:</p>
 * <pre>
 * {@code
 * @GET("/ocs/v2.php/cloud/capabilities?format=json")
 * Call<OcsResponse<OcsCapabilitiesResponse>> getCapabilities();
 * }
 * </pre>
 *
 * @param <T> defines the payload type of this {@link OcsResponse}.
 */
@SuppressWarnings("unused, SpellCheckingInspection")
public class OcsResponse<T> {
    public OcsWrapper<T> ocs;

    public static class OcsWrapper<T> {
        public OcsMeta meta;
        public T data;

        public static class OcsMeta {
            public String status;
            @SerializedName("statuscode")
            public int statusCode;
            public String message;
        }
    }
}
 No newline at end of file
+40 −0
Original line number Diff line number Diff line
package com.nextcloud.android.sso.model.ocs;

import com.google.gson.annotations.SerializedName;

/**
 * <p>This is a basic implementation for the <code>users</code> endpoint which maps often required properties.<br>
 * You can use it directly in combination with {@link OcsResponse} or extend it.</p>
 * <p>Example usage with Retrofit:</p>
 * <pre>
 * {@code
 * @GET("/ocs/v2.php/cloud/users/{search}?format=json")
 * Call<OcsResponse<OcsUser>> getUser(@Path("search") String userId);
 * }
 * </pre>
 * @see <a href="https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-api-overview.html#user-metadata">User API</a>
 */
@SuppressWarnings("SpellCheckingInspection")
public class OcsUser {
    public boolean enabled;
    @SerializedName("id")
    public String userId;
    public long lastLogin;
    public OcsQuota quota;
    public String email;
    @SerializedName("displayname")
    public String displayName;
    public String phone;
    public String address;
    public String website;
    public String twitter;
    public String[] groups;
    public String language;
    public String locale;

    public static class OcsQuota {
        public long free;
        public long used;
        public long total;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ public class MainActivity extends AppCompatActivity {
                        /* Show result on the UI thread */
                        runOnUiThread(() -> ((TextView) findViewById(R.id.result)).setText(
                                getString(R.string.account_info,
                                        user.displayname,
                                        user.displayName,
                                        serverInfo.capabilities.theming.name,
                                        serverInfo.version.semanticVersion))
                        );
Loading