예제 #1
0
 public void SetUp()
 {
     config            = (ResilienceConfig)ResilienceConfig.GenerateCommon();
     config.ShortDelay = 1;
     config.LongDelay  = 1;
     instance          = CreateCommonResilience();
 }
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterModule <LoggingModule>();
            builder.RegisterType <HttpClient>();
            builder.RegisterType <SimpleEncryptor>().As <IEncryptor>();

            builder.RegisterInstance(ResilienceConfig.GenerateCommon()).As <IResilienceConfig>();
            builder.RegisterType <CommonResilience>().As <IResilience>();
            builder.RegisterType <GenericClientFactory>().As <IGenericClientFactory>();

            builder.RegisterType <SimilarityDetector>().As <ISimilarityDetector>();
            builder.RegisterType <OneHotEncoder>().As <IWordVectorEncoder>();
            builder.RegisterType <CosineSimilarityDistance>().As <IDistance>();

            builder.RegisterType <CaptionHandler>().As <ICaptionHandler>();
            builder.RegisterType <SimilarMediaTags>().As <ISimilarMediaTags>();

            builder.RegisterType <FileCachedCall>().As <ICachedCall>();

            builder.RegisterType <WebSmartTags>().Named <ISmartTags>("Web");
            builder.Register(ctx => new MediaSmartTags(ctx.Resolve <ILogger <MediaSmartTags> >(), ctx.Resolve <ICaptionHandler>(), ctx.ResolveNamed <ISmartTags>("Web"))).As <IMediaSmartTags>();
            builder.RegisterType <InstaSmartTags>().As <ISmartTags>();
            builder.RegisterType <TagEnricher>().As <ITagEnricher>();
            builder.RegisterType <InstaSmartTagsByLocation>().As <ISmartTagsByLocation>();
            builder.RegisterType <PlainSerializer>().As <ISerializer>();
            builder.RegisterDecorator <EncryptedSerializer, ISerializer>();
            builder.RegisterType <FileSessionHandler>().As <ISessionHandler>();
            builder.RegisterInstance(new UserSessionData {
                UserName = user, Password = password
            });
            builder.Register(
                ctx =>
                InstaApiBuilder.CreateBuilder()
                .SetUser(ctx.Resolve <UserSessionData>())
                .UseLogger(ctx.Resolve <ILoggerFactory>())
                .SetRequestDelay(Delay)
                .Build())
            .InstancePerLifetimeScope();
            base.Load(builder);
        }
        /// <summary>
        ///     Create new API instance
        /// </summary>
        /// <returns>
        ///     API instance
        /// </returns>
        /// <exception cref="ArgumentNullException">User auth data must be specified</exception>
        public IInstaApi Build()
        {
            if (user == null)
            {
                user = UserSessionData.Empty;
            }

            if (httpHandler == null)
            {
                httpHandler = new HttpClientHandler();
            }

            if (httpClient == null)
            {
                httpClient = new HttpClient(httpHandler)
                {
                    BaseAddress = new Uri(InstaApiConstants.InstagramUrl)
                };
            }

            if (requestMessage == null)
            {
                if (device == null)
                {
                    device = AndroidDeviceGenerator.GetRandomAndroidDevice();
                }

                requestMessage = new ApiRequestMessage
                {
                    PhoneId  = device.PhoneGuid.ToString(),
                    Guid     = device.DeviceGuid,
                    Password = user?.Password,
                    Username = user?.UserName,
                    DeviceId = ApiRequestMessage.GenerateDeviceId(),
                    AdId     = device.AdId.ToString()
                };
            }

            if (string.IsNullOrEmpty(requestMessage.Password))
            {
                requestMessage.Password = user?.Password;
            }

            if (string.IsNullOrEmpty(requestMessage.Username))
            {
                requestMessage.Username = user?.UserName;
            }

            try
            {
                InstaApiConstants.TimezoneOffset = (int)DateTimeOffset.Now.Offset.TotalSeconds;
            }
            catch
            {
            }

            if (httpRequestProcessor == null)
            {
                httpRequestProcessor = new HttpRequestProcessor(delay, httpClient, httpHandler, requestMessage, loggerFactory?.CreateLogger <HttpRequestProcessor>());
            }

            if (apiVersionType == null)
            {
                apiVersionType = InstaApiVersionType.Version86;
            }

            var instaApi = new InstaApi(loggerFactory?.CreateLogger <InstaApi>(),
                                        httpRequestProcessor,
                                        user,
                                        apiVersionType.Value,
                                        device,
                                        new CommonResilience(loggerFactory?.CreateLogger <CommonResilience>(),
                                                             ResilienceConfig.GenerateCommon()));

            return(instaApi);
        }