Bu yazımda gitti gidiyor api leri ile kategori verilerini nasıl çekeriz ve gitti gidiyorun algoritması ile bu aldığımız kategori verileri ile nasıl bir kategori hiyerarşisi oluşturabilirz bundan bahsediyor olacağım.
Öncelikle servisleri kullanabilmeniz için dev.gittigidiyor.com üzerinden api bilgilerine sahip olmanız gerekiyor.
Kategorileri Getirme
1.BasicAuthentication işlemi : Bu işlem api bilgileriniz ile servisleri çağırmadan önce yapmanız gereken bir authentication işlemidir.Buradaki veriler gitti gidiyor geliştirici hesabınızda tanımladığınız bilgilerdir.
Aşağıdaki gibi bir metod oluşturarak bu işlemi yapabiliriz.
public void SetBasicAuthentication(string apiKey, string secretKey, string roleName, string rolePass)
{
/*create authentication*/
AuthConfig config = new AuthConfig();
config.ApiKey = apiKey;
config.SecretKey = secretKey;
config.RoleName = roleName;
config.RolePass = rolePass;
ConfigurationManager.setAuthParameters(config);
}//End Method
2.Kategori Verilerini Getirme : Gitti gidiyor üzerindeki tüm kategorileri getirme işlemi.Normal şartlarda kategorileri getirmek için tek bir metod kullanmak yetmiyor.Fakat sayfalama tekniği kullanarak bu verileri sayfa sayfa 100 er adet çekebiliyoruz.
Ben bu örneğimde kendim bir class oluşturup bu class üzerinden gidiyor olacağım.Adımlarımız şu şekilde olacak.
1-Hiyerarşik olarak kategori verilerinin tutulacağı bir class oluşturma.
2-Gitti Gidiyor api leri ile tüm kategorileri çekme
3-Alınan kategori verilerinin hiyerarşik olarak düzenlenmesi.
/*1-Hiyerarşik olarak kategori verilerinin tutulacağı bir class oluşturma.*/
public partial class Integration_Category
{
private string ParentID { get; set; };
private string CategoryID { get; set; };
private string CategoryName { get; set; };
private string CategoryPath { get; set; };
private bool IsEnabled { get; set; };
}//End Class
/*2-Gitti Gidiyor api leri ile tüm kategorileri çekme*/
public List<Integration_Category> GetGittiGidiyorCategories()
{
List<Integration_Category> listOfCategories = new List<Integration_Category>();
/*set properties definitions*/
bool getDetail = true;
bool getSubCatDetail = true;
string lang = "tr";
int pageSize = 100;
int pageCount = 1;
int currentPageCount = 0;
do
{
GittiGidiyor.Category.categoryServiceResponse catList = categoryService.getCategories((currentPageCount * pageSize),
pageSize,
getDetail,
getSubCatDetail,
true, lang);
if (catList == null)
break;
if (catList.categories == null)
break;
foreach (categoryType category in catList.categories)
{
Integration_Category currentCat = new Integration_Category()
{ CategoryID = category.categoryCode,
CategoryName = category.categoryName,
ParentID = ""
};
listOfCategories.Add(currentCat);
}//End Foreach
/*pageCount is setted at first loop*/
if (pageCount == 1)
{
pageCount = (int)Math.Ceiling(catList.categoryCount / 100M);
}
currentPageCount++;
} while (currentPageCount != pageCount);
/*Kategoria ağacının getirileceği aşağıda yer alan metod*/
listOfCategories = this.GetCategoryTree(listOfCategories);
return listOfCategories;
}//End Method
/*3-Alınan kategori verilerinin hiyerarşik olarak düzenlenmesi.*/
private List<Integration_Category> GetCategoryTree(List<Integration_Category> tempCategoryList)
{
List<Integration_Category> listOfCategory = new List<Integration_Category>();
foreach (Integration_Category parentCat in tempCategoryList.Where(c => c.CategoryID.Length == 1))
{
parentCat.ParentID = null;
parentCat.IntegrationType = GittiGidiyorSetting.IntegrationType;
parentCat.IsEnabled = true;
parentCat.CategoryPath = parentCat.CategoryName;
listOfCategory.Add(parentCat);
int level = 1;
listOfCategory.AddRange(this.GetSubCategoryList(tempCategoryList, parentCat.CategoryPath, parentCat.CategoryID, level + 1));
}//End Foreach
return listOfCategory;
}//End Method
/*alt kategorileri getiren recursive method*/
private List<Integration_Category> GetSubCategoryList(
List<Integration_Category> tempCategoryList,
string categoryPath,
string parentCategoryCode,
int level)
{
List<Integration_Category> listOfSubCategory = new List<Integration_Category>();
foreach (Integration_Category subCat in tempCategoryList.Where(c => c.CategoryID.StartsWith(parentCategoryCode) && c.CategoryID.Length == level))
{
subCat.IntegrationType = GittiGidiyorSetting.IntegrationType;
subCat.IsEnabled = true;
subCat.ParentID = parentCategoryCode;
subCat.CategoryPath = categoryPath + " / " + subCat.CategoryName;
listOfSubCategory.Add(subCat);
listOfSubCategory.AddRange(this.GetSubCategoryList(tempCategoryList, subCat.CategoryPath, subCat.CategoryID, level + 1));
}//End Foreach
return listOfSubCategory;
}//End Method
Öncelikle servisleri kullanabilmeniz için dev.gittigidiyor.com üzerinden api bilgilerine sahip olmanız gerekiyor.
Kategorileri Getirme
1.BasicAuthentication işlemi : Bu işlem api bilgileriniz ile servisleri çağırmadan önce yapmanız gereken bir authentication işlemidir.Buradaki veriler gitti gidiyor geliştirici hesabınızda tanımladığınız bilgilerdir.
Aşağıdaki gibi bir metod oluşturarak bu işlemi yapabiliriz.
public void SetBasicAuthentication(string apiKey, string secretKey, string roleName, string rolePass)
{
/*create authentication*/
AuthConfig config = new AuthConfig();
config.ApiKey = apiKey;
config.SecretKey = secretKey;
config.RoleName = roleName;
config.RolePass = rolePass;
ConfigurationManager.setAuthParameters(config);
}//End Method
2.Kategori Verilerini Getirme : Gitti gidiyor üzerindeki tüm kategorileri getirme işlemi.Normal şartlarda kategorileri getirmek için tek bir metod kullanmak yetmiyor.Fakat sayfalama tekniği kullanarak bu verileri sayfa sayfa 100 er adet çekebiliyoruz.
Ben bu örneğimde kendim bir class oluşturup bu class üzerinden gidiyor olacağım.Adımlarımız şu şekilde olacak.
1-Hiyerarşik olarak kategori verilerinin tutulacağı bir class oluşturma.
2-Gitti Gidiyor api leri ile tüm kategorileri çekme
3-Alınan kategori verilerinin hiyerarşik olarak düzenlenmesi.
/*1-Hiyerarşik olarak kategori verilerinin tutulacağı bir class oluşturma.*/
public partial class Integration_Category
{
private string ParentID { get; set; };
private string CategoryID { get; set; };
private string CategoryName { get; set; };
private string CategoryPath { get; set; };
private bool IsEnabled { get; set; };
}//End Class
/*2-Gitti Gidiyor api leri ile tüm kategorileri çekme*/
public List<Integration_Category> GetGittiGidiyorCategories()
{
List<Integration_Category> listOfCategories = new List<Integration_Category>();
/*set properties definitions*/
bool getDetail = true;
bool getSubCatDetail = true;
string lang = "tr";
int pageSize = 100;
int pageCount = 1;
int currentPageCount = 0;
do
{
GittiGidiyor.Category.categoryServiceResponse catList = categoryService.getCategories((currentPageCount * pageSize),
pageSize,
getDetail,
getSubCatDetail,
true, lang);
if (catList == null)
break;
if (catList.categories == null)
break;
foreach (categoryType category in catList.categories)
{
Integration_Category currentCat = new Integration_Category()
{ CategoryID = category.categoryCode,
CategoryName = category.categoryName,
ParentID = ""
};
listOfCategories.Add(currentCat);
}//End Foreach
/*pageCount is setted at first loop*/
if (pageCount == 1)
{
pageCount = (int)Math.Ceiling(catList.categoryCount / 100M);
}
currentPageCount++;
} while (currentPageCount != pageCount);
/*Kategoria ağacının getirileceği aşağıda yer alan metod*/
listOfCategories = this.GetCategoryTree(listOfCategories);
return listOfCategories;
}//End Method
/*3-Alınan kategori verilerinin hiyerarşik olarak düzenlenmesi.*/
private List<Integration_Category> GetCategoryTree(List<Integration_Category> tempCategoryList)
{
List<Integration_Category> listOfCategory = new List<Integration_Category>();
foreach (Integration_Category parentCat in tempCategoryList.Where(c => c.CategoryID.Length == 1))
{
parentCat.ParentID = null;
parentCat.IntegrationType = GittiGidiyorSetting.IntegrationType;
parentCat.IsEnabled = true;
parentCat.CategoryPath = parentCat.CategoryName;
listOfCategory.Add(parentCat);
int level = 1;
listOfCategory.AddRange(this.GetSubCategoryList(tempCategoryList, parentCat.CategoryPath, parentCat.CategoryID, level + 1));
}//End Foreach
return listOfCategory;
}//End Method
/*alt kategorileri getiren recursive method*/
private List<Integration_Category> GetSubCategoryList(
List<Integration_Category> tempCategoryList,
string categoryPath,
string parentCategoryCode,
int level)
{
List<Integration_Category> listOfSubCategory = new List<Integration_Category>();
foreach (Integration_Category subCat in tempCategoryList.Where(c => c.CategoryID.StartsWith(parentCategoryCode) && c.CategoryID.Length == level))
{
subCat.IntegrationType = GittiGidiyorSetting.IntegrationType;
subCat.IsEnabled = true;
subCat.ParentID = parentCategoryCode;
subCat.CategoryPath = categoryPath + " / " + subCat.CategoryName;
listOfSubCategory.Add(subCat);
listOfSubCategory.AddRange(this.GetSubCategoryList(tempCategoryList, subCat.CategoryPath, subCat.CategoryID, level + 1));
}//End Foreach
return listOfSubCategory;
}//End Method