package pinterest;

import com.google.gson.Gson;
import exceptions.PinterestException;
import fields.PinFields;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.utils.URIBuilder;
import responses.Pin;
import responses.Pins;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class Pinterest {
    private static final String PROTOCOL = "https";
    private static final String HOST = "api.pinterest.com";
    private static final String PIN_PATH = "/v1/pins/{PIN_ID}/";
    private static final String BOARD_PATH = "/v1/boards/{BOARD_NAME}/pins/";

    private final String accessToken;

    public Pinterest(String accessToken) {
        this.accessToken = accessToken;
    }

    public Pin retrievePinWithDefaultFields(final String id) {
        try {
            return new Gson().fromJson(IOUtils.toString(buildPinUri(id, null)), Pin.class);
        } catch (URISyntaxException | IOException e) {
            throw new PinterestException(e.getMessage(), e);
        }
    }

    public Pin retrievePinWithFields(final String id, final PinFields pinFields) {
        try {
            return new Gson().fromJson(IOUtils.toString(buildPinUri(id, pinFields.build())), Pin.class);
        } catch (URISyntaxException | IOException e) {
            throw new PinterestException(e.getMessage(), e);
        }
    }

    public Pins retrievePinsFromBoard(final String boardName) {
        try {
            return new Gson().fromJson(IOUtils.toString(buildBoardUri(boardName, null)), Pins.class);
        } catch (URISyntaxException | IOException e) {
            throw new PinterestException(e.getMessage(), e);
        }
    }

    public Pins retrievePinsFromBoard(final String boardName, final PinFields pinFields) {
        try {
            return new Gson().fromJson(IOUtils.toString(buildBoardUri(boardName, pinFields.build())), Pins.class);
        } catch (URISyntaxException | IOException e) {
            throw new PinterestException(e.getMessage(), e);
        }
    }

    private URI buildPinUri(final String id, final String fields) throws URISyntaxException {
        final URIBuilder uriBuilder =  new URIBuilder()
                .setScheme(PROTOCOL)
                .setHost(HOST)
                .setPath(PIN_PATH.replace("{PIN_ID}", id))
                .setParameter("access_token", accessToken);

        if (isNotBlank(fields)) {
            uriBuilder.setParameter("fields", fields);
        }

        return uriBuilder.build();
    }

    private URI buildBoardUri(final String name, final String fields) throws URISyntaxException {
        final URIBuilder uriBuilder = new URIBuilder()
                .setScheme(PROTOCOL)
                .setHost(HOST)
                .setPath(BOARD_PATH.replace("{BOARD_NAME}", name))
                .setParameter("access_token", accessToken);

        if (isNotBlank(fields)) {
            uriBuilder.setParameter("fields", fields);
        }

        return uriBuilder.build();
    }
}

@@ -589,16 +589,78 @@
 _ID%7D/%22;%0A
+    private static final String MY_PIN_PATH = %22/v1/me/pins/%22;%0A
     priv
@@ -729,16 +729,16 @@
 pins/%22;%0A
-
 %0A    pri
@@ -1488,32 +1488,675 @@
        %7D%0A    %7D%0A%0A
+    public Pins retrieveMyPinsWithDefaultFields(final String id) %7B%0A        try %7B%0A            return new Gson().fromJson(IOUtils.toString(buildMyPinUri(null)), Pins.class);%0A        %7D catch (URISyntaxException %7C IOException e) %7B%0A            throw new PinterestException(e.getMessage(), e);%0A        %7D%0A    %7D%0A%0A    public Pins retrieveMyPinsWithFields(final String id, final PinFields pinFields) %7B%0A        try %7B%0A            return new Gson().fromJson(IOUtils.toString(buildMyPinUri(pinFields.build())), Pins.class);%0A        %7D catch (URISyntaxException %7C IOException e) %7B%0A            throw new PinterestException(e.getMessage(), e);%0A        %7D%0A    %7D%0A%0A
     public Pins 
@@ -3015,32 +3015,32 @@
  .setHost(HOST)%0A
-
                 
@@ -3277,32 +3277,476 @@
 build();%0A    %7D%0A%0A
+    private URI buildMyPinUri(final String fields) throws URISyntaxException %7B%0A        final URIBuilder uriBuilder =  new URIBuilder()%0A                .setScheme(PROTOCOL)%0A                .setHost(HOST)%0A                .setPath(MY_PIN_PATH)%0A                .setParameter(%22access_token%22, accessToken);%0A%0A        if (isNotBlank(fields)) %7B%0A            uriBuilder.setParameter(%22fields%22, fields);%0A        %7D%0A%0A        return uriBuilder.build();%0A    %7D%0A%0A
     private URI 

Add methods to retrieve the user's Pins