-
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 all 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,25 @@ | ||
package com.commercetools.sunrise.ctp.client; | ||
|
||
import com.commercetools.sunrise.framework.controllers.metrics.SimpleMetricsSphereClientProvider; | ||
import com.google.inject.AbstractModule; | ||
import io.sphere.sdk.client.SphereClient; | ||
import io.sphere.sdk.client.SphereClientConfig; | ||
|
||
import javax.inject.Singleton; | ||
|
||
/** | ||
* Module that allows to inject a {@link SphereClient} and its configuration. | ||
*/ | ||
public final class SphereClientModule extends AbstractModule { | ||
|
||
@Override | ||
protected void configure() { | ||
bind(SphereClientConfig.class) | ||
.toProvider(SphereClientConfigProvider.class) | ||
.in(Singleton.class); | ||
|
||
bind(SphereClient.class) | ||
.toProvider(SimpleMetricsSphereClientProvider.class) | ||
.in(Singleton.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package com.commercetools.sunrise.framework.localization; | ||
package com.commercetools.sunrise.ctp.project; | ||
|
||
import com.commercetools.sunrise.framework.localization.NoCountryFoundException; | ||
import com.commercetools.sunrise.framework.localization.NoCurrencyFoundException; | ||
import com.commercetools.sunrise.framework.localization.NoLocaleFoundException; | ||
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 +69,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,19 @@ | ||
package com.commercetools.sunrise.ctp.project; | ||
|
||
import com.google.inject.AbstractModule; | ||
import io.sphere.sdk.projects.Project; | ||
|
||
import javax.inject.Singleton; | ||
|
||
/** | ||
* Module that allows to inject a commercetools {@link Project}. | ||
*/ | ||
public final class ProjectModule extends AbstractModule { | ||
|
||
@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,31 @@ | ||
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; | ||
|
||
/** | ||
* Provides the commercetools {@link Project} associated with the injected {@link SphereClient}. | ||
*/ | ||
public final class ProjectProvider implements Provider<Project> { | ||
|
||
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)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
package com.commercetools.sunrise.framework.localization; | ||
|
||
import com.commercetools.sunrise.ctp.project.ProjectContext; | ||
import com.commercetools.sunrise.sessions.country.CountryInSession; | ||
import com.neovisionaries.i18n.CountryCode; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Provides the {@link CountryCode} instance of the country saved in the user's session. | ||
* If not set, it provides the default country from the project. | ||
*/ | ||
public final class CountryFromSessionProvider implements Provider<CountryCode> { | ||
|
||
private final ProjectContext projectContext; | ||
private final CountryInSession countryInSession; | ||
|
||
@Inject | ||
public CountryFromSessionProvider(final ProjectContext projectContext, final CountryInSession countryInSession) { | ||
CountryFromSessionProvider(final ProjectContext projectContext, final CountryInSession countryInSession) { | ||
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. Nice that you removed the |
||
this.projectContext = projectContext; | ||
this.countryInSession = countryInSession; | ||
} | ||
|
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.