I slowly made an Imgur client and everything works but I have few questions about how to make classes more OOP and cleaner.
- Is that correct to make
generateToken
static when it is private? Is calling the method from the constructor an anti-pattern?
public class ImgurClient extends Imgur { public ImgurClient(String client_id, String client_secret) { super(client_id, client_secret); generateToken(); } private static void generateToken(){ System.out.println("Enter into link, accept application and paste here full redirect url to get credentials"); System.out.println(ImgurURLs.getAuthorizeUrlWithClientID()); ImgurAuthentication imgurAuthentication = new ImgurAuthentication(); //todo metody statyczne? moze na gore? imgurAuthentication.getURLFromUserAndGenerateTokenFromImgurOauth(); } @Override public void getAccountBase() { } @Override void getAccountImages() { } }
This is the class where I parse redirect URL with tokens:
- Should every methodbe static here?
- I know calling two methods from one method is incorrect, but how should I make it better?
Extract methods are pretty similar. How should I make it better?
public class ImgurAuthentication { private final Pattern accessTokenPattern = Pattern.compile("access_token=([^&]*)"); private final Pattern refreshTokenPattern = Pattern.compile("refresh_token=([^&]*)"); private final Pattern expiresInPattern = Pattern.compile("expires_in=(\\d+)"); private final Pattern accountIdPattern = Pattern.compile("account_id=(\\d+)"); private Matcher matcher; private String url = getUrlFromUser(); public void getURLFromUserAndGenerateTokenFromImgurOauth(){ getUrlFromUser(); extractTokensValueFromURL(); } private String getUrlFromUser(){ Scanner scanner = new Scanner(System.in); return scanner.nextLine(); } private void extractTokensValueFromURL(){ try{ extractAccessToken(); extractRefreshToken(); extractExpiresIn(); extractAccountId(); }catch (IllegalStateException ex){ throw new IllegalArgumentException("Incorrect authorization url", ex); } } private void extractAccessToken(){ matcher = accessTokenPattern.matcher(url); matcher.find(); AccountCredentials.TOKEN=matcher.group(1); } private void extractRefreshToken(){ matcher = refreshTokenPattern.matcher(url); matcher.find(); AccountCredentials.REFRESH_TOKEN=matcher.group(1); } private void extractExpiresIn(){ matcher = expiresInPattern.matcher(url); matcher.find(); AccountCredentials.EXPIRES_IN = Long.parseLong(matcher.group(1)); } private void extractAccountId(){ matcher = accountIdPattern.matcher(url); matcher.find(); AccountCredentials.ACCOUNT_ID = Long.parseLong(matcher.group(1)); } }
This is my const
class where I keep everything about the user:
I know keeping
const
in an interface is an anti-pattern. How about that way?public final class AccountCredentials { private AccountCredentials(){} public static String CLIENT_ID = ""; public static String CLIENT_SECRET = ""; public static String TOKEN = ""; public static String REFRESH_TOKEN = ""; public static long EXPIRES_IN = -1; public static long ACCOUNT_ID=-1; }