-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide Project via injection #672
Changes from 4 commits
1d1391e
3e68e42
e719e09
e11b597
911e5ce
e884de7
7d91166
911b8a7
10ef43b
ea7bf5f
c8be5e0
edc76c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.commercetools.sunrise.ctp.project; | ||
|
||
import com.commercetools.sunrise.framework.injection.RequestScoped; | ||
import com.google.inject.AbstractModule; | ||
import com.neovisionaries.i18n.CountryCode; | ||
|
||
import javax.money.CurrencyUnit; | ||
import java.util.Locale; | ||
|
||
public final class LocalizationModule extends AbstractModule { | ||
|
||
@Override | ||
protected void configure() { | ||
bind(Locale.class) | ||
.toProvider(LocaleFromUrlProvider.class) | ||
.in(RequestScoped.class); | ||
bind(CountryCode.class) | ||
.toProvider(CountryFromSessionProvider.class) | ||
.in(RequestScoped.class); | ||
bind(CurrencyUnit.class) | ||
.toProvider(CurrencyFromCountryProvider.class) | ||
.in(RequestScoped.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package com.commercetools.sunrise.framework.localization; | ||
package com.commercetools.sunrise.ctp.project; | ||
|
||
import com.google.inject.ImplementedBy; | ||
import com.neovisionaries.i18n.CountryCode; | ||
import io.sphere.sdk.projects.Project; | ||
import play.Configuration; | ||
|
||
import javax.money.CurrencyUnit; | ||
import java.util.List; | ||
|
@@ -64,4 +66,8 @@ default boolean isCountrySupported(final CountryCode countryCode) { | |
default boolean isCurrencySupported(final CurrencyUnit currency) { | ||
return currencies().contains(currency); | ||
} | ||
|
||
static ProjectContext of(final Configuration globalConfig, final String configPath, final Project project) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me it looks like this method isn't used or do you plan to use it in tests? |
||
return new ProjectContextImpl(globalConfig, configPath, project); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.commercetools.sunrise.ctp.project; | ||
|
||
import com.google.inject.AbstractModule; | ||
import io.sphere.sdk.projects.Project; | ||
|
||
import javax.inject.Singleton; | ||
|
||
public final class ProjectModule extends AbstractModule { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name clearly states that it provides a project, but as an user you have to look at the source code to see what this module provides. What do you think about adding some javadoc here? |
||
|
||
@Override | ||
protected void configure() { | ||
bind(Project.class) | ||
.toProvider(ProjectProvider.class) | ||
.in(Singleton.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.commercetools.sunrise.ctp.project; | ||
|
||
import io.sphere.sdk.client.SphereClient; | ||
import io.sphere.sdk.client.SphereRequest; | ||
import io.sphere.sdk.projects.Project; | ||
import io.sphere.sdk.projects.queries.ProjectGet; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
import java.time.Duration; | ||
|
||
import static io.sphere.sdk.client.SphereClientUtils.blockingWait; | ||
|
||
public final class ProjectProvider implements Provider<Project> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now could even remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I would like to let developers to bind using this provider, in case they need to override some parts of an injection module and reuse others. |
||
|
||
private final SphereClient sphereClient; | ||
|
||
@Inject | ||
ProjectProvider(final SphereClient sphereClient) { | ||
this.sphereClient = sphereClient; | ||
} | ||
|
||
@Override | ||
public Project get() { | ||
final SphereRequest<Project> request = ProjectGet.of(); | ||
return blockingWait(sphereClient.execute(request), Duration.ofSeconds(30)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed from Module.java, because this file belongs to Starter Project and is therefore never updated with new releases, what makes migration more difficult. Now moved to LocalizationModule, which is enabled by default on reference.conf (default configuration), and can be disabled via configuration as explained in #671 in order to bind your own implementations.